MQTTAndCayenne

Page Actions
Start presentation

Slide 1: Setting up an IoT

Session 3: MQTT and Cayenne

Uli Raich

Formally CERN, Geneva, Switzerland

Slide 2: 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

Slide 3: Accessing the WiFi network

wifiDemoCode.png

Slide 4: Accessing the WiFi network from Micropython

wifiConnect.png

Slide 5: 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 \x93topic\x94 and/or it can push its

results on a certain topic

mqttPostOfffice.png

Slide 6: MQTT Demo

mqttDemo.png

Slide 7: The Micropython library

micropython-lib.png

Slide 8: A MQTT example using umqtt.simple

umqttDemo.png

Slide 9: Sending dummy temperature data

mqttDummySensorDemo.png

Slide 10: 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

Slide 11: Subscribe a callback

subscribeDemo.png

Slide 12: Callback result

callbackResult.png

Slide 13: Controlling a LED

In order to control a LED through MQTT we now simply interpret the payload:
  • Message \x93LED on\x94
  • Message \x93LED off\x94

Slide 14: Changed Callback

changedCallback.png

Slide 15: 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

Slide 16: Cayenne functionality

  • MQTT broker is accessible at mqtt.mydevices.com

  • Supplies GUI
cayenneFunctionality.png

Slide 17: Signing up for Cayenne

Sign up at
https://mydevices.com/cayenne/signup

cayenneSignup.png

Slide 18: Cayenne Start

cayenneStart.png

Slide 19: 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

Slide 20: Installing Cayenne on the Raspberry PI

cayenneRPI.png

Slide 21: Installing Cayenne on the Raspberry Pi manually

cayenne_RPI_ManualInstallation.png

Slide 22: Register your Controller Single Board Computers

cayenneRegisterRPI.png

Slide 23: Run Cayenne on the Arduino

cayenneArduinoInstall.png

Slide 24: Basic Arduino Sketch for the Arduino

cayenneArduinoSketch.png

Slide 25: Cayenne Credentials

cayenneCredentials.png

Slide 26: Cayenne network layers

cayenneNetLayers.png

The Cayenne credentials are integrated into the MQTT topic string

Value and units are integrated into the payload string

Slide 27: Cayenne and Python

This falls under \x93bring your own thing\x94 !

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

Slide 28: Cayenne and Micropython

The Python Cayenne Client library depends on the Eclipse Paho MQTT library

Micropython uses its own, stripped down \x93umqtt\x94 (micro mqtt) library

such that the standard Cayenne Client does not work on Micropython

but \x85

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.

Slide 29: 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

Slide 30: The code to connect to Cayenne

cayenneConnectCode.png

Slide 31: Connecting to Cayenne

cayenneConnect.png

Slide 32: The empty Cayenne dashboard

cayenneEmptyDashboard.png

Slide 33: Send dummy measurement

cayenneDummyTemp.png

Slide 34: Dummy temperature in Cayenne

dummyTemp.png

Slide 35: Dummy temperature parameters

dummyTempPars.png

Slide 36: Fix widget to dashboard

dummyTempFixed.png

Slide 37: Dummy temperature history

Cayenne keeps measurement values which can be plotted as a history plot

or downloaded for further evaluation

dummyTempHistory.png

Slide 38: A custom widget

cayenneCustonWidget.png

Slide 39: A gauge widget

cayenneTempGauge.png

Slide 40: Dummy temperature gauge on the dashboard

cayenneWarm.png

Slide 41: Gauge value ranges

cayenneGaugeRanges.png

Slide 42: It's hot!

cayenneHot.png

Slide 43: 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!

Slide 44: Sending temperature and humidity: the code

cayenneTempHum.png

Slide 45: Dashboard with temperature and humidity

cayenneDashboardTempHum.png

Slide 46: 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?

Slide 47: Measurements implemented in the Cayenne Client

cayenneMQTTCalls.png

Slide 48: How to control a device?

Cayenne supplies only 2 control widgets:

  • a push button
  • A slider

Slide 49: Cayenne Slider

cayenneDummySlider.png

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

Slide 50: Slider on the dashboard

cayenneDashSlider.png

Slide 51: A Cayenne Button

cayenneDummyButtton.png

Slide 52: Dashboard with button

cayenneDashButton.png

Slide 53: 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

Slide 54: 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!

Slide 55: Exercises!

More on how to interpret this message in the next session

-- %USERSIG{UliRaich - 2019-05-14}%

Comments

 
I Attachment Action Size Date Who Comment
afnog19-ws-session3.odpodp afnog19-ws-session3.odp manage 5 MB 15 May 2019 - 19:13 UnknownUser  
callbackResult.pngpng callbackResult.png manage 142 K 22 May 2019 - 12:31 UnknownUser  
cayenneArduinoInstall.pngpng cayenneArduinoInstall.png manage 128 K 22 May 2019 - 16:32 UnknownUser  
cayenneArduinoSketch.pngpng cayenneArduinoSketch.png manage 191 K 22 May 2019 - 16:35 UnknownUser  
cayenneCmdMsg.pngpng cayenneCmdMsg.png manage 189 K 22 May 2019 - 19:15 UnknownUser  
cayenneCmdMsg2.pngpng cayenneCmdMsg2.png manage 68 K 22 May 2019 - 19:15 UnknownUser  
cayenneConnect.pngpng cayenneConnect.png manage 137 K 22 May 2019 - 16:52 UnknownUser  
cayenneConnectCode.pngpng cayenneConnectCode.png manage 56 K 22 May 2019 - 16:52 UnknownUser  
cayenneCredentials.pngpng cayenneCredentials.png manage 109 K 22 May 2019 - 16:36 UnknownUser  
cayenneCustonWidget.pngpng cayenneCustonWidget.png manage 184 K 22 May 2019 - 19:26 UnknownUser  
cayenneDashButton.pngpng cayenneDashButton.png manage 156 K 22 May 2019 - 19:03 UnknownUser  
cayenneDashSlider.pngpng cayenneDashSlider.png manage 139 K 22 May 2019 - 19:03 UnknownUser  
cayenneDashboardTempHum.pngpng cayenneDashboardTempHum.png manage 119 K 22 May 2019 - 18:56 UnknownUser  
cayenneDoc.pngpng cayenneDoc.png manage 158 K 22 May 2019 - 16:21 UnknownUser  
cayenneDummyButtton.pngpng cayenneDummyButtton.png manage 211 K 22 May 2019 - 19:04 UnknownUser  
cayenneDummySlider.pngpng cayenneDummySlider.png manage 178 K 22 May 2019 - 19:04 UnknownUser  
cayenneDummyTemp.pngpng cayenneDummyTemp.png manage 71 K 22 May 2019 - 18:32 UnknownUser  
cayenneEmptyDashboard.pngpng cayenneEmptyDashboard.png manage 150 K 22 May 2019 - 16:52 UnknownUser  
cayenneFunctionality.pngpng cayenneFunctionality.png manage 135 K 22 May 2019 - 16:09 UnknownUser  
cayenneGaugeRanges.pngpng cayenneGaugeRanges.png manage 155 K 22 May 2019 - 18:48 UnknownUser  
cayenneHot.pngpng cayenneHot.png manage 147 K 22 May 2019 - 18:32 UnknownUser  
cayenneMQTTCalls.pngpng cayenneMQTTCalls.png manage 66 K 22 May 2019 - 18:56 UnknownUser  
cayenneNetLayers.pngpng cayenneNetLayers.png manage 34 K 22 May 2019 - 16:45 UnknownUser  
cayenneRPI.pngpng cayenneRPI.png manage 150 K 22 May 2019 - 16:24 UnknownUser  
cayenneRegisterRPI.pngpng cayenneRegisterRPI.png manage 148 K 22 May 2019 - 16:24 UnknownUser  
cayenneSignup.pngpng cayenneSignup.png manage 122 K 22 May 2019 - 16:15 UnknownUser  
cayenneStart.pngpng cayenneStart.png manage 146 K 22 May 2019 - 16:15 UnknownUser  
cayenneTempGauge.pngpng cayenneTempGauge.png manage 205 K 22 May 2019 - 18:41 UnknownUser  
cayenneTempHum.pngpng cayenneTempHum.png manage 82 K 22 May 2019 - 18:52 UnknownUser  
cayenneWarm.pngpng cayenneWarm.png manage 149 K 22 May 2019 - 18:44 UnknownUser  
cayenne_RPI_ManualInstallation.pngpng cayenne_RPI_ManualInstallation.png manage 189 K 22 May 2019 - 16:30 UnknownUser  
changedCallback.pngpng changedCallback.png manage 15 K 22 May 2019 - 16:06 UnknownUser  
dummyTemp.pngpng dummyTemp.png manage 110 K 22 May 2019 - 18:32 UnknownUser  
dummyTempFixed.pngpng dummyTempFixed.png manage 126 K 22 May 2019 - 18:32 UnknownUser  
dummyTempHistory.pngpng dummyTempHistory.png manage 29 K 22 May 2019 - 18:32 UnknownUser  
dummyTempPars.pngpng dummyTempPars.png manage 137 K 22 May 2019 - 18:37 UnknownUser  
micropython-lib.pngpng micropython-lib.png manage 75 K 14 May 2019 - 17:31 UnknownUser  
mqttDemo.pngpng mqttDemo.png manage 191 K 14 May 2019 - 17:23 UnknownUser  
mqttDummySensorDemo.pngpng mqttDummySensorDemo.png manage 74 K 20 May 2019 - 18:40 UnknownUser  
mqttExample.pngpng mqttExample.png manage 73 K 15 May 2019 - 16:12 UnknownUser  
mqttPostOfffice.pngpng mqttPostOfffice.png manage 50 K 14 May 2019 - 17:18 UnknownUser  
ourCredentials.pngpng ourCredentials.png manage 17 K 22 May 2019 - 19:15 UnknownUser  
realData.pngpng realData.png manage 27 K 15 May 2019 - 17:12 UnknownUser  
subscribeDemo.pngpng subscribeDemo.png manage 78 K 20 May 2019 - 18:45 UnknownUser  
umqttDemo.pngpng umqttDemo.png manage 72 K 14 May 2019 - 17:38 UnknownUser  
wifiConnect.pngpng wifiConnect.png manage 128 K 14 May 2019 - 17:18 UnknownUser  
wifiDemoCode.pngpng wifiDemoCode.png manage 43 K 15 May 2019 - 16:14 UnknownUser  
Topic revision: r9 - 22 May 2019, UliRaich