Axelle
Axelle

Reputation: 472

Python paho mqtt client won't publish and subscribe at the same time

When I test the publish through MQTTLens, it works. However when I press a button it does trigger "on_publish" but nothing gets received at the other end's on_message; it is not getting triggered. There is two Raspberry Pi's running the same script, the only difference is their broker IP and that the topics are reversed.

import RPi.GPIO as io
import os
import json
from time import sleep
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

############### MQTT section ##################

Broker = "192.168.1.10"

rcv_topic = "home/groundfloor/livingroom/lights/lightx"    # receive messages on this topic
snd_topic = "home/groundfloor/kitchen/lights/lightx"       # send messages to this topic

def on_connect(mqttc, obj, flags, rc):
    print("rc: "+str(rc))
    mqttc.subscribe(rcv_topic) #receving/subscriber    

#when receving a message:
def on_message(mqttc, obj, msg):
    print("sub") #this is not being executed on button push, but it is when I publish through the MQTTLens
    print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
    try:
        p = msg.payload.decode("utf-8")
        print("decoded payload: " + p)
        x = json.loads(p)
        set_leds(leds, tuple(x['leds'])) #set leds to received value

        return
    except Exception as e:
        print(e)

# callback functie voor publish  event
def on_publish(mqttc, obj, mid):
    print("pub")
    return

mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect(Broker, 1883, 60) #last could be a port too
mqttc.loop_start() #client.loop_forever()

############### led&button section ##################
def init_leds(leds):
    io.setup(leds, io.OUT)

def set_leds(leds, states):
    print("leds and states: " + str(leds) + " " + str(states))
    io.output(leds, states)

def snd_msg(led):
    dataToSend=json.dumps({"leds":[led1State,led2State]})
    print("data: " + dataToSend)
    mqttc.publish(snd_topic, dataToSend)

io.add_event_detect(btn1,io.FALLING,callback=lambda *a: snd_msg(1),bouncetime=500)

############### main ##################

def main():
    try:
        while True:
            init_leds(leds)
    except KeyboardInterrupt:
        pass
    finally:
        io.cleanup()

#toplevel script
#below will only execute if ran directly - above is always accessible
if __name__ == '__main__':
    main()

I only included the parts of my code that are directly related to my issue and altered some of it to be shorter. If more code is required, however, I can always provide it.

I realise that this might be a duplicate of this question, but I've already tried deriving my code from the answer and it does not seem to be solving my issue unless I'm doing something wrong.

Upvotes: 1

Views: 2394

Answers (2)

Arbaz Alam
Arbaz Alam

Reputation: 1382

It seems that you are publishing and Subscribing on two different IPs. In order to receive the message you have to publish on IP 192.168.1.10 (I am supposing that this is your Broker IP) on topic TOPIC_TEST (assume) and Subscribe on IP 192.168.1.10 on same topic TOPIC_TEST.

Upvotes: 1

Axelle
Axelle

Reputation: 472

As suggested by hardillb (I'm not sure how to tag someone), my error was that I was using different IP's as a Broker for each respective Rpi.. they're now both listening to 1.10 and it works.

You mentioned swapping the broker ips, surely they should both be pointing at the same broker? – hardillb

Upvotes: 0

Related Questions