O'Reilly logo

Mastering Internet of Things by Peter Waher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding momentary values

Adding content to the XML response is not straightforward. We add each value, if available, as a child element. At the end, we close the root element:

   if (this.lastLight.HasValue) 
   { 
         Response.Write("<Light value='"); 
         Response.Write(ToString(this.lastLight.Value, 2)); 
         Response.Write("' unit='%'/>"); 
   } 
 
   if (this.lastMotion.HasValue) 
   { 
         Response.Write("<Motionvalue='"); 
         Response.Write(this.lastMotion.Value ?  
               "true" : "false"); 
         Response.Write("'/>"); 
   } 
 
   Response.Write("</Momentary>"); 
} 

Since the method is called from a synchronous resource, the response will be sent automatically. The GitHub project also contains the JSON method, which is implemented in the same manner and which returns the same type of JSON as in the MQTT ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required