End Presentation


TWiki Slide Show
Next
Setting up an IoT

Session 3: MQTT and Cayenne

Uli Raich

Formally CERN, Geneva, Switzerland

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 1 of 55





















TWiki Slide Show
Next
The Network Layers
The WeMos D1 mini has a built-in WiFi interface

In order to communicate with other IoT nodes we must therefore

connect to the WiFi network first

On top of WiFi we will run a TCP session

On top of TCP we will run the MQTT protocol

On top of MQTT we have dedicated topics and payloads imposed by Cayenne

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 2 of 55





















TWiki Slide Show
Next
Accessing the WiFi network
wifiDemoCode.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 3 of 55





















TWiki Slide Show
Next
Accessing the WiFi network from Micropython
wifiConnect.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 4 of 55





















TWiki Slide Show
Next
MQTT
MQTT (Message Queuing Telemetry Transport: a publish-subscribe based

message protocol running of top of TCP.

A processor can subscribe to messages of a certain “topic” and/or it can push its

results on a certain topic

mqttPostOfffice.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 5 of 55





















TWiki Slide Show
Next
MQTT Demo
mqttDemo.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 6 of 55





















TWiki Slide Show
Next
The Micropython library
micropython-lib.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 7 of 55





















TWiki Slide Show
Next
A MQTT example using umqtt.simple
umqttDemo.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 8 of 55





















TWiki Slide Show
Next
Sending dummy temperature data
mqttDummySensorDemo.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 9 of 55





















TWiki Slide Show
Next
Receiving commands
Receiving commands is a bit more tricky! We must:

  • Write a callback function and register it with the MQTT client
  • This function takes 2 arguments:
  • The topic
  • The payload
  • Subscribe to the topic
  • Wait forever. The callback function will be executed when a message with the
    topic we subscribed to, will arrive

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 10 of 55





















TWiki Slide Show
Next
Subscribe a callback
subscribeDemo.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 11 of 55





















TWiki Slide Show
Next
Callback result
callbackResult.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 12 of 55





















TWiki Slide Show
Next
Controlling a LED
In order to control a LED through MQTT we now simply interpret the payload:
  • Message “LED on”
  • Message “LED off”

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 13 of 55





















TWiki Slide Show
Next
Changed Callback
changedCallback.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 14 of 55





















TWiki Slide Show
Next
Missing functions in IoT
  • MQTT broker should be globally reachable
  • GUI to easily visualize sensor measurements
  • GUI elements to easily control actuators
  • GUI should be accessible through a WEB interface
  • GUI should be accessible through smart phone app
This is what Cayenne supplies

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 15 of 55





















TWiki Slide Show
Next
Cayenne functionality
  • MQTT broker is accessible at mqtt.mydevices.com

  • Supplies GUI
cayenneFunctionality.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 16 of 55





















TWiki Slide Show
Next
Signing up for Cayenne
Sign up at
https://mydevices.com/cayenne/signup

cayenneSignup.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 17 of 55





















TWiki Slide Show
Next
Cayenne Start
cayenneStart.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 18 of 55





















TWiki Slide Show
Next
Cayenne Doc
You find the documentation at

https://mydevices.com/cayenne/docs/intro

Contains short introductory video clips, information of Cayenne on Raspberry Pi and Arduino but also how to use Cayenne on any device.

Pointers to the Cayenne MQTT Libraries and their API

cayenneDoc.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 19 of 55





















TWiki Slide Show
Next
Installing Cayenne on the Raspberry PI
cayenneRPI.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 20 of 55





















TWiki Slide Show
Next
Installing Cayenne on the Raspberry Pi manually
cayenne_RPI_ManualInstallation.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 21 of 55





















TWiki Slide Show
Next
Register your Controller Single Board Computers
cayenneRegisterRPI.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 22 of 55





















TWiki Slide Show
Next
Run Cayenne on the Arduino
cayenneArduinoInstall.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 23 of 55





















TWiki Slide Show
Next
Basic Arduino Sketch for the Arduino
cayenneArduinoSketch.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 24 of 55





















TWiki Slide Show
Next
Cayenne Credentials
cayenneCredentials.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 25 of 55





















TWiki Slide Show
Next
Cayenne network layers
cayenneNetLayers.png

The Cayenne credentials are integrated into the MQTT topic string

Value and units are integrated into the payload string

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 26 of 55





















TWiki Slide Show
Next
Cayenne and Python
This falls under “bring your own thing” !

Having a look at the Arduino sketch you can see that it uses the

Arduino Cayenne C++ library.

Cayenne MQTT libraries are available in

  • C, C++
  • Python
  • Java
and probably a few other languages

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 27 of 55





















TWiki Slide Show
Next
Cayenne and Micropython
The Python Cayenne Client library depends on the Eclipse Paho MQTT library

Micropython uses its own, stripped down “umqtt” (micro mqtt) library

such that the standard Cayenne Client does not work on Micropython

but …

The Python Cayenne Client is Open Source and can be adapted to umqtt, which is what I did for you.

The adapted Client library uses the same API as the original one,

which means that the example programs work unchanged.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 28 of 55





















TWiki Slide Show
Next
The Cayenne Client
When creating a Cayenne Client Class it

  • Initializes its internal variables
  • Checks if the OLED display is connected and if yes, initializes it
  • The Cayenne Client has has a begin method which
  • Connects to WiFi
  • Connects to the Cayenne MQTT broker at mqtt.mydevices.com
  • Subscribes to commands send by Cayenne
  • Displays its internal state on the OLED display

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 29 of 55





















TWiki Slide Show
Next
The code to connect to Cayenne
cayenneConnectCode.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 30 of 55





















TWiki Slide Show
Next
Connecting to Cayenne
cayenneConnect.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 31 of 55





















TWiki Slide Show
Next
The empty Cayenne dashboard
cayenneEmptyDashboard.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 32 of 55





















TWiki Slide Show
Next
Send dummy measurement
cayenneDummyTemp.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 33 of 55





















TWiki Slide Show
Next
Dummy temperature in Cayenne
dummyTemp.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 34 of 55





















TWiki Slide Show
Next
Dummy temperature parameters
dummyTempPars.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 35 of 55





















TWiki Slide Show
Next
Fix widget to dashboard
dummyTempFixed.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 36 of 55





















TWiki Slide Show
Next
Dummy temperature history
Cayenne keeps measurement values which can be plotted as a history plot

or downloaded for further evaluation

dummyTempHistory.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 37 of 55





















TWiki Slide Show
Next
A custom widget
cayenneCustonWidget.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 38 of 55





















TWiki Slide Show
Next
A gauge widget
cayenneTempGauge.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 39 of 55





















TWiki Slide Show
Next
Dummy temperature gauge on the dashboard
cayenneWarm.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 40 of 55





















TWiki Slide Show
Next
Gauge value ranges
cayenneGaugeRanges.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 41 of 55





















TWiki Slide Show
Next
It's hot!
cayenneHot.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 42 of 55





















TWiki Slide Show
Next
Several dummy measurements
Now we want to send a dummy temperature and a dummy humidity value.

How do we distinguish?

They use different communication channels!

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 43 of 55





















TWiki Slide Show
Next
Sending temperature and humidity: the code
cayenneTempHum.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 44 of 55





















TWiki Slide Show
Next
Dashboard with temperature and humidity
cayenneDashboardTempHum.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 45 of 55





















TWiki Slide Show
Next
Other measurements
We have seen that we can send temperature with

  • client.celsiusWrite(channel,temp)
And relative humidity with

  • Client.humidityWrite(channel,hum)
Which other types of measurements can we send?

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 46 of 55





















TWiki Slide Show
Next
Measurements implemented in the Cayenne Client
cayenneMQTTCalls.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 47 of 55





















TWiki Slide Show
Next
How to control a device?
Cayenne supplies only 2 control widgets:

  • a push button
  • A slider

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 48 of 55





















TWiki Slide Show
Next
Cayenne Slider
cayenneDummySlider.png

We can define the range for the slider in the above case: 0..255

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 49 of 55





















TWiki Slide Show
Next
Slider on the dashboard
cayenneDashSlider.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 50 of 55





















TWiki Slide Show
Next
A Cayenne Button
cayenneDummyButtton.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 51 of 55





















TWiki Slide Show
Next
Dashboard with button
cayenneDashButton.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 52 of 55





















TWiki Slide Show
Next
How to receive commands
We must define a callback function, which is called

whenever the user of the Cayenne dashboard

changes a setting on the push button or slider

The callback takes a message parameter which is a tuple of

  • Cayenne topic
  • Cayenne payload

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 53 of 55





















TWiki Slide Show
Next
Cayenne command message
cayenneCmdMsg.png

Please compare the command message with our credentials and try to find out

to which channel this command has been addressed. Can you find the values as well?

Remember: we defined the push button on channel 3 and the slider in channel 2!

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 54 of 55





















TWiki Slide Show
Next
Exercises!
More on how to interpret this message in the next session

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 55 of 55





















First slide Previous End Presentation






























-- Uli Raich - 2019-05-14

Comments

I Attachment History Action Size Date Who Comment
Unknown file formatodp afnog19-ws-session3.odp r1 manage 5245.0 K 2019-05-15 - 19:13 UliRaich  
PNGpng callbackResult.png r1 manage 142.7 K 2019-05-22 - 12:31 UliRaich  
PNGpng cayenneArduinoInstall.png r1 manage 128.4 K 2019-05-22 - 16:32 UliRaich  
PNGpng cayenneArduinoSketch.png r1 manage 191.2 K 2019-05-22 - 16:35 UliRaich  
PNGpng cayenneCmdMsg.png r1 manage 189.5 K 2019-05-22 - 19:15 UliRaich  
PNGpng cayenneCmdMsg2.png r1 manage 68.6 K 2019-05-22 - 19:15 UliRaich  
PNGpng cayenneConnect.png r1 manage 137.9 K 2019-05-22 - 16:52 UliRaich  
PNGpng cayenneConnectCode.png r1 manage 56.8 K 2019-05-22 - 16:52 UliRaich  
PNGpng cayenneCredentials.png r1 manage 109.7 K 2019-05-22 - 16:36 UliRaich  
PNGpng cayenneCustonWidget.png r1 manage 184.8 K 2019-05-22 - 19:26 UliRaich  
PNGpng cayenneDashButton.png r1 manage 156.5 K 2019-05-22 - 19:03 UliRaich  
PNGpng cayenneDashSlider.png r1 manage 139.6 K 2019-05-22 - 19:03 UliRaich  
PNGpng cayenneDashboardTempHum.png r1 manage 119.5 K 2019-05-22 - 18:56 UliRaich  
PNGpng cayenneDoc.png r1 manage 158.3 K 2019-05-22 - 16:21 UliRaich  
PNGpng cayenneDummyButtton.png r1 manage 211.0 K 2019-05-22 - 19:04 UliRaich  
PNGpng cayenneDummySlider.png r1 manage 178.5 K 2019-05-22 - 19:04 UliRaich  
PNGpng cayenneDummyTemp.png r1 manage 71.1 K 2019-05-22 - 18:32 UliRaich  
PNGpng cayenneEmptyDashboard.png r1 manage 150.2 K 2019-05-22 - 16:52 UliRaich  
PNGpng cayenneFunctionality.png r1 manage 135.7 K 2019-05-22 - 16:09 UliRaich  
PNGpng cayenneGaugeRanges.png r1 manage 155.2 K 2019-05-22 - 18:48 UliRaich  
PNGpng cayenneHot.png r1 manage 147.2 K 2019-05-22 - 18:32 UliRaich  
PNGpng cayenneMQTTCalls.png r1 manage 67.0 K 2019-05-22 - 18:56 UliRaich  
PNGpng cayenneNetLayers.png r1 manage 34.5 K 2019-05-22 - 16:45 UliRaich  
PNGpng cayenneRPI.png r1 manage 150.8 K 2019-05-22 - 16:24 UliRaich  
PNGpng cayenneRegisterRPI.png r1 manage 148.3 K 2019-05-22 - 16:24 UliRaich  
PNGpng cayenneSignup.png r1 manage 122.2 K 2019-05-22 - 16:15 UliRaich  
PNGpng cayenneStart.png r1 manage 146.3 K 2019-05-22 - 16:15 UliRaich  
PNGpng cayenneTempGauge.png r1 manage 205.1 K 2019-05-22 - 18:41 UliRaich  
PNGpng cayenneTempHum.png r1 manage 82.7 K 2019-05-22 - 18:52 UliRaich  
PNGpng cayenneWarm.png r1 manage 149.6 K 2019-05-22 - 18:44 UliRaich  
PNGpng cayenne_RPI_ManualInstallation.png r1 manage 189.9 K 2019-05-22 - 16:30 UliRaich  
PNGpng changedCallback.png r1 manage 15.3 K 2019-05-22 - 16:06 UliRaich  
PNGpng dummyTemp.png r1 manage 110.2 K 2019-05-22 - 18:32 UliRaich  
PNGpng dummyTempFixed.png r1 manage 126.3 K 2019-05-22 - 18:32 UliRaich  
PNGpng dummyTempHistory.png r1 manage 29.5 K 2019-05-22 - 18:32 UliRaich  
PNGpng dummyTempPars.png r1 manage 137.8 K 2019-05-22 - 18:37 UliRaich  
PNGpng micropython-lib.png r1 manage 75.2 K 2019-05-14 - 17:31 UliRaich  
PNGpng mqttDemo.png r1 manage 191.3 K 2019-05-14 - 17:23 UliRaich  
PNGpng mqttDummySensorDemo.png r1 manage 74.4 K 2019-05-20 - 18:40 UliRaich  
PNGpng mqttExample.png r2 r1 manage 73.2 K 2019-05-15 - 16:12 UliRaich  
PNGpng mqttPostOfffice.png r1 manage 50.2 K 2019-05-14 - 17:18 UliRaich  
PNGpng ourCredentials.png r1 manage 17.9 K 2019-05-22 - 19:15 UliRaich  
PNGpng realData.png r1 manage 27.7 K 2019-05-15 - 17:12 UliRaich  
PNGpng subscribeDemo.png r1 manage 78.3 K 2019-05-20 - 18:45 UliRaich  
PNGpng umqttDemo.png r2 r1 manage 72.2 K 2019-05-14 - 17:38 UliRaich  
PNGpng wifiConnect.png r1 manage 128.4 K 2019-05-14 - 17:18 UliRaich  
PNGpng wifiDemoCode.png r1 manage 43.8 K 2019-05-15 - 16:14 UliRaich  

This topic: AFNOG > WebHome > AFNOGWorkshop2019 > AFNOG-2019Slides > WorkshopSlides > MQTTAndCayenne
Topic revision: r9 - 2019-05-22 - 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