Hi Shramik,
I’ve tried updating a sensor state on any of my esp8266 (Zone 1, 2, 4) using the attached python code on a linux host. Visually, I can see the state of the sensor has changed in the Cayenne dashboard based on the colour change to the sensor, however the ESP8266 is not picking up the state change and activating the sensor (ie. fan switch). I’ve even changed from using digital_sensor to digital_actuator but no difference observed. Any ideas?
#!/usr/bin/env python
import cayenne.client
import time
import logging
Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
NOTE: the MQTT_CLIENT_ID is the client_id for the ESP8266
MQTT_USERNAME = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
MQTT_PASSWORD = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
#Zone 1 ESP8266
#MQTT_CLIENT_ID = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
#Zone 2 ESP8266
#MQTT_CLIENT_ID = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
#Zone 4 ESP8266
MQTT_CLIENT_ID = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
#Test_python
#MQTT_CLIENT_ID = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
#Test_python2
#MQTT_CLIENT_ID = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
TRIGGER_CHANNEL = 2 #Virtual channel for publishing the trigger value.
STATE = 0
i=0
timestamp = 0
The callback for when a message is received from Cayenne.
def on_message(message):
print("message received: " + str(message))
# If there is an error processing the message return an error string, otherwise return nothing.
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
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, loglevel=logging.INFO)
def fan_state(STATE):
if STATE:
#client.virtualWrite(TRIGGER_CHANNEL,1,“digital_sensor”,“d”)
client.virtualWrite(TRIGGER_CHANNEL,1,“digital_actuator”,“d”)
else:
#client.virtualWrite(TRIGGER_CHANNEL,0,“digital_sensor”,“d”)
client.virtualWrite(TRIGGER_CHANNEL,0,“digital_actuator”,“d”)
while True:
client.loop()
if (time.time() > timestamp + 10):
timestamp = time.time()
if (i%2):
fan_state(1)
else:
fan_state(0)
i = i + 1
if (i == 12): # Reset the sensor value to test that the trigger gets reset.
i = 0