Implement Arduino REST API in IoT Projects
Francesco Azzola demonstrates how to use REST-style requests with an Arduino and LED device and introduces the aREST library.
Join the DZone community and get the full member experience.Join For Free
the arduino rest api is a mechanism to exchange data between arduino and other external systems. when exploiting the arduino rest api, a client application reads or sends information to an arduino board. a typical use case for the arduino rest api is an external system or application that retrieves sensor values. the api can be used in iot projects when differents systems and boards are connected together and exchange information. even iot cloud platforms use this mechanism. it is useful when an external application (client) sends a request and arduino replies with some data. the api works over an http protocol so these requests are synchronous. in an iot application, there are other protocols that are much more efficient than http (like mqtt). arduino rest api over http plays an important role in a client-server scenario where arduino acts as a server. mqtt, for example, uses a different pattern like publish-subscriber.
arduino rest api: arest library
to implement the rest api paradigm there is an interesting library called arest . this library is a framework that supports rest services and provides several features. it supports different dev boards like arduino, raspberry pi, es8266, etc. you can find more information at the arest website. this library is simple to use and can be downloaded directly from arduino library through arduino ide.
using this library we can implement arduino rest api paradigm because arest support
- reads pin values in rest style
- writes pin values in rest style
- remote sketch function calls
for example, an external application or system can read the pin value using a simple http request. moreover, the same app or system can set the pin value using an http rest request. this is useful when we have an app that runs on a smartphone that wants to interact with an arduino board. this interaction takes place using arduino rest api.
one interesting aspect of arest library is the capability to expose arduino sketch function in a rest style. these sketch functions are called directly using a rest http request.
arduino rest api implementation
now that we know the basic concepts about arduino rest api and how to use it to integrate arduino with an external system, it is time to put it in practice. in the example, we want to control an led strip using rest api calls. the sketch is simple because we have to focus on the arduino rest api. the led strip is a
neopixels rgb stick board
and uses the adafruit library, and it is possible to select the single rgb led color. the sketch below shows how to wire it to arduino uno.
this picture uses different neopixel components but the connections are the same.
using the arduino rest api request, we want to set the led strip color. the color is passed to the sketch function as a parameter in hex format. this example demonstrates how powerful is this library. the arduino code is simple:
the arduino function that we want to make available in the arduino rest api is
. so the sketch registers it at line 36 calling it
let’s run the sketch on the arduino uno board and make a simple http request using the browser. let us suppose we want to set the red color, the result is shown below:
these are some screenshots with different led colors, controlled by rest requests from a browser:
and below the video showing arduino at work:
hopefully, you have learned how to use the arduino rest api to send and receive data from arduino using rest style requests.
Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.