Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Time delayed Condition for MQTT(Trying to activate LED if PAHO client receives two specified messages from broker within 1 second of each other)
Time delayed Condition for MQTT [message #1797948] Fri, 09 November 2018 05:46
Max Maximus is currently offline Max MaximusFriend
Messages: 1
Registered: November 2018
Junior Member
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.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)
        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

Previous Topic:Multiple subscription Problem
Next Topic:Is it possible for a subscriber to know a publisher's client ID?
Goto Forum:

Current Time: Sat Nov 28 23:50:10 GMT 2020

Powered by FUDForum. Page generated in 0.02593 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top