Hello All,
I have a PAHO client (code below) on a raspberry pi connected to an LED.
I am trying to figure out a way to activate the LED only if the client receives the strings "red" and "green" in the topic "test" within 1 second (approx) of each other.
Through trial an error I have been trying to figure this out but clearly missing something, I am certain that I need to use a while loop in the on_message function but I keep messing it up. Any assistance, would be appreciated. Thanks in advance.
import paho.mqtt.client as paho
import RPi.GPIO as GPIO
import time
#define LED pin to light up
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT) # LED to GPIO24
#Define Function
def on_message(client, userdata, message):
if (message.payload.decode() == "green" and message.payload.decode() == "red"):
GPIO.output(24, True)
time.sleep(2)
GPIO.output(24, False)
client = paho.Client("client-003")
client.on_message = on_message
client.connect("my_IP_Address", 1883, 60) # connect
client.loop_start() # start loop to process received messages
client.subscribe("test") # subscribe
time.sleep(1)
client.loop_forever()