Session 3: MQTT and Cayenne
Uli Raich
Formally CERN, Geneva, Switzerland
COPYRIGHT © 2025 by the contributing authors
Slide 1 of 32
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
COPYRIGHT © 2025 by the contributing authors
Slide 2 of 32
Accessing the WiFi network
COPYRIGHT © 2025 by the contributing authors
Slide 3 of 32
Accessing the WiFi network from Micropython
COPYRIGHT © 2025 by the contributing authors
Slide 4 of 32
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
COPYRIGHT © 2025 by the contributing authors
Slide 5 of 32
COPYRIGHT © 2025 by the contributing authors
Slide 6 of 32
COPYRIGHT © 2025 by the contributing authors
Slide 7 of 32
A MQTT example using umqtt.simple
COPYRIGHT © 2025 by the contributing authors
Slide 8 of 32
Sending dummy temperature data
COPYRIGHT © 2025 by the contributing authors
Slide 9 of 32
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
COPYRIGHT © 2025 by the contributing authors
Slide 10 of 32
COPYRIGHT © 2025 by the contributing authors
Slide 11 of 32
COPYRIGHT © 2025 by the contributing authors
Slide 12 of 32
In order to control a LED through MQTT we now simply interpret the payload:
- Message “LED on”
- Message “LED off”
COPYRIGHT © 2025 by the contributing authors
Slide 13 of 32
COPYRIGHT © 2025 by the contributing authors
Slide 14 of 32
- 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
COPYRIGHT © 2025 by the contributing authors
Slide 15 of 32
- MQTT broker is accessible at mqtt.mydevices.com
- Supplies GUI
COPYRIGHT © 2025 by the contributing authors
Slide 16 of 32
COPYRIGHT © 2025 by the contributing authors
Slide 17 of 32
COPYRIGHT © 2025 by the contributing authors
Slide 18 of 32
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
COPYRIGHT © 2025 by the contributing authors
Slide 19 of 32
Installing Cayenne on the Raspberry PI
COPYRIGHT © 2025 by the contributing authors
Slide 20 of 32
Installing Cayenne on the Raspberry Pi manually
COPYRIGHT © 2025 by the contributing authors
Slide 21 of 32
Register your Controller Single Board Computers
COPYRIGHT © 2025 by the contributing authors
Slide 22 of 32
Run Cayenne on the Arduino
COPYRIGHT © 2025 by the contributing authors
Slide 23 of 32
Basic Arduino Sketch for the Arduino
COPYRIGHT © 2025 by the contributing authors
Slide 24 of 32
COPYRIGHT © 2025 by the contributing authors
Slide 25 of 32
The Cayenne credentials are integrated into the MQTT topic string
Value and units are integrated into the payload string
COPYRIGHT © 2025 by the contributing authors
Slide 26 of 32
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
and probably a few other languages
COPYRIGHT © 2025 by the contributing authors
Slide 27 of 32
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.
COPYRIGHT © 2025 by the contributing authors
Slide 28 of 32
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
COPYRIGHT © 2025 by the contributing authors
Slide 29 of 32
The code to connect to Cayenne
COPYRIGHT © 2025 by the contributing authors
Slide 30 of 32
COPYRIGHT © 2025 by the contributing authors
Slide 31 of 32
The empty Cayenne dashboard
COPYRIGHT © 2025 by the contributing authors
Slide 32 of 32
--
Uli Raich - 2019-05-14
Comments
This topic: AFNOG
> WebHome >
AFNOGWorkshop2019 >
AFNOG-2019Slides >
WorkshopSlides > MQTTAndCayenne
Topic revision: r8 - 2019-05-22 - UliRaich

Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback