1 year ago

#328385

test-img

Matouš Hucl

MQTT client infinite loop with script

I have a problem. I am trying to make remote control from my PC of my RPi using MQTT. The goal is to control Pi (turn the LED on and off) from Sript1 by publish and to recieve MQTT message back from Script 2 aswell by publish, that I can store in SQLite database. The MQTT message to RPi is controled through input by "yes". The scripts work well. Even getting one message back. But the problem is that in the first script its not loop and it happens just once and I don t have idea how to make it so I can turn LED on and off again and again through console (by "yes") and recieve the MQTT message back. When I get "Connected with result code 0" I can´t make another input. Is there any way how to make this script works like that? Thank you very much

Console messages:

do you want to turn LED on?

yes
Done
do you want to turn LED off?

yes
Done
Connected with result code 0
Backmsg b'LEDon'

Script 1 (PC)

import paho.mqtt.publish as publish #publish MQTT
import paho.mqtt.client as mqtt

def publishMQTT():
    publish.single("lamp/status", "lampON", hostname="test.mosquitto.org")
    print("Done")

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("Backmsg") #topic
    

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

while True:
        var = 0
             
        if var == 0:
            print("do you want to turn LED on?")
            on = input()
              
        if on == "yes":
            publishMQTT()
            var = var+1
                                 
        if var == 1:
            print("do you want to turn LED off?")
            off = input()
        
        if off == "yes":
            publishMQTT()
            var = var-1
            
        client = mqtt.Client()
        client.on_connect = on_connect           
        client.on_message = on_message
        client.connect("test.mosquitto.org", 1883, 60)
        client.loop_forever() 

Script 2 (RPi)

import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import RPi.GPIO as GPIO

var = 0

led = 22

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) #nevim co zanmená
GPIO.setup(led,GPIO.OUT) #led je output
GPIO.output(led,GPIO.LOW)

def publishMQTT():
    publish.single("Backmsg", "LEDon", hostname="test.mosquitto.org")
    print("Done")

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("lamp/status") #topic
    
def on_message(client, userdata, msg):
    global var
    print(msg.topic+" "+str(msg.payload))
    
    if var == 0:
        GPIO.output(led,GPIO.HIGH) #LED on
        var = var+1
        publishMQTT()
    else:
        GPIO.output(led,GPIO.LOW) #LED off
        var = var-1
        publishMQTT()    
    
try:
    while True:
    
        # Create an MQTT client and attach our routines to it.
        client = mqtt.Client()
        client.on_connect = on_connect
        client.on_message = on_message
        client.connect("test.mosquitto.org", 1883, 60)
        client.loop_forever()
    
except KeyboardInterrupt:
    GPIO.cleanup()  

client

mqtt

remote-access

publisher

subscriber

0 Answers

Your Answer

Accepted video resources