RestServer


Description

In this tutorial is shown how to access to the digital and analog pins of the board through REST calls, using an Arduino UNO WiFi.
In paricular this tutorial demonstrates how to create your own API using REST style.

Warning:
Please, it's important to upgrade at the latest ESP firmware, using the Arduino UNO firmware updater tool, before to continue this tutorial.

Hardware 

Circuit

Connect the Led to the pin13 or use the Led13 integrated on the board.
You can use an analog sensor to the pin2.
After connect the board to PC using an USB cable.

Code

Now upload the code below, using the Arduino IDE:

Warning:
This tutorial is available only using the Arduino IDE 1.8.x and downloading the UNOWiFiDev.Edition library from Library Manager.

/*
File: RestServer.ino
This example for the Arduino Uno WiFi shows how to access the digital and analog pins
of the board through REST calls. It demonstrates how you can create your own API using
REST style.

Possible commands created in this shetch:

"/arduino/digital/13" -> digitalRead(13)
"/arduino/digital/13/1" -> digitalWrite(13, HIGH)
"/arduino/analog/2/123" -> analogWrite(2, 123)
"/arduino/analog/2" -> analogRead(2)
"/arduino/mode/13/input" -> pinMode(13, INPUT)
"/arduino/mode/13/output" -> pinMode(13, OUTPUT)

This example code is part of the public domain

Note: works only with Arduino Uno WiFi Developer Edition.

http://www.arduino.org/learning/tutorials/boards-tutorials/restserver-and-restclient
*/

#include <Wire.h>
#include <UnoWiFiDevEd.h>


void setup() {
Wifi.begin();
Wifi.println("REST Server is up");
}

void loop() {

while(Wifi.available()){
process(Wifi);
}
delay(50);

}

void process(WifiData client) {
// read the command
String command = client.readStringUntil('/');

// is "digital" command?
if (command == "digital") {
digitalCommand(client);
}

// is "analog" command?
if (command == "analog") {
analogCommand(client);
}

// is "mode" command?
if (command == "mode") {
modeCommand(client);
}
}

void digitalCommand(WifiData client) {
int pin, value;

// Read pin number
pin = client.parseInt();

// If the next character is a '/' it means we have an URL
// with a value like: "/digital/13/1"
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}
else {
value = digitalRead(pin);
}

// Send feedback to client
client.println("HTTP/1.1 200 OK\n");
client.print(F("Pin D"));
client.print(pin);
client.print(F(" set to "));
client.println(value);
client.print(EOL); //char terminator

}

void analogCommand(WifiData client) {
int pin, value;

// Read pin number
pin = client.parseInt();

// If the next character is a '/' it means we have an URL
// with a value like: "/analog/5/120"
if (client.read() == '/') {
// Read value and execute command
value = client.parseInt();
analogWrite(pin, value);

// Send feedback to client
client.println("HTTP/1.1 200 OK\n");
client.print(F("Pin D"));
client.print(pin);
client.print(F(" set to analog "));
client.println(value);
client.print(EOL); //char terminator

}
else {
// Read analog pin
value = analogRead(pin);

// Send feedback to client
client.println("HTTP/1.1 200 OK\n");
client.print(F("Pin A"));
client.print(pin);
client.print(F(" reads analog "));
client.println(value);
client.print(EOL); //char terminator

}
}

void modeCommand(WifiData client) {
int pin;

// Read pin number
pin = client.parseInt();

// If the next character is not a '/' we have a malformed URL
if (client.read() != '/') {
client.println(F("error"));
client.print(EOL); //char terminator
return;
}

String mode = client.readStringUntil('\r');

if (mode == "input") {
pinMode(pin, INPUT);
// Send feedback to client
client.println("HTTP/1.1 200 OK\n");
client.print(F("Pin D"));
client.print(pin);
client.println(F(" configured as INPUT!"));
client.print(EOL); //char terminator
return;
}

if (mode == "output") {
pinMode(pin, OUTPUT);
// Send feedback to client
client.println("HTTP/1.1 200 OK\n");
client.print(F("Pin D"));
client.print(pin);
client.println(F(" configured as OUTPUT!"));
client.print(EOL); //char terminator
return;
}

client.print(F("error: invalid mode "));
client.println(mode);
client.print(EOL); //char terminator
}


OUTPUT

Uploaded the sketch, follow these steps:
- open a browser;
- if you want to turn on the Led 13 then type on the browser: ipaddress or hostname.local/arduino/13/1
Led13 ON
- If you want to turn off the Led 13 then type on the browser: ipaddress or hostname.local/arduino/13/0
Turn OFF Led13
- If you want to check the Led 13 status then type on the browser: ipaddress or hostname.local/arduino/13
Led status
- If you want to set for example the pin 13 as INPUT  then type on the browser: ipaddress or hostname.local/arduino/mode/13/input
set as input
- If you want to set for example the pin13 as OUTPUT  then type on the browser: ipaddress or hostname.local/arduino/mode/13/output
set as output
-If you want to read an analog value for example on the pin 2 then type on the browser: ipaddress or hostname.local/arduino/analog/2
Read analog value

- Instead if you want to set an analog value then type on the browser: ipaddress or hostname.local/arduino/analog/2/123
write analog value
If you haven't configured your board then click here.