End Presentation


TWiki Slide Show
Next
Setting up and IoT

Session 4: Sending real measurement data to Cayenne and

receiving commands and executing them on the hardware

Uli Raich

formally CERN, Geneva, Switzerland

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





















TWiki Slide Show
Next
The Cayenne MQTT protocol
In the last lecture we have seen the command message sent by Cayenne:
cayenneCmdMsg2.png cayenneCredentialsCmp.png
At first glance the Cayenne message looks pretty unreadable but comparing it to the Cayenne credentials gives us some insight

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





















TWiki Slide Show
Next
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

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





















TWiki Slide Show
Next
Starting MQTTfx
mqttfxBaseWindow.png

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





















TWiki Slide Show
Next
Setting up the profile
mqttfxCredentials.png

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





















TWiki Slide Show
Next
Copy Cayenne credentials
mqttfxcredFromCayenne.png

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





















TWiki Slide Show
Next
Connecting to Cayenne
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

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





















TWiki Slide Show
Next
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”.

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





















TWiki Slide Show
Next
The payload
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

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





















TWiki Slide Show
Next
Publish the data message
mqttfxPublish.png

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





















TWiki Slide Show
Next
MQTTfx data seen in Cayenne
mqttfxPubCayenne.png

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





















TWiki Slide Show
Next
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

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





















TWiki Slide Show
Next
Subscribing in MQTTfx
mqttfxSubscribe.png

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





















TWiki Slide Show
Next
Clicking the button
mqttfxCmd.png

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





















TWiki Slide Show
Next
Cayenne is waiting for a response
mqttfxWaiting.png

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





















TWiki Slide Show
Next
Treating the command
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

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





















TWiki Slide Show
Next
Sending the response
mqttfxResponse.png

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





















TWiki Slide Show
Next
The switch shows “on”
mqttfxButtonOn.png

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





















TWiki Slide Show
Next
The Cayenne MQTT client
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

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





















TWiki Slide Show
Next
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

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





















TWiki Slide Show
Next
Measurement types
A big number of data types is defined in the Cayenne MQTT protocol
Have a look at the docs under”Supported data types”:

https://mydevices.com/cayenne/docs/cayenne-mqtt-api/#cayenne-mqtt-api-mqtt-messaging-topics

Only the most basic ones are implemented in our client:

cayenneDataTypes.png

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





















TWiki Slide Show
Next
Measurement units
The same is true for the units, here are the ones defined in the client:

cayenneUnits.png

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





















TWiki Slide Show
Next
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.

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





















TWiki Slide Show
Next
SHT30 on Cayenne(1)
cayenneSHT30Publish1.png

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





















TWiki Slide Show
Next
SHT30 on Cayenne(2)
CayenneSHT30Publish.png

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





















TWiki Slide Show
Next
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

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





















TWiki Slide Show
Next
Registering the callback
cayenneRegCallback.png

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





















TWiki Slide Show
Next
Parsing the command message
cayenneCallback.png

The callback gets a tuple with topic and payload as parameter
The CayenneMessage class helps with parsing the message

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





















TWiki Slide Show
Next
The CayenneMessage class
cayenneMsgClass.png

cayenneMsg.png

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





















TWiki Slide Show
Next
Analogue to Digital Conversion
Often real world signals are analogue in nature.

Before treating them digitally we must convert them to digital values

This is done by an Analogue to Digital Converter

The ESP8266 has a 10 bit ADC on chip, the ESP32 has 3 12 bit ADCs

To demonstrate this we use a photo resistor reading the light intensity.

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





















TWiki Slide Show
Next
Photo resistor and LED
The photo resistor and the 1 kΩ resistor form a voltage divider

The LED can be switched on by program and changes the light

Intensity seen by the photo resistor
photoresistorSchema.png proto.png

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





















TWiki Slide Show
Next
ADC readout

The ADC and GPIO driver classes

are already available in MicroPython.

This makes readout a child’s game.

protoDriver.png

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





















TWiki Slide Show
Next
Prototype board on Cayenne
cayenneProtoCode.png

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





















TWiki Slide Show
Next
The prototype GUI
cayenneProtoResult.png
First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 34 of 42





















TWiki Slide Show
Next

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





















TWiki Slide Show
Next
History of Intensity Measurements
cayenneProtoHistory.png

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





















TWiki Slide Show
Next
A trigger in Cayenne
We will observe the light intensity a and switch a light on, if it gets too dark

cayenneTrigger.png

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





















TWiki Slide Show
Next
The trigger code
triggerCode.png

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





















TWiki Slide Show
Next
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.

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





















TWiki Slide Show
Next
The Buzzer GUI
QtBuzzer.png

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





















TWiki Slide Show
Next
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

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





















TWiki Slide Show
Next
The end of the show
There is still work for you during the exercises session to

make you IoT node work with Sensor readout

Publishing the result on Cayenne (for the SHT30)

Subscribe to a command message from the Cayenne slider

to modify the settings of a colour component of your WS2812 rgb LED

For further study please consult:

https://afnog.iotworkshop.africa/do/view/AFNOG/AFNOGWorkshop2019

You will find plenty examples on

https://github.com/uraich?tab=repositories

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





















First slide Previous End Presentation






























-- Uli Raich - 2019-05-15

Comments

I Attachment History Action Size Date Who Comment
PNGpng CayenneSHT30Publish.png r1 manage 67.3 K 2019-05-24 - 08:11 UliRaich  
PNGpng QtBuzzer.png r1 manage 22.8 K 2019-05-24 - 08:15 UliRaich  
PNGpng cayenneCallback.png r1 manage 37.0 K 2019-06-08 - 16:21 UliRaich  
PNGpng cayenneCmdMsg2.png r1 manage 68.6 K 2019-05-24 - 07:38 UliRaich  
PNGpng cayenneCredentialsCmp.png r1 manage 30.6 K 2019-06-08 - 16:11 UliRaich  
PNGpng cayenneDataTypes.png r1 manage 33.1 K 2019-05-24 - 08:08 UliRaich  
PNGpng cayenneMsg.png r1 manage 9.6 K 2019-06-09 - 13:57 UliRaich  
PNGpng cayenneMsgClass.png r1 manage 59.5 K 2019-06-09 - 12:25 UliRaich  
PNGpng cayenneProtoCode.png r1 manage 68.0 K 2019-06-09 - 14:07 UliRaich  
PNGpng cayenneProtoHistory.png r1 manage 114.5 K 2019-06-09 - 12:28 UliRaich  
PNGpng cayenneProtoResult.png r1 manage 115.8 K 2019-06-09 - 12:28 UliRaich  
PNGpng cayenneRegCallback.png r1 manage 26.2 K 2019-06-08 - 16:21 UliRaich  
PNGpng cayenneSHT30Publish1.png r1 manage 82.0 K 2019-05-24 - 08:11 UliRaich  
PNGpng cayenneTrigger.png r1 manage 126.0 K 2019-06-09 - 12:30 UliRaich  
PNGpng cayenneUnits.png r1 manage 24.3 K 2019-05-24 - 08:08 UliRaich  
PNGpng mqttfxBaseWindow.png r1 manage 27.5 K 2019-05-24 - 07:38 UliRaich  
PNGpng mqttfxButtonOn.png r1 manage 140.6 K 2019-05-24 - 07:38 UliRaich  
PNGpng mqttfxCmd.png r1 manage 90.3 K 2019-05-24 - 07:38 UliRaich  
PNGpng mqttfxCredentials.png r1 manage 60.2 K 2019-05-24 - 07:45 UliRaich  
PNGpng mqttfxPubCayenne.png r1 manage 152.2 K 2019-05-24 - 07:53 UliRaich  
PNGpng mqttfxPublish.png r1 manage 37.2 K 2019-05-24 - 07:39 UliRaich  
PNGpng mqttfxResponse.png r1 manage 32.5 K 2019-05-24 - 07:39 UliRaich  
PNGpng mqttfxSubscribe.png r1 manage 54.6 K 2019-05-24 - 07:39 UliRaich  
PNGpng mqttfxWaiting.png r1 manage 143.4 K 2019-05-24 - 07:58 UliRaich  
PNGpng mqttfxcredFromCayenne.png r1 manage 155.7 K 2019-05-24 - 07:45 UliRaich  
PNGpng photoresistorSchema.png r1 manage 18.2 K 2019-06-09 - 12:25 UliRaich  
PNGpng proto.png r1 manage 114.2 K 2019-06-09 - 12:25 UliRaich  
PNGpng protoDriver.png r1 manage 31.4 K 2019-06-09 - 12:25 UliRaich  
PNGpng triggerCode.png r1 manage 104.8 K 2019-06-09 - 14:15 UliRaich  

This topic: AFNOG > WebHome > AFNOGWorkshop2019 > AFNOG-2019Slides > WorkshopSlides > PublishAndSubscribe
Topic revision: r5 - 2019-06-09 - 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