Background: BME280 Sensor connected to a Raspberry Pi using I2C, sensor works fine and I’m able to pull readings from it. But, I’m having issues with sending the readings to Cayenne,
Here is the relevant code:
import cayenne.client
#…
#####
# Connection Info for Cayenne
cayenne_url = “mqtt.mydevices.com”
cayenne_port = 1883
cayenne_usr = “32cb1320-ee08-11ea-883c-638d8ce4c23d”
cayenne_pwd = “947a180ce51cea6c1cba69f21e61db124e6cc380”
cayenne_client_id = “140b30d0-ee55-11ea-883c-638d8ce4c23d”
def post_to_cayenne(temp, humi, press):
print()
print("Sending to Cayenne...")
print(f"Connecting to {cayenne_url}...")
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(cayenne_usr, cayenne_pwd, cayenne_client_id, port=cayenne_port)
print("Sending data")
print(f" Temp: {temp}")
client.fahrenheitWrite(0, temp)
print(f" Humi: {humi}")
# Couldn't find a client.humidityWrite in the source code, so used virtualWrite instead...
client.virtualWrite(1, humi, "rel_hum", "P")
print(f"Press: {press}")
client.pascalWrite(2, press)
print("Completed sending data to Cayenne.")
data = bme280.sample(bus, address, calibration_params)
temp_f = round(conv.c_to_f(data.temperature), 2)
humidity = round(data.humidity, 2)
pressure = round(data.pressure, 2)
post_to_cayenne(temp_f, humidity, pressure)
Running the code outputs:
Sending to Cayenne…
Connecting to mqtt.mydevices.com…
Connecting to mqtt.mydevices.com:1883
Sending data
Temp: 79.02
Humi: 34.15
Press: 981.83
Completed sending data to Cayenne.
But nothing shows in the widgets or the received data. If I publish using something like MQTTLens, the data shows up just fine. Not sure what the issue is. I could just use straight paho.mqtt, but I was hoping the library would make things a tad bit easier.