Session 4: Sending real measurement data to Cayenne and
receiving commands and executing them on the hardware
Uli Raich
formally CERN, Geneva, Switzerland
COPYRIGHT © 2024 by the contributing authors
Slide 1 of 32
The Cayenne MQTT protocol
In the last lecture we have seen the command message sent by Cayenne:
At first glance the Cayenne message looks pretty unreadable but comparing it to the Cayenne credentials gives us some insight
COPYRIGHT © 2024 by the contributing authors
Slide 2 of 32
Trying the protocol with MQTTfx
The MQTTfx client allows you to
- manually configure a Cayenne client creating a connection profile.
This profile contains the Cayenne credentials
- Cayenne user name
- Cayenne password
- Cayenne client_id
- connect to the Cayenne MQTT broker at mqtt.mydevices.com
- publish messages in Cayenne MQTT format
- Subscribe to and receive Cayenne cmd messages and answer them
COPYRIGHT © 2024 by the contributing authors
Slide 3 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 4 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 5 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 6 of 32
Once the profile is created you can connect to Cayenne with the “connect” button.
You should see the “offline” note in on your Cayenne device page go away
Once connected we can start sending messages to Cayenne
COPYRIGHT © 2024 by the contributing authors
Slide 7 of 32
Publish a message on Cayenne
Cayenne MQTT expects topics of the following form:
v1/username/things/clientID/data/channel
This tells Cayenne that “data” are going to be sent.
Replace “username” with your Cayenne user name and
“clientID” with the client ID of your device
We will send dummy temperature data to channel 0.
Therefore “channel” must be replaced by “0”.
COPYRIGHT © 2024 by the contributing authors
Slide 8 of 32
The payload (the actual data) has the following form:
type,unit=value
In our case:
Type (temperature): temp
Unit (Celsius): c
Value: 29.3 (typical Ugandan temperature)
Our payload therefore becomes:
Temp,c=29.3
COPYRIGHT © 2024 by the contributing authors
Slide 9 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 10 of 32
MQTTfx data seen in Cayenne
COPYRIGHT © 2024 by the contributing authors
Slide 11 of 32
Subscribing to cmd messages
In order to see the command messages e.g. those
sent by the push button we must first subscribe to them.
The format of a subscription is:
v1/username/things/clientID/cmd/channel
Since our push button is connected to channel 3
this is what we define as channel
.
v1/88253c70-76e9-11e9-9636-f9904f7b864b/things/
1ce44ea0-76ea-11e9-b4eb-6bf2c2412b24/cmd/3
COPYRIGHT © 2024 by the contributing authors
Slide 12 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 13 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 14 of 32
Cayenne is waiting for a response
COPYRIGHT © 2024 by the contributing authors
Slide 15 of 32
Normally we would treat the command by acting on the hardware.
If this was successful we would inform Cayenne that the new value was set:
Format:
v1/88253c70-76e9-11e9-9636-f9904f7b864b/things/
1ce44ea0-76ea-11e9-b4eb-6bf2c2412b24/data/3
and the topic would be the new value: in this case: 1
COPYRIGHT © 2024 by the contributing authors
Slide 16 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 17 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 18 of 32
With this information we understand how the Cayenne MQTT client works:
- In its begin method it gets the Cayenne credentials and saves them
- It sets up a root topic string
self.rootTopic = "v1/%s/things/%s" % (username, clientid)
- It connects to WiFi and then to the Cayenne MQTT
broker using the tcp protocol
- It subscribes to the cmd topic
COPYRIGHT © 2024 by the contributing authors
Slide 19 of 32
Cayenne MQTT client publish
When you publish measurement data with
- celsiusWrite(channel,value)
- humidityWrite(channel,value)
- hectrPascalWrite(channel,value)
- etc.
The root topic string is completed to a full data topic including the channel information
The corresponding data type and the unit (this information is hard coded into the call:
for celsiusWrite the data type is “temp” and the unit is “c”)
as well as the value as used to create the payload string
This information is published
COPYRIGHT © 2024 by the contributing authors
Slide 20 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 21 of 32
The same is true for the units, here are the ones defined in the client:
COPYRIGHT © 2024 by the contributing authors
Slide 22 of 32
Combining measurement and publishing
In session 2 we have seen how to readout sensors and control actuators
Now we have seen how to publish and how to subscribe to Cayenne messages
We have to combine those two in order to create a full cayenne IoT node.
COPYRIGHT © 2024 by the contributing authors
Slide 23 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 24 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 25 of 32
Cayenne MQTT client subscribe
This is a little more tricky:
We have to create a callback function
and register it with the Cayenne MQTT client
When the push button or slider on Cayenne
are activated this function will be called
The function must find out from which channel
the request is coming, and which are the data
It must then act on the hardware correspondingly
The Cayenne MQTT client sends a response message
without our intervention
COPYRIGHT © 2024 by the contributing authors
Slide 26 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 27 of 32
Parsing the command message
The callback gets a tuple with topic and payload as parameter
The
CayenneMessage class helps with parsing the message
COPYRIGHT © 2024 by the contributing authors
Slide 28 of 32
A Qt app and Cayenne MQTT
The graphical user interface of Cayenne is fairly limited.
Can we write an application with another GUI system interacting
with our devices connected to the Cayenne MQTT broker?
Example:
The WeMos D1 buzzer.
We want to play a song from a list of choices.
COPYRIGHT © 2024 by the contributing authors
Slide 29 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 30 of 32
The Cayenne MQTT C library
The Cayenne MQTT C library contains everything we need
to communicate with the Cayenne MQTT broker:
- Network and CayenneMQTTClient data structures defined in include files
- NetworkInit and NetworkConnect to connect to the Network
- CayenneMQTTClientIni and CayenneMQTTConnect
to connect to the Cayenne MQTT broker
- CayenneMQTTPublishDataInt to publish the song number
COPYRIGHT © 2024 by the contributing authors
Slide 31 of 32
COPYRIGHT © 2024 by the contributing authors
Slide 32 of 32
--
Uli Raich - 2019-05-15
Comments
This topic: AFNOG
> WebHome >
AFNOGWorkshop2019 >
AFNOG-2019Slides >
WorkshopSlides > PublishAndSubscribe
Topic revision: r3 - 2019-06-08 - UliRaich
Copyright © 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