Adding IoT Flare to a Hot Springs and Spa Business

Develop the REST API

Now that we can read voltages, we begin on coding our API. There are several resources available for creating a REST API using Restify, so it was easy to create a couple of routes (API endpoints) for integrations to use.

Specifically:

  • GET /temperatures — returns a JSON array with all three pool temperature data objects.

  • GET /temperatures/:pool_id — returns JSON object with :pool_id temperature data where :pool_id is between 0 and 2.

Example response from GET /temperatures:


[
   {
      "id":0,
      "vdcin":"5151",
      "temperature":"73.4",
      "temperatureUnit":"F",
      "error":null
   },
   {
      "id":1,
      "vdcin":"4915",
      "temperature":"64.9",
      "temperatureUnit":"F",
      "error":null
   },
   {
      "id":2,
      "vdcin":"5143",
      "temperature":"73.1",
      "temperatureUnit":"F",
      "error":null
   }
]

This should be all that the foundation needs for now, since we are going to be reading only temperature data. In the future, we might add pressure sensor data for maintenance purposes, but that's a ways off. The only tricky part of developing the node.js app was the asynchronous nature of node.js. Making the system call and then dealing with the callbacks and whatnot was a little challenging to get the brain wrapped around. So, JavaScript promises were used to make the logic easier to understand. Other modules to help deal with synchronous calls exist, but the author was familiar with JavaScript promises, so we went with those.

Note: as a nice little bonus, the code was also ported to Python, which can be seen in the bhs-api code repository. Why? Mostly born out of frustration with dealing in asynchronous system calls in JavaScript, but also somewhat for the fun of it.

Also note: instructions for setting up the node.js app that powers our API can be found in the embeddedarm/bhs-api repository's README file.

Example Web Page Using API

A simple web page was created as a demonstration and proof of concept for advertising the temperature data online. It uses jQuery and AJAX to make a call to the REST API server and uses the returned JSON object to display data. This is done every 10 seconds to show off the capabilities of displaying "real-time" (quote-unquote) temperature data.

______________________