Reference Language | Libraries | Comparison | Changes
Ciao
MQTT connector
Description
This Connector allows to communicate using MQTT in an Arduino sketch.
Below the steps that you should follow to use the MQTT connector:
- setup Shell connector;
- enable the connector;
- write a simple sketch;
- upload the sketch and enjoy.
Setup connector
You can find MQTT Connector configuration file 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.
Example
#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 :-( ");
}
}
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.