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 Freethe 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:
...
// create arest instance
arest rest = arest();
// neopixel init
adafruit_neopixel pixels = adafruit_neopixel(numpixels, pin,
neo_grb + neo_khz800);
void setup() {
serial.begin(115200);
// register rgb function
rest.function("rgb", setpixelcolor);
serial.println("try dhcp...");
if (ethernet.begin(macadd) == 0) {
serial.println("dhcp fail...static ip");
ethernet.begin(macadd , ip, mydns, mygateway) ;
}
server.begin();
serial.print("server ip: ");
serial.println(ethernet.localip());
pixels.begin();
serial.println("setup complete.\n");
}
void loop() {
// listen for incoming clients
ethernetclient client = server.available();
rest.handle(client);
wdt_reset();
}
int setpixelcolor(string hexcolor) {
hexcolor="0x" + hexcolor;
serial.println("hex color " + hexcolor);
long n = strtol( &hexcolor[0], null, 16);
serial.println("n :" + string(n));
long r = n >> 16;
long g = n >> 8 & 0xff;
long b = n & 0xff;
// set single pixel color
return 1;
}
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
rgb
.
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:
xxxxxxxxxx
http://192.168.1.5/rgb?param=_ffff00
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.
Comments