RestServer and RestClient

RestServer/RestClient

These two examples, for the Arduino Uno WiFi, show how to use the Ciao library to access the digital and analog pins on the board through REST calls. 
It demonstrates how you can create your own API when using REST style calls through the browser.

Hardware Required

You can also use the component of the WorkShop kit or simply the LED  and button of Tinkerkit connected with some Wires Female/Male.
For more details visit our Store.

Circuit

You need to create two simple circuits one for the Rest Server and the another for the Rest Client.

  • Rest Server circuit:

Connect the Led anode (usually the longer pin) in series to a 220Ω resistor and connect it to the board pin 13. After connect the cathode to GND, as shown in the picture. 
Rest server circuit

  • Client Server circuit:

In the other board, connect a switch or button to pin 2 following this indications: 
- If you use a tinkerkit pushbutton, it has 3 output: connect + to 5V, - to GND and Signal to pin2. 
Tinkerkit pushbutton 
- If you use a button with four pins then connect a pin of the button to 5V, another pin, that nearest, to a resistor connected to GND of the board. Finally connect another pin of the button to the digital pin 2, as shown in figure. 
Client server circuit

Now plug in the boards to PC and upload the sketches below.

Code

  • RestServer Code:

Upload this sketch on an Arduino UNO WiFi to create your Rest Server.

 
 
/*
 
RestServer.ino
 
Arduino Uno WiFi Ciao example
 
This example for the Arduino Uno WiFi shows how to use the
Ciao library to access the digital and analog pins
on the board through REST calls. It demonstrates how
you can create your own API when using REST style
calls through the browser.
 
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
 
http://www.arduino.org/learning/tutorials/restserver-and-restclient
 
*/
 
#include <Wire.h>
#include <ArduinoWiFi.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("Status: 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("Status: 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("Status: 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("Status: 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("Status: 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
}
 
 
  • RestClient Code:

Now that you have realized your Rest Server then use the other Arduino Uno Wifi to make the Rest calls, uploading the sketch below.

 
 
#include <Wire.h>
#include <Ciao.h>
 
#define CONNECTOR     "rest"
#define SERVER_ADDR   "192.168.60.114" // change ip address with your server ip address
 
int buttonState; //this variable tracks the state of the button, low if not pressed, high if pressed
int ledState = HIGH; //this variable tracks the state of the LED, negative if off, positive if on
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50;   // the debounce time; increase if the output flickers
String command = "/arduino/mode/13/output";
int previous_value = LOW;
 
void setup() {
Ciao.begin();
Ciao.write(CONNECTOR, SERVER_ADDR, command);
pinMode(2, INPUT);
 
}
 
void loop() {
 
//sample the state of the button - is it pressed or not?
buttonState = digitalRead(2);
 
//filter out any noise by setting a time buffer
if ( (buttonState == HIGH) && (previous_value == LOW) && (millis() - lastDebounceTime) > debounceDelay ) {
    if (ledState == HIGH){
        command = "/arduino/digital/13/0";
        ledState = LOW;
    }
    else{
        command = "/arduino/digital/13/1";
        ledState = HIGH;
    }
   
    lastDebounceTime = millis(); //set the current time
    CiaoData data = Ciao.write(CONNECTOR, SERVER_ADDR, command);
    if (!data.isEmpty()){
      Ciao.println( "State: " + String (data.get(1)) );
      Ciao.println( "Response: " + String (data.get(2)) );
    }
    else{
      Ciao.println ("Write Error");
    }
 
}
 
previous_value = buttonState;
 
}
 
 

Output

Uploaded the sketches on the boards, open the browser and insert the server address. 
Arduino address 
Access to web panel and click on WIFI Console. 
If the Server works then it will be shown the screen below: 
WiFii console 1/2 
Now click on the pushbutton connected on the Rest Client, the led on pin 13 goes ON. 
Click again the pushbutton, the led goes OFF and on the WIFI CONSOLE it will be displayed this screen: 
WiFi console 2/2