Qwiic Kit for Raspberry Pi V2 Hookup Guide

Pages
Contributors: M-Short, bboyho
Favorited Favorite 4

More with Cayenne

Now let's look at the Cayenne part of our code. Sending data to the service will enable you to view the sensor readings on the Cayenne's dashboard.

Sensor Data on Cayenne

Credentials

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_sgp40_voc_index = "v1/" + username + "/things/" + clientid + "/data/7"

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. We will send this when the counter reaches about 900. This is about 15 minutes assuming that we set the delay to 1 second ( i.e. time.sleep(1), which is not shown below) so that we utilize the Cayenne service as necessary and to make it easier to handle our data over a long period of time.

language:python
if u==900:
    #send data every 15 minutes to Cayenne, 15 minutes => ~900 seconds; u=900
    #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_sgp40_voc_index, payload = voc_index, retain = True)
    u=0 #reset to 0 to begin logging data after another 15 minutes