I was recently playing with the Arduino Yun for a whole set of new projects, and I discovered that Arduino was providing an official REST API for the Arduino Yun, in the form of an Arduino sketch. And after using it for a while, I thought about a new idea: what not do the same for the Arduino platform in general?
But first, let see what a REST API really is. It’s actually a concept that is widely used by many web applications like SaaS (Software as a Service). REST stands for REpresentational State Transfer, and is a communication architecture that was created back in 2000. It defines many constraints that the architecture has to follow, the most important features being:
- A client/server communication: a client sends a request to a server, and the server answers accordingly
- A stateless communication: every request has to contain all the information so the server can understand it, without having to rely on some information stored on the server
- A uniform interface to easily identify resources on the server
This allowed to standardise communication between web applications, and made them more scalable, faster, and simplified the development of more complex applications. And for our Arduino projects, it allows to standardise the communication between your Arduino and the external world via WiFi or Ethernet, and develop complex applications without having to modify your Arduino sketch every time.
Indeed, I have been using the CC3000 WiFi chip for a while, and the problem that I encountered while developing web applications using the CC3000 chip is that I had to create a new Arduino sketch for every application, that needs to be coordinated with the rest of the application, for example an interface running on my computer. For example, using this REST API, switching the state from a pin on the Arduino board can be done directly in the browser by typing the following URL:
With this REST API, it’s easy to load a sketch once for all on your Arduino, and then only work on the interface on your computer that makes REST calls on your Arduino board. And for now, this kind of interface was only available on the official Arduino boards, like the WiFi & Ethernet shields, and the Yun.
But I wanted to have the same on my CC3000 WiFi chip: make a sketch that will create a web server on the Arduino board, and then accept REST commands from an external client, like from an interface running on my computer. That's why I created a dedicated library that encapsulate these ideas, and even allows you to create your own functions that can be called from the API. And in this guide, we are going to see how to use this library.