Arduino Function: Ciao REST 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, Restserver 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 connector.
This connector allows to make “http” requests.
Below the steps that you should follow to use the Rest connector:

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

Setup connector

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

{
 "name" : "rest",
 "description" : "REST connector for the Ciao Core",
 "authors": ["Arduino Team <swdev@arduino.org>;"],
 "repository" : "https://github.com/arduino-org/Ciao",
 "version" : "0.0.1",
 "params" : {
            }
}
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:
In this Connector there aren’t parameters to set.

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 connector please edit the file at the following path: /usr/lib/python2.7/ciao/conf/rest.ciao.json.conf

{
 "name" : "rest",
 "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 Connector. Once done Ciao Core will be ready and configured to use Rest Connector.

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

Write a simple sketch

Below it is shown an example to try the Rest Connector, in particular in this sketch is shown how it is possible turn on/off a LED, using Rest calls.

#include <Wire.h>
#include <Ciao.h>
#define CONNECTOR     "rest"
#define SERVER_ADDR   "192.168.1.1" // 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;
}

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

Written by arduino.org