Hi,
I have written a piece of python code which is not optimized at all.The code controls the status of 20 switches (off=0/on=2). I’m using the mqtt api to connect to Cayenne. Here is the code:
import cayenne.client #Cayenne MQTT Client
from time import sleep
from gpiozero import Button
button1=Button(14) # Declaring button pin 14
button2=Button(15) # Declaring button pin 15
button3=Button(18) # Declaring button pin 18
button4=Button(23) # Declaring button pin 23
button5=Button(24) # Declaring button pin 24
button6=Button(25) # Declaring button pin 25
button7=Button(8) # Declaring button pin 8
button8=Button(7) # Declaring button pin 7
button9=Button(12) # Declaring button pin 12
button10=Button(16) # Declaring button pin 16
button11=Button(20) # Declaring button pin 20
button12=Button(21) # Declaring button pin 21
button13=Button(2) # Declaring button pin 2
button14=Button(3) # Declaring button pin 3
button15=Button(4) # Declaring button pin 4
button16=Button(17) # Declaring button pin 17
button17=Button(27) # Declaring button pin 27
button18=Button(22) # Declaring button pin 22
button19=Button(10) # Declaring button pin 10
button20=Button(9) # Declaring button pin 9
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME = "x"
MQTT_PASSWORD = "x"
MQTT_CLIENT_ID = "x"
client = cayenne.client.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
def send_on1():
client.virtualWrite(8, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 8
print("Button pressed\n")
def send_off1():
client.virtualWrite(8, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 8
print("Button released\n")
def send_on2():
client.virtualWrite(10, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 10
print("Button pressed\n")
def send_off2():
client.virtualWrite(10, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 10
print("Button released\n")
def send_on3():
client.virtualWrite(12, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 12
print("Button pressed\n")
def send_off3():
client.virtualWrite(12, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 12
print("Button released\n")
def send_on4():
client.virtualWrite(16, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 16
print("Button pressed\n")
def send_off4():
client.virtualWrite(16, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 16
print("Button released\n")
def send_on5():
client.virtualWrite(18, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 18
print("Button pressed\n")
def send_off5():
client.virtualWrite(18, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 18
print("Button released\n")
def send_on6():
client.virtualWrite(22, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 22
print("Button pressed\n")
def send_off6():
client.virtualWrite(22, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 22
print("Button released\n")
def send_on7():
client.virtualWrite(24, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 24
print("Button pressed\n")
def send_off7():
client.virtualWrite(24, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 24
print("Button released\n")
def send_on8():
client.virtualWrite(26, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 26
print("Button pressed\n")
def send_off8():
client.virtualWrite(26, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 26
print("Button released\n")
def send_on9():
client.virtualWrite(32, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 32
print("Button pressed\n")
def send_off9():
client.virtualWrite(32, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 32
print("Button released\n")
def send_on10():
client.virtualWrite(36, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 36
print("Button pressed\n")
def send_off10():
client.virtualWrite(36, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 36
print("Button released\n")
def send_on11():
client.virtualWrite(38, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 38
print("Button pressed\n")
def send_off11():
client.virtualWrite(38, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 38
print("Button released\n")
def send_on12():
client.virtualWrite(40, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 40
print("Button pressed\n")
def send_off12():
client.virtualWrite(40, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 40
print("Button released\n")
def send_on13():
client.virtualWrite(3, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 3
print("Button pressed\n")
def send_off13():
client.virtualWrite(3, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 3
print("Button released\n")
def send_on14():
client.virtualWrite(5, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 5
print("Button pressed\n")
def send_off14():
client.virtualWrite(5, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 5
print("Button released\n")
def send_on15():
client.virtualWrite(7, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 7
print("Button pressed\n")
def send_off15():
client.virtualWrite(7, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 7
print("Button released\n")
def send_on16():
client.virtualWrite(11, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 11
print("Button pressed\n")
def send_off16():
client.virtualWrite(11, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 11
print("Button released\n")
def send_on17():
client.virtualWrite(13, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 13
print("Button pressed\n")
def send_off17():
client.virtualWrite(13, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 13
print("Button released\n")
def send_on18():
client.virtualWrite(15, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 15
print("Button pressed\n")
def send_off18():
client.virtualWrite(15, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 15
print("Button released\n")
def send_on19():
client.virtualWrite(19, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 19
print("Button pressed\n")
def send_off19():
client.virtualWrite(19, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 19
print("Button released\n")
def send_on20():
client.virtualWrite(21, 1, "digital_sensor", "d") #Publish "1" to Cayenne MQTT Broker Channel 21
print("Button pressed\n")
def send_off20():
client.virtualWrite(21, 0, "digital_sensor", "d") #Publish "0" to Cayenne MQTT Broker Channel 21
print("Button released\n")
button1.when_pressed=send_on1 #When button pressed run send_on function
button1.when_released=send_off1 #When button released run send_off function
button2.when_pressed=send_on2 #When button pressed run send_on function
button2.when_released=send_off2 #When button released run send_off function
button3.when_pressed=send_on3 #When button pressed run send_on function
button3.when_released=send_off3 #When button released run send_off function
button4.when_pressed=send_on4 #When button pressed run send_on function
button4.when_released=send_off4 #When button released run send_off function
button5.when_pressed=send_on5 #When button pressed run send_on function
button5.when_released=send_off5 #When button released run send_off function
button6.when_pressed=send_on6 #When button pressed run send_on function
button6.when_released=send_off6 #When button released run send_off function
button7.when_pressed=send_on7 #When button pressed run send_on function
button7.when_released=send_off7 #When button released run send_off function
button8.when_pressed=send_on8 #When button pressed run send_on function
button8.when_released=send_off8 #When button released run send_off function
button9.when_pressed=send_on9 #When button pressed run send_on function
button9.when_released=send_off9 #When button released run send_off function
button10.when_pressed=send_on10 #When button pressed run send_on function
button10.when_released=send_off10 #When button released run send_off function
button11.when_pressed=send_on11 #When button pressed run send_on function
button11.when_released=send_off11 #When button released run send_off function
button12.when_pressed=send_on12 #When button pressed run send_on function
button12.when_released=send_off12 #When button released run send_off function
button13.when_pressed=send_on13 #When button pressed run send_on function
button13.when_released=send_off13 #When button released run send_off function
button14.when_pressed=send_on14 #When button pressed run send_on function
button14.when_released=send_off14 #When button released run send_off function
button15.when_pressed=send_on15 #When button pressed run send_on function
button15.when_released=send_off15 #When button released run send_off function
button16.when_pressed=send_on16 #When button pressed run send_on function
button16.when_released=send_off16 #When button released run send_off function
button17.when_pressed=send_on17 #When button pressed run send_on function
button17.when_released=send_off17 #When button released run send_off function
button18.when_pressed=send_on18 #When button pressed run send_on function
button18.when_released=send_off18 #When button released run send_off function
button19.when_pressed=send_on19 #When button pressed run send_on function
button19.when_released=send_off19 #When button released run send_off function
button20.when_pressed=send_on20 #When button pressed run send_on function
button20.when_released=send_off20 #When button released run send_off function
while True:
client.loop()
Everything works fine, but sometimes the value of the status gets stuck in Cayenne dashboard. For example if my switch is off, it still shows its on. I’m not sure if my problem is the python code which I’ve written, or there is a bug in system. Thanks for your help in advance.