Arduino Function: Ciao REST SERVER Connector

Description

Arduino Ciao is a easy-to-use and powerful technology that enables Arduino sketches to communicate intuitively with the "outside World".
It aims to simplify interaction between microcontroller and Linino OS.
It’s compatible with all version of LininoOS but you will need to install it manually.
See here  for installation instructions.
The Ciao library provides already some "internal" Connectors as MQTT, Shell, File, Rest, Rest Server and other available on github.com/arduino-org repository.
The "internal" Connectors can be used setting up few parameters on LininoOS side and few lines on a Arduino sketch.

In particular here it is shown the Rest Server Connector.
This Connector allows to receive and manage “http” requests.
Below the steps that you should follow to use the Rest Server Connector:

  1. Setup Rest Server Connector;
  2. enable the Connector;
  3. write a simple sketch;
  4. upload the sketch and enjoy.

Setup connector

Before using Ciao Rest Server connector you have to configure it properly, this can be done by editing its configuration file.

You can find Rest Server configuration file at the following path: /usr/lib/python2.7/ciao/connectors/restserver/restserver.json.conf

{
"name" : "restserver",
"description" : "REST server connector for the Ciao Core",
"authors": ["Arduino Team <swdev@arduino.org>;"],
"repository" : "https://github.com/arduino-org/Ciao",
"version" : "0.0.2",
"params" : {
"port" : 80
},
"log" : {
"level" : "info"
}
}

The parameters at the beginning are for internal use, do NOT edit them (name, description, version, ciao) unless you know exactly what you are doing.

The configurable part is the one identified by "params" key:

  • “port”: specifies the communication port. Default the port is number 80 but the advanced users can change it to speed up the communication.

Enable connector

Each Ciao connector must have a configuration file for the Ciao Core, this simple file is mandatory to enable the connector.

To enable Rest Server connector please edit the file at the following path:
/usr/lib/python2.7/ciao/conf/restserver.ciao.json.conf

{
"name" : "restserver",
"enabled": true,
"type" : "managed",
[...]
}

The key enabled must be set to true (boolean value).
This is the only parameter you are required to edit in order to enable the Rest Server Connector. Once done Ciao Core will be ready and configured to use Rest Server Connector.

Let's write a simple Arduino sketch to use it.

Write a simple sketch

The example below shows how to use Rest Server Connector to receive and manage the Rest calls, in particular it is shown how to access to the analog and digital pin of the board via rest calls and to manage the switching on/off of a LED.

/*
supported boards:Yun,Tian.
Possible commands to send from the xmpp client:
*"/arduino/digital/PIN"       -> to read a digital PIN
*"/arduino/digital/PIN/VALUE"-> to write a digital PIN (VALUE:1/0)
*"/arduino/analog/PIN/VALUE"  -> to write in a PWM PIN(VALUE rang:0-255);
*"/arduino/analog/PIN"        -> to read a analog PIN
*"/arduino/servo/PIN/VALUE"   -> to write angle in a SERVO PIN(VALUE range:0-180);
*"/arduino/mode/PIN/VALUE"    -> to set the PIN mode (VALUE: input / output)
Example:
"/arduino/mode/13/output"-> pinMode(13, OUTPUT)
"/arduino/digital/13/1"     -> digitalWrite(13, HIGH)
*/

#include<Ciao.h>
#include<Servo.h>
Servo servo;

void setup() {
 Ciao.begin();
}

void loop() {
CiaoData data =Ciao.read("restserver");
if(!data.isEmpty()){
String id = data.get(0);
String sender = data.get(1);
String message = data.get(2);
message.toUpperCase();
String command[3];
splitString(message,"/",command,3);
execute(command,id);
}
}

void execute(String cmd[],String id) {
if(cmd[0]=="DIGITAL") {
digitalCommand(cmd,id);
}
elseif(cmd[0]=="ANALOG") {
analogCommand(cmd,id);
}
elseif(cmd[0]=="SERVO") {
servoCommand(cmd,id);
}
elseif(cmd[0]=="MODE") {
setMode(cmd,id);
}
else
Ciao.writeResponse("restserver",id,"sorry, i don't understand :(");
}

void servoCommand(String cmd[],String id){
  int pin, value;
pin =(cmd[1]).toInt();
if(cmd[2]!="-1") {
value =(cmd[2]).toInt();
if(value <=180&& value >=0){
servo.attach(pin);
servo.write(value);
Ciao.writeResponse("restserver",id,"Servo D"+String(pin)+" set to "+String(value)+" degrees");
}
    else
Ciao.writeResponse("restserver",id,"Invalid angle value");
}

  else
    Ciao.writeResponse("restserver",id,"Invalid command");
}

void digitalCommand(String cmd[],String id) {
int pin, value;
pin =(cmd[1]).toInt();
if(cmd[2]!="-1") {
value =(cmd[2]).toInt();
digitalWrite(pin, value);
if(value ==1)
      Ciao.writeResponse("restserver",id,"Pin D"+String(pin)+" ON");
    elseif(value ==0)
Ciao.writeResponse("restserver",id,"Pin D"+String(pin)+" OFF");
}
  elseif(cmd[2]=="-1") {
    value = digitalRead(pin);
    Ciao.writeResponse("restserver",id,"D"+String(pin)+" value = "+String(value));
  }
}

void analogCommand(String cmd[],String id) {
int pin, value;
pin =(cmd[1]).toInt();
if(cmd[2]!="-1") {
value =(cmd[2]).toInt();
analogWrite(pin, value);
Ciao.writeResponse("restserver",id,"D"+String(pin)+" set to analog");
}
  elseif(cmd[2]=="-1") {
    value = analogRead(pin);
    Ciao.writeResponse("restserver",id,"A"+String(pin)+" value = "+String(value));
}
}

void setMode(String cmd[],String id) {
 int pin;
pin =(cmd[1]).toInt();
if(cmd[2]=="INPUT") {
pinMode(pin, INPUT);
    Ciao.writeResponse("restserver",id," pin D"+String(pin)+" set in INPUT mode");
return;
}
  if(cmd[2]=="OUTPUT") {
pinMode(pin, OUTPUT);
Ciao.writeResponse("restserver",id," pin D"+String(pin)+" set in OUTPUT mode");
return;
}
  Ciao.writeResponse("restserver",id,"invalid mode");
}

For more information or examples about Ciao connector visit:
https://github.com/arduino-org/Ciao/tree/master/ciao/connectors

Written by arduino.org