Arduino UNO WiFi: RestClient and RestServer

Hi!

I would just like to seek help on the following codes regarding the Ciao Library.

My project is a Wi-Fi controlled Robot using a JoyStick Controller.

The client side contains a Potentiometer at A0 and A1.

The server side contains a Motor Driver Shield for Two DC Motors[\b] with the following fixed pin configurations:
- Pin 8 - Direction of Motor A
- Pin 9 - Speed of Motor A, PWM
- Pin 10 - Speed of Motor B, PWM
- Pin 11 - Direction of Motor B
Motor A has the Left Wheel, Motor B has the Right Wheel.
Here’s the code for my Client Side:
```
**#include <Wire.h>
#include <UnoWiFiDevEd.h>

#define CONNECTOR    “rest”
#define SERVER_ADDR  “192.168.1.34”

String command;
int xaxis, yaxis;

void setup() {
  Ciao.begin();
  Ciao.write(CONNECTOR, SERVER_ADDR, “/arduino/initialization/”);
  Ciao.print(“BEGIN TRANSMISSION\n”);
//  Serial.begin(9600);
}

void loop() {
  xaxis = map(analogRead(A0), 0, 1000, -1, 1);
  yaxis = map(analogRead(A1), 0, 1000, -1, 1);
//  Serial.print(“X = “), Serial.print(xaxis);
//  Serial.print(” | Y = “), Serial.print(yaxis), Serial.print(”\n”);
  if (xaxis == -1) {
    command = “/arduino/motor/1”;//LEFT
    Ciao.print(“LEFT\n”);
  }
  else if (xaxis == 1) {
    command = “/arduino/motor/2”;//RIGHT
    Ciao.print(“RIGHT\n”);
  }
  else if (yaxis == -1) {
    command = “/arduino/motor/3”;//BACKWARD
    Ciao.print(“BACKWARD\n”);
  }
  else if (yaxis == 1) {
    command = “/arduino/motor/4”;//FORWARD
    Ciao.print(“FORWARD\n”);
  }
  else{
    command = “/arduino/motor/0”;//STOPCHASSIS
    Ciao.print(“STOPCHASSIS\n”);
  }
  Ciao.write(CONNECTOR, SERVER_ADDR, “/arduino/motor”);
  delay(100);
}**
** **And here's the code for my Server side:** **
*/

  • “/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)
    */

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

void setup(){
    Wifi.begin();
    Wifi.println(“RECEIVING TRANSMISSION\n”);
}

void loop(){
  while(Wifi.available()){
    Wifi.println(“WIFI IS AVAILABLE.”);
    process(Wifi);
  }
  delay(100);
}

void process(WifiData client) {
  String command = client.readStringUntil(’/’);
  Wifi.println(“READ COMMAND.”);
  if (command == “motor”) {
    motorCommand(client);
  }
  if (command == “initialization”){
    initializationCommand(client);
  }
}

void initializationCommand(WifiData client){
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    client.print(EOL);
    return;
}

void motorCommand(WifiData client){
  int value = client.parseInt();
  if (value == 1){
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(10, LOW);
      client.print(EOL);
      return;
    }
    else if (value == 2){
      digitalWrite(9, LOW);
      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
      client.print(EOL);
      return;
    }
    else if (value == 3){
      digitalWrite(8, LOW);
      digitalWrite(9, HIGH);
      digitalWrite(10, HIGH);
      digitalWrite(11, HIGH);
      client.print(EOL);
      return;
    }
    else if (value == 4){
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
      client.print(EOL);
      return;
    }
    else if (value == 0){
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      client.print(EOL);
      return;
    }
}**
```
I can’t seem to get these two working. I am just following the Sample Sketches of RestServer.ino and RestClient.ino frameworks, but I can’t get both to work. I feel that I have a problem on the Server Side, and I have been tracing the problem through inserting WiFi.print(“WIFI IS AVAILABLE.”); at every beginning of each function. And, at the while(Wifi.available()) function, the WIFI IS AVAILABLE doesn’t print out at the WIFI CONSOLE. It seems like the Server Side is not connected/does not receive what the client sends.
Since, the Client Side only requires the Ciao.write() function to transmit, based on observations, it seems like I don’t get how the server works.
Please help me work out these codes! :frowning:
Thank you!

try to reference the WiFiData object, not copy it

void process(WifiData& client) {

Edit: I see that in the example it is not passed as reference.
the UnoWiFiDevEd is strange. I changed the firmware to WiFi Link

Juraj:
try to reference the WiFiData object, not copy it

void process(WifiData& client) {

Edit: I see that in the example it is not passed as reference.
the UnoWiFiDevEd is strange. I changed the firmware to WiFi Link

Hi Juraj! Thanks for this tip. Will look into this.

I got another concern. Data are sending very slowly. How do I speed up the transmission from Client to Server? Here is my updated and working code while waiting for your reply:

Client Side:

#include <Wire.h>
#include <UnoWiFiDevEd.h>
 
#define CONNECTOR     "rest" 
#define SERVER_ADDR   "192.168.1.102"

String command;
int xaxis, yaxis;
 
void setup() {
  Ciao.begin();
  Ciao.write(CONNECTOR, SERVER_ADDR, "/arduino/digital/0");
}
 
void loop() {
  xaxis = map(analogRead(A0), 0, 1000, -1, 1);
  yaxis = map(analogRead(A1), 0, 1000, -1, 1);
  flex = map(analogRead(A2), 0, 1000, -1, 1);
  if (xaxis == -1) {
      Ciao.write(CONNECTOR, SERVER_ADDR, "/arduino/digital/1");
  }
  else if (xaxis == 1) {
      Ciao.write(CONNECTOR, SERVER_ADDR, "/arduino/digital/2");
  }
  else if (yaxis == -1) {
      Ciao.write(CONNECTOR, SERVER_ADDR, "/arduino/digital/3");
  }
  else if (yaxis == 1) {
      Ciao.write(CONNECTOR, SERVER_ADDR, "/arduino/digital/4");
  }
  else{
      Ciao.write(CONNECTOR, SERVER_ADDR, "/arduino/digital/0");
  }
}

Server Side:

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

String command;

void setup(){
    Serial.begin(9600);
    Wifi.begin();
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
}

void loop(){
    while(Wifi.available()){
      process(Wifi);
    }
}

void process(WifiData client) {
  command = client.readStringUntil('/');
  if (command == "mode") {
    modeCommand(client);
    Serial.println(command);
  }
  if (command == "digital") {
    digitalCommand(client);
    Serial.println(command);
  }
  if (command == "analog") {
    analogCommand(client);
    Serial.println(command);
  }
}

void modeCommand(WifiData client) {
  int pin;
  pin = client.parseInt();
  if (client.read() != '/') {
    client.print(EOL);
    Serial.println(command);
  }
  String mode = client.readStringUntil('\r');
  if (mode == "input") {
    pinMode(pin, INPUT);
    client.print(EOL);
    Serial.println(command);
    return;
  }
  if (mode == "output") {
    pinMode(pin, OUTPUT);
    client.print(EOL);
    Serial.println(command);
  }
  client.print(EOL);
  Serial.print(command), Serial.print("\n");
}

void digitalCommand(WifiData client) {
  int value;
  value = client.parseInt();
  if (value == 1){
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, LOW);
  }
  else if (value == 2){
    digitalWrite(9, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
  }
  else if (value == 3){
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
  }
  else if (value == 4){
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
  }
  else if (value == 0){
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
  }
  client.print(EOL);
  Serial.println(value);
}

void analogCommand(WifiData client) {
  int pin, value;
  pin = client.parseInt();
  if (client.read() == '/') {
    value = client.parseInt();
    analogWrite(pin, value);
    client.print(EOL);
    Serial.println(command);
  }
  else {
    value = analogRead(pin);
    client.print(EOL);
    Serial.println(command);
  }
}

Apparently, the code works with fixed naming conventions (e.g. /arduino/analog, /arduino/digital, /arduino/mode) must remain as is and these three functions (e.g. analogCommand(), digitalCommand()
and modeCommand()) remain present throughout the code on the Server Side. And, yes; I agree that the current set-up for UnoWifiDevEd is weird.

Hopefully you can help me speed up the transmission! Thanks a lot!

only /arduino/ is fixed. it is a factory and esp-link firmware feature.

about why is it slow and a path to full speed, I write in Uno WiFi & WiFi Link unofficial documentation

Juraj:
only /arduino/ is fixed. it is a factory and esp-link firmware feature.

about why is it slow and a path to full speed, I write in Uno WiFi & WiFi Link unofficial documentation

I read through the documentation. And, from what I understand, I have to install a new firmware to my UNO WiFi Developer Edition boards? Also, among the example sketches, it seems that only the WiFiRestServer.ino is posted. How about WiFiRestClient.ino?

Also, do I have to be forced to use the WiFi Link library over the UnoWifiDevEd library in terms of speed?

Thank you!

You are not forced to WiFi Link. There are many possibilities and I am still in proces of exploring them.

With WiFi Link I was able to resolve some problems like the speed, thank the fact that the WiFi Link firmware is an Arduino sketch, so it was simple for me.

All examples of UnoWifiDevEd and WiFi Link and el-link are in the IDE after library installation.

one of ideas to try is using esp8266 SDK AT firmware with Uno WiFi. the <lib/SC16IS750.h> contains a Stream implementation for communicating with the esp in a way as SoftwareSerial is used with esp modules.

and there would by the possibility to switch to what I call ‘straight serial’. the communication with the esp would start over SC16IS750 and the sketch could set with AT command the esp pin 4 to LOW to switch to straight serial.