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
setColor. 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.