14
Getting Some REST
RESTful APIs are an increasingly popular mechanism for allowing devices to
communicate on the Internet, sans human involvement. For Web-based services,
RESTful API is realized through HTTP methods such as POST, GET, PUSH, and
DELETE.
In a nutshell, a RESTful API leverages what looks like a website address (URL) to
exchange requests with a Web server. It is a simple yet elegant method to
achieve autonomous communication between the Internet and the "Things" that
comprise the IoT. Every Web service will provide their own API, but in general the
format is fairly consistent. For this project, the Ubidots IoT backend service was
selected (Figure 8). They have a well-documented API that is very easy to use.
The documentation can be found here.
In both devices, when uploading or downloading the sensor readings, the data
itself is passed as a JSON message (JSON is short for JavaScript Object
Notation), which is an open standard file format that has the benefit of being both
efficient for digital transmission but also is humanly readable. For uploading the
weather data to the Ubidots server, we use the POST method:
POST /api/v1.6/collections/values/?token=
HTTP/1.1
Host: things.ubidots.com
Content-Type: application/json
Content-Length:
[{"variable": "TemperatureVariableID", "value":79.5},
{"variable": "PressureVariableID", "value":1010.2},
{"variable": "", "value":54.67}]
Where , , and would be replaced with a unique alphanumeric string for each
variable that is generated by Ubidots for your particular account.
will be replaced with a unique, account-based token from the Ubidots service.
Think of the token as a random, very hard to guess account name. Lastly,
must be calculated for each message. It
is the number of characters, including the letters, numbers, spaces, and special
characters.
There are software libraries to parse JSON messages or a custom lightweight
function can be created that is application specific. For embedded developers
that are accustomed to total control of their device, RESTful APIs present a
paradigm shift in that the API provider can (and often will) update their API
occasionally. This means the embedded system developer must respond with
firmware updates to ensure the devices and IoT backend can continue to
communicate.
To download the temperature data into the servo-connected Huzzah, we use
GET:
GET /api/v1.6/devices//temperature/
values?page_size=1&token=XXX HTTP/1.1
Host: things.ubidots.com
Connection: close
In this example, would be replaced with the name you
created for your device on the Ubidots website. Also, the part of the GET request
that reads "?page_size=1" means that we are only interested in getting the
most recent sensor readings. If it read "?page_size=5" then the server would
return the data from the five most recent sensor readings.
Figure 7: TE Connectivity
provides all the needed
software libraries on their
GitHub repository.
Figure 8: Ubidots provides
for a simple yet powerful and
inexpensive IoT backend.