Arduino Function: Ciao MQTT 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.


Ciao provides already some "internal" Connectors like 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 MQTT Connector.

Here the steps to follow to let MCU communicate over MQTT:

  1. setup MQTT connector
  2. enable the connector
  3. write a simple sketch
  4. upload the sketch and enjoy

Setup connector

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

You can find MQTT conf at the following path: /usr/lib/python2.7/ciao/connectors/mqtt/mqtt.json.conf
{
"name" : "mqtt",
"description" : "MQTT (v3.1) connector for Ciao Core",
"version" : "0.0.1",
"ciao": {
"host" : "127.0.0.1",
"port" : 8900
},
"params" : {
"host" : "YOUR_MQTT_IP_OR_HOSTNAME",
"port" : 1883,
"username" : "USERNAME_IF_REQUIRED",
"password" : "PASSWORD_IF_REQUIRED",
"clean_session": true,
"qos": 2,
"subscribed_topic": [ "TOPIC_TO_SUBSCRIBE" ],
"client_id": "",
"lwt_topic": "",
"lwt_message": ""
}
}

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:

  • host (required): the IP or the hostname of the MQTT server you want to use (must be a string)
  • port (required): specifies the port your MQTT server is listening to (must be an integer)
  • username and password: provide username to use on the board and its password (must be string)
  • clean_session: (boolean) specifies if it needs to delete the pending messages relative to a previous session. Default is set TRUE.
  • qos: (integer) default this value is set 2, lower values improve the efficiency but increases latency.
  • subscribed_topic (required): specifies the topic that it need to execute via MQTT.
  • client_id (required): specifies an username, it must be univocal.
  • lwt_topic(last will testament topic) specifies a topic where it will be written a message if you are disconnected.
  • lwt_message(last will testament message): specifies a message that it will be written if  you are disconnected.

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 MQTT connector please edit the file at the following path: /usr/lib/python2.7/ciao/conf/mqtt.ciao.json.conf
{
"name" : "mqtt",
"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 MQTT connector. Once done your LininoOS will be ready and configured to use Ciao Core and MQTT connector.

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

Write a simple sketch

Following few lines of code to use MQTT connector and let MCU interact with an mqtt broker. 

The only parameter you will have to change is variable TOPIC to match the topic subscribed you want to communicate with.
#include <Ciao.h>

#define LED 13
String TOPIC="topic_subscribed";

void setup() {
pinMode(LED,OUTPUT);
Ciao.begin();
}

void loop() {

CiaoData data = Ciao.read("mqtt");

if(!data.isEmpty() && !data.isError()){

String id = data.get(0);
String sender = data.get(1);
String message = data.get(2);

message.toLowerCase();

if(message == "ciao" )
Ciao.write("mqtt", TOPIC,"Hello, i'm Arduino :-) ");
else if ( message == "led on"){
digitalWrite(LED,HIGH);
Ciao.write("mqtt", TOPIC,"LED ON");
}
else if ( message == "led off"){
digitalWrite(LED,LOW);
Ciao.write("mqtt", TOPIC,"LED OFF");
}
else
Ciao.write("mqtt", TOPIC,"Sorry i don't understand :-( ");
}
}
For more information or examples about Ciao Connectors visit:
 
https://github.com/arduino-org/Ciao/tree/master/ciao/connectors

Written by arduino.org