Implement Arduino REST API in IoT Projects

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

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.

arduino rest api


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:

java




x
44


1
...
2
// create arest instance
3
arest rest = arest();
4
// neopixel init
5
adafruit_neopixel pixels = adafruit_neopixel(numpixels, pin, 
6
                                neo_grb + neo_khz800);
7
void setup() {
8
  serial.begin(115200);
9
 
          
10
  // register rgb function
11
  rest.function("rgb", setpixelcolor);
12
  serial.println("try dhcp...");
13
  if (ethernet.begin(macadd) == 0) {
14
    serial.println("dhcp fail...static ip");
15
    ethernet.begin(macadd , ip, mydns, mygateway) ;
16
  }
17
 
          
18
  server.begin();
19
  serial.print("server ip: ");
20
  serial.println(ethernet.localip());
21
 
          
22
  pixels.begin();
23
  serial.println("setup complete.\n");
24
}
25
 
          
26
void loop() {
27
  // listen for incoming clients
28
  ethernetclient client = server.available();
29
  rest.handle(client);
30
  wdt_reset();
31
}
32
 
          
33
int setpixelcolor(string hexcolor) {
34
  hexcolor="0x" + hexcolor;
35
  serial.println("hex color " + hexcolor);
36
  long n = strtol( &hexcolor[0], null, 16);
37
  serial.println("n :" + string(n));
38
  long r = n >> 16;
39
  long g = n >> 8 & 0xff;
40
  long b = n & 0xff;
41
 
          
42
  // set single pixel color
43
  return 1;
44
}



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:

java




xxxxxxxxxx
1


1
http://192.168.1.5/rgb?param=_ffff00



these are some screenshots with different led colors, controlled by rest requests from a browser:

arduino rest green

arduino rest blue

arduino rest red


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.

 

 

 

 

Top