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

Calling our sensor API from JavaScript

We will call our /Momentary API in two ways. First, we will update the gauge on the screen. This is done by resetting the src property on the IMG tag of the gauge. To make sure the browser reloads the image, we add a query parameter that is unique, but not used by the API. Since it is an IMG tag, the browser will automatically add an Accept header field, telling the resource it wants an image. Code is straightforward:

   var Images = document.getElementsByTagName("IMG"); 
   Images[0].src = "/Momentary?TP=" + Date(); 

For updating the table, we use an XMLHttpRequest object. Here, we must manually set the Accept header to make sure we get the representation we desire. We need to follow the process. When we ...

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