Tags:
create new tag
view all tags

MQTT and Cayenne

Introduction

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

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.

mqtt.png

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:

mosquittoDemo.png

Accessing the MQTT broker from MicroPython

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

  • connect
  • publish
  • subscribe
  • wait_msg
and the possibility to register a callback routine, which is called when a message is published on a subscribed topic. To test things, start the mosquitto broker on the PC and find the PCs IP address (use ifconfig)

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.

mqttClient.png

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

  • LED on
  • LED off
which can be used to control the builtin LED on the ESP32 CPU board.

mqttSubClient.png

Cayenne

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 measurements in a plot and allows you to 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 measurements. Control widgets are also available. The WEB site with the dash board can also be run on your smart phone and you can 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.

Interfacing to Cayenne

Cayenne supplies libraries for many programming languages giving access to its broker. It uses its own dedicated MQTT messages. 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 included in our MicroPython binary.

The screen dump below shows a typical Cayenne dashboard, which is in fact the solution of one of the last exercises in this course.

cayenneSHT30andLED.png

On the left hand side it shows the IoT projects on which I had been working with the IoT course SHT30 project selected. There are two number widgets for temperature and humidity values coming from the SHT30 with an icon for temperature and another one for relative humidity. Then you see the history of temperature and humidity measurements over the last few minutes and finally a push button used to control the built-in LED on the ESP32 CPU board. As you can see, the LED is currently "on".

The rest of this chapter will lead you to such a result.

The first steps

Before being able to do anything in Cayenne you have to sign up at https://mydevices.com/cayenne/signup.

cayenneSignUp.png

Once you are logged into Cayenne you will have to define your first project. Since none of the devices proposed corresponds to the ESP32 WeMos D1 mini, select: "Bring your own thing"

bringYourOwn.png

This will show you the Cayenne credentials for your new project (my own MQTT user code and password are partially blanked out):

cayenneCredentials.png

As you can see, Cayenne supplies you with 3 code numbers

  • one number corresponding to your MQTT user ID
  • another one corresponding to your user's password
  • and a device ID corresponding to your IoT project.
These three numbers will be needed in your ESP32 Cayenne client to be able to connect to the Cayenne dashboard corresponding to this IoT project. You can also see that Cayenne is now waiting for the device to connect.

-- Uli Raich - 2020-08-31

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng bringYourOwn.png r1 manage 197.1 K 2020-09-08 - 19:28 UliRaich  
PNGpng cayenneCredentials.png r1 manage 65.3 K 2020-09-08 - 19:41 UliRaich  
PNGpng cayenneDashboard.png r1 manage 55.7 K 2020-08-31 - 19:35 UliRaich  
PNGpng cayenneSHT30andLED.png r1 manage 178.6 K 2020-09-08 - 18:56 UliRaich  
PNGpng cayenneSignUp.png r1 manage 23.7 K 2020-09-08 - 19:19 UliRaich  
PNGpng cayenneSignup.png r1 manage 122.2 K 2020-09-08 - 19:18 UliRaich  
PNGpng dashboard.png r1 manage 68.6 K 2020-09-08 - 18:42 UliRaich  
PNGpng mosquittoDemo.png r1 manage 522.0 K 2020-09-05 - 21:10 UliRaich  
PNGpng mqtt.png r1 manage 85.8 K 2020-08-31 - 19:32 UliRaich  
PNGpng mqttClient.png r1 manage 35.7 K 2020-09-05 - 20:49 UliRaich  
PNGpng mqttSubClient.png r1 manage 55.2 K 2020-09-05 - 20:55 UliRaich  
Edit | Attach | Watch | Print version | History: r6 < r5 < r4 < r3 < r2 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r6 - 2020-10-05 - UliRaich
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback