DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building REST API Backend Easily With Ballerina Language
  • What Is API-First?
  • Breaking Up a Monolithic Database with Kong
  • Aggregating REST APIs Calls Using Apache Camel

Trending

  • Introduction to Retrieval Augmented Generation (RAG)
  • Engineering LLMOps: Building Robust CI/CD Pipelines for LLM Applications on Google Cloud
  • You Don't Get to Retrofit Trust: Why API Security Must Be Designed In, Not Bolted On
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Implement Arduino REST API in IoT Projects

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.

By 
Francesco Azzola user avatar
Francesco Azzola
·
May. 25, 16 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
52.2K Views

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.

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.

arduino REST API Web Protocols IoT Requests Library application LEd Use case

Published at DZone with permission of Francesco Azzola. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building REST API Backend Easily With Ballerina Language
  • What Is API-First?
  • Breaking Up a Monolithic Database with Kong
  • Aggregating REST APIs Calls Using Apache Camel

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook