Communicating over MQTT
MQTT, the
Message
Queuing
Telemetry
Transport protocol is the communication protocol of choice in many IoT applications. It uses a broker receiving subscriptions and publications, distributing messages on a certain topic to everybody subscribed to it. It is a light weight protocol running of top of TCP.
Below we see an example running the MQTT broker mosquitto. Two nodes (mosquitto_sub) have subscribed to the topic AFNOG19 and AFNOG18 respectively.. When a third node publishes information on topic AFNOG19, then the first subscriber receives the information (payload) while when publishing on AFNOG18 it is the second subscriber seeing the message.
MQTT and Cayenne
Cayenne supplies an mqtt broker at mqtt.mydevices.com which uses a strict format for the topic and the payload string. When registering at
MyDevices Cayenne as a new user you will be attributed a
username and a
password. For each device you register you will get a
device_id.
Cayenne uses these credentials in the MQTT topic to verify permission to access the MQTT broker. A typical publication topic has the form:
v1/username/things/clientID/data/channel
while the payload has the form:
type, unit=value (e.g. temp,c=24.5 to publish a temperature reading of 24.5 °C)
For the full details please refer to the
Cayenne MQTT API.
Libraries in C, C++, Python, Java can be found on github.
Unfortunately, the Cayenne MQTT Python library depends on the Eclipse Paho MQTT Python client, which is not available on Micropython. I therefore ported the Cayenne MQTT Client class to the simple.py MQTT client available in micropython. The API is the same and the example programs should work without modification.
The new Cayenne client package has already been integrated into micropython such that you can make use of its classes and methods without any further installation.The source code has nevertheless been attached to this page for your inspection. Please remove the .txt extension after download to run the programs.
https://afnog.iotworkshop.africa/pub/AFNOG/CommunicatingOverMQTT/clientPublic.py.txt
As you can observe by studying the begin method of the CayenneMQTTClient class that default values for
ssid and wifiPassword are used, which correspond to the WiFi ssid and password we had during the tutorial in the classroom. In the Cayenne example programs the begin method is called without these parameters. If you want to run these programs with your own WiFi network, you will have to pass these two parameters ssid="Your SSID" and wifiPassword="Your WiFi password" thus overriding the default.
The method call will therefore be changed from:
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO) to
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, ssid="Your SSID", wifPassword="Your Wifi paddword", loglevel=logging.INFO)
https://afnog.iotworkshop.africa/pub/AFNOG/CommunicatingOverMQTT/__init__.py.txt
https://afnog.iotworkshop.africa/pub/AFNOG/CommunicatingOverMQTT/ledControlPublic.py.txt
In order to use the package clientPublic.py.txt, it must be renamed to client.py and the Cayenne and WiFi credentials must be added. This program together with __init
__.py must be stored in a directory called "cayenne".
The example program
The example program uses hardware prepared for a simplistic demonstration IoT. It consists of a photo-resistor in series with a 1kOhm resistor forming a voltage divider. The voltage over the photo-resistor, changing with changing light intensity, is measured with the 10 bit ADC available on the ESP8266 and accessible on the A0 pin of the WeMos D1 mini CPU card. The measured voltage is published on channel 1. In addition to the photo-resistor we have an LED shining on the photo-resistor. This LED is connected to D0 on the WeMos D1 mini corresponding to GPIO 16 and can be controlled on channel 2. Finally the built-in LED on the CPU card, connected to D4 or GPIO 2 can be controlled through channel 3.
--
Uli Raich - 2019-04-09
Comments