myDevices Cayenne claims to be the world's first drag and drop IoT builder. It provides a dash board on which the user can set up predefined widgets in which sensor data can be displayed or from which actuators can be controlled. It also allows to trigger actions when certain values appear on the sensors read out. For example, a ventilator can be switch on when the values read from a thermometer reach a certain limit. Cayenne uses MQTT a simple subscribe-publish protocol running on top of TCP.
MQTT (Message Queuing Telemetry Transport: a publish-subscribe based message protocol running of top of TCP. A broker takes subscriptions for messages of a certain “topic” and it sends messages, coming from publishers to everyone having subscribed to the topic.
We can easily test the mechanism using the mosquitto MQTT broker. Install mosquitto and mosquitto-clients using apt. mosquitto is the broker and mosquitto_sub allows you to subscribe to a topic while mosquitto_pub will publish a topic, see the screen dump below:
Up to now the communication between the MQTT broker and its client happened on the PC and on the same machine. In micropython-lib you will find a module umqtt.simple with methods
In your publishing MQTT client (replacing mosquito_pub) you must create an MQTTClient object, connect it to the broker (this is where you need the IP address of the server on which the broker is running) and then publish messages to the DCSIT topic. These messages you can see on mosquitto_sub on the PC, subscribed to the DCSIT topic.
It is not much more difficult to write a client equivalent to mosquitto_sub. Here we register a callback routine which MQTTClient will call when a message for the subscribed topic has arrived. After registration of the callback and subscription to the topic we simply wait for messages to come in, messages which will be treated in the callback. In the callback below the messages are simply printed but it would also be possible to control hardware in the callback routine. Imagine messages like
myDevices Cayenne supplies an MQTT broker and an application to visualize data sent to it in nice graphical widgets. It can also save a history of measurement in a plot and allows you you download the historic data to your PC as a csv file for further analysis. It supplies a dash board on which you can create the Graphical User Interface objects most suited for your measurement. Control widgets are also available. You can also run a Cayenne app on your smart phone and visualize your sensor data or control your actuators from there.
You may want to have a look at the slides from the IoT workshop ( session 3 and session 4) at the African Internet Summit 2019 in Kampala, Uganda where access to Cayenne from an ESP8266 was explained.
Cayenne supplies libraries for many programming languages giving access to its broker. The Cayenne language binding for Python unfortunately depends on the Eclipse Paho MQTT library, which is not available in MicroPython. Since this library is OpenSource however, it was not too difficult to adapt it to MicroPython's umqtt library and thus making it work on MicroPython as well. The adapted library uses the same API as the original code which means that the example programs remain unchanged. The library is available in our MicroPython binary.
I | Attachment | History | Action | Size | Date | Who | Comment |
---|---|---|---|---|---|---|---|
![]() |
cayenneDashboard.png | r1 | manage | 55.7 K | 2020-08-31 - 19:35 | UliRaich | |
![]() |
mosquittoDemo.png | r1 | manage | 522.0 K | 2020-09-05 - 21:10 | UliRaich | |
![]() |
mqtt.png | r1 | manage | 85.8 K | 2020-08-31 - 19:32 | UliRaich | |
![]() |
mqttClient.png | r1 | manage | 35.7 K | 2020-09-05 - 20:49 | UliRaich | |
![]() |
mqttSubClient.png | r1 | manage | 55.2 K | 2020-09-05 - 20:55 | UliRaich |