1

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.

Community
  • 1
  • 1
Axelle
  • 442
  • 1
  • 9
  • 24
  • Edit the code to include the `on_connect` function please so we can check how/when you are doing the subscribing. – hardillb May 13 '17 at 17:04
  • I've added it ! – Axelle May 13 '17 at 17:11
  • I can't see anything wrong with the MQTT side of things. I don't have a anything to hook up to the GPIO pins on a pi at the moment so I swapped in a `sys.stdin.readline()` in the `while True` loop and it sends/receives messages just fine. – hardillb May 13 '17 at 17:40
  • You mentioned swapping the broker ips, surely they should both be pointing at the same broker? – hardillb May 13 '17 at 17:41
  • Oh I had no clue I thought that they had to point to their own IP.. I'm guessing that's my issue then? I'm going to try pointing both to 192.168.1.10 @hardillb – Axelle May 13 '17 at 17:58
  • That was my issue! Thank you so much! – Axelle May 13 '17 at 18:00

2 Answers2

1

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.

Arbaz Alam
  • 1,172
  • 1
  • 14
  • 24
0

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

Axelle
  • 442
  • 1
  • 9
  • 24