Reference   Language | Libraries | Comparison | Changes

Ciao

Rest Server connector

Description

This connector allows to make “http” requests. Below the steps that you should follow to use the Rest connector:

  • setup Rest Server connector;
  • enable the connector;
  • write a simple sketch;
  • upload the sketch and enjoy.

Setup connector

You can find Rest Server Connector 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.

Example

/*
  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");
}
Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.

Share