Tags:
create new tag
view all tags

Connecting to Cayenne

A Micropython class connecting to WiFi and subscribing to the myDevices MQTT broker

Since every sensor node needs to first connect to WiFi and later subscribe to the MQTT broker at mqtt.mydevices.com. Unfortunately the
Cayenne-MQTT-Python library depends on the Eclipse Paho MQTT Python client library, which is not available in Micropython.

I therefore ported the code to the umqtt.simple MQTT client available in Micropython. The API is the same as in the standard Cayenne MQTT client, which means that the example programs provided in this library will work without modification.

In order to connect to myDevices Cayenne you need:

  • your WiFi SSID
  • your WiFi password
  • the Cayenne credentials:
    • client name
    • user name
    • password
The CayenneMQTTClient class has a begin method taking Cayenne username, password and client id as parameters. The ssid and wifiPassword have default values but can be specified as well. The begin method will first connect to WiFi and then connect to the MQTT broker at mqtt.mydevices.com. It then subscribes to the command topic such that cmd messages from Cayenne can be passed on to a callback routine registered by the user of the CayenneMQTTClient class. In case of a cmd message, the required response message is sent automatically.

If the OLED display is connected, the state of network connection will be shown.

The CayenneMQTTClient provides the following methods to publish measurement results:

For temperature:

  • celsiusWrite(channel,value)
  • fahrenheitWrite(channel,value)
  • kelvinWrite(channel,value)
For other measurements, I think that from the name of the method it is clear which type of measurement is treated:
  • humidityWrite(channel,vakue)
  • luxWrite(channel,value)
  • pascalWrite(channel,value)
  • hectoPascalWrite(channel,value)
  • voltageWrite(channel,value), the value must be given in mV
  • digitalWrite(channel,value), value must be 0 or 1

Parsing of command messages

The callback function gets a message, a tuple consisting of the topic and the payload, as parameter. This message must be parsed to extract the channel number and the value. The CayenneMessage class helps with this. Create a CayenneMessage object cayenneMsg with topic and payload (message[0] and message[1]) as corresponding parameters.

cayenneMsg.channel will then contain the channel number on which the command must be executed, cayenneMsg.value contains the value.

You can also get hold of

  • cayenneMsg.client_id
  • cayenneMsg.msg_id
should these be needed.

The code of the CayenneMQTTClient class can be found here:

https://github.com/uraich/MicropythonCayenneMQTTClient/tree/master/cayenne

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
Texttxt cayenneDS18B20.py.txt r1 manage 1.0 K 2019-03-28 - 12:42 UliRaich  
Texttxt cayennePublic.py.txt r1 manage 2.7 K 2019-03-28 - 12:42 UliRaich  
Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r3 - 2019-05-28 - 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