Qwiic Kit for Raspberry Pi Hookup Guide

This Tutorial is Retired!

Note: This tutorial is for the Qwiic Starter Kit for Rasberry Pi V1. For users with the Qwiic Starter Kit for Raspberry Pi V2, make sure to check out the updated tutorial.

View the updated tutorial: Qwiic Kit for Raspberry Pi V2 Hookup Guide

Pages
Contributors: M-Short, bboyho
Favorited Favorite 1

More with Cayenne

Now let's look at the Cayenne part of our code. Let's start with the the definitions. Remember the username, password, and client ID we got earlier? We are going to copy and paste these into the code for the respective username, password, and clientid. Now our code knows not only to post to Cayenne, but who's account and what project this is for.

language:python
username = "______ENTER_MQTT_USERNAME____"
password = "______ENTER_MQTT_PASSWORD____"
clientid = "___ENTER_CAYENNE_CLIENTE_ID___"
mqttc=mqtt.Client(client_id = clientid)
mqttc.username_pw_set(username, password = password)
mqttc.connect("mqtt.mydevices.com", port=1883, keepalive=60)
mqttc.loop_start()

Topics

Next, we are going to setup our topics. Topics are how MQTT keeps track of what is what. Each topic gets a different channel which is the number at the end of the line. Otherwise, the code is exactly the same for each topic name. We just need to figure out once at the beginning what pieces of data we want to send.

language:python
#set MQTT topics (we are not setting topics for everything)
topic_bme_temp = "v1/" + username + "/things/" + clientid + "/data/1"
topic_bme_hum = "v1/" + username + "/things/" + clientid + "/data/2"
topic_bme_pressure = "v1/" + username + "/things/" + clientid + "/data/3"
topic_bme_altitude = "v1/" + username + "/things/" + clientid + "/data/4"

topic_prox_proximity = "v1/" + username + "/things/" + clientid + "/data/5"
topic_prox_ambient = "v1/" + username + "/things/" + clientid + "/data/6"

topic_ccs_temp = "v1/" + username + "/things/" + clientid + "/data/7"
topic_ccs_tvoc = "v1/" + username + "/things/" + clientid + "/data/8"
topic_ccs_co2 = "v1/" + username + "/things/" + clientid + "/data/9"

Publishing Sensor Data to the Cloud

Once in the main part of the code, we are going to publish data to each of those topics so Cayenne will see this. You'll notice we are using the topics we set up earlier, as well as setting the payload to the variable we want to send with it.

language:python
#publishing data to Cayenne (we are not publishing everything)
mqttc.publish (topic_bme_temp, payload = tempf, retain = True)
mqttc.publish (topic_bme_hum, payload = humidity, retain = True)
mqttc.publish (topic_bme_pressure, payload = pressure, retain = True)
mqttc.publish (topic_bme_altitude, payload = altitudef, retain = True)

mqttc.publish (topic_prox_proximity, payload = proximity, retain = True)
mqttc.publish (topic_prox_ambient, payload = ambient, retain = True)

#mqttc.publish (topic_ccs_temp, payload = ccstemp, retain = True)
mqttc.publish (topic_ccs_tvoc, payload = tvoc, retain = True)
mqttc.publish (topic_ccs_co2, payload = co2, retain = True)