assuming that you have both the code working fine and you are able to read sensor data.
so next is that you need to send the sensor data from this two code to cayenne.
Below is the basic code which is used to send data to cayenne.
#!/usr/bin/env python
import cayenne.client
import time
//include any library/packages you want to use.
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME = "MQTT_USERNAME"
MQTT_PASSWORD = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"
client = cayenne.client.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883)
i=0
timestamp = 0
while True:
client.loop()
//code to read your sensor.
if (time.time() > timestamp + 10):
//this sends data every 10 seconds.
client.celsiusWrite(1, i)
client.luxWrite(2, i*10)
client.hectoPascalWrite(3, i+800)
timestamp = time.time()
i = i+1
So there are three main things to consider:
-
. Library/packages you need to add in this code for the sensor.
in your case for dht:import Adafruit_DHT
and for pH:
import io # used to create file streams
import fcntl # used to access I2C parameters like addresses
import time # used for sleep delay and timestamps
import string # helps parse strings
-
.the code you used for getting reading from the sensor.
for DHT: Adafruit_Python_DHT/simpletest.py at master · adafruit/Adafruit_Python_DHT · GitHub
for pH: https://github.com/dombold/MyHydroPi/blob/master/Python%203/Sensors/rpi_i2c_ph_sensor_py3.py -
. now that you have the sensor reading, you need to send data to cayenne.
For DHT:
client.virtualWrite(channel, temperature, "temp", "c")
client.virtualWrite(channel, humidity, "rel_hum", "p")
For pH:
client.virtualWrite(channel, pH, "soil_ph", "null")
have a look at this Data types for Cayenne MQTT API