Basic functions

Now that everything is in place, we can test the project. We'll start with the basic functions of the REST API, which are the basic functions of Arduino: pinMode, digitalRead, digitalWrite, analogRead, and analogWrite.

The software part is actually not complicated at all: everything is already done for you in the WiFi example of the library. For that, simply open the file called "WiFi_CC3000.ino" in the examples folder of the aREST library. Save this file to another folder so you can edit it.

You will have to change these lines so the sketch can connect to your WiFi network:
Download: file
#define WLAN_SSID       "yourSSID"
#define WLAN_PASS       "yourPassword"
For now, let's not look at the rest of the sketch, and simply compile the sketch & upload it to the Arduino board. You can now open the Serial monitor. If everything works fine, you should see that the board is connecting to your WiFi networks, and starts listening for incoming connections.

You can now head over to your favorite web browser. First, we have to set one of the LED pin as an output. Just type:

http://arduino.local/mode/7/o

This should set the pin as an output, and you should get the confirmation printed in your browser:

Setting pin D7 to output
If it doesn't work at this point, don't worry (yet!). The sketch is using the mDNS library so the board can be accessed using arduino.local. But you can also use the IP address of the board directly. To get the address, simply uncomment the "displayConnectionDetails" function in the sketch and look at the IP on the Serial monitor. You can then use this IP in place of the arduino.local in your browser.
We can then continue using the REST API and turn the LED on. For that, we need a digitalWrite function used on pin number 7. This can be done again in the browser by typing:

http://arduino.local/digital/7/1

The LED should turn on and you should have the confirmation message in your browser:

Pin D7 set to 1

At this point, you should see how powerful the project is: you can now build your projects on the client side only, without having the modify the Arduino sketch every time. You can have the sketch loaded once for all, and then simply change which REST call you make.

Let's try one more of the basic functions, for example analogRead(). To test it, simply connect a wire from the Arduino 3.3V pin to the analog input A0 of the Arduino board. As the Arduino Analog-Digital converter works with a 5V range over 1024 values, we should get something around "722" being readout by the project (3.3/5 * 1024). Simply type the following command in your browser:

http://arduino.local/analog/0/r

I got this printed in my browser:

{"value": 722, "id": "008", "name": "mighty_cat", "connected": true}

Here you can see that the value is return in a JSON container, so it can be used directly by other apps and languages, for example by PHP. You can also notice that other information is being returned in the JSON container, that identify the board with an id and a name. You can change these directly in the Arduino sketch.

Note that you can find all the information about the aREST library and the REST API on the GitHub repository of the project, by reading the README file.
This guide was first published on Apr 17, 2014. It was last updated on Apr 17, 2014. This page (Basic functions) was last updated on Sep 18, 2019.