0

When I try and run the code below the screen remains blank and doesn't indicate the client is connected to the broker.

#! /usr/bin/python
import paho.mqtt.client as mqtt

broker = "localhost"
#define what happens after connection
def on_connect(rc):
    print "Connection returned result: "+str(rc)
#On recipt of a message do action
def on_message(msg):
    n = msg.payload
    t = msg.topic
    print t+" "+str(n)
# create broker
mqttc = mqtt.Client()

#define callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect

#connect
mqttc.connect(broker, 1883, 60)

#Subscribe to topic
mqttc.subscribe("/sensor/rfid", 2)

#keep connected
mqttc.loop_forever()

I can verify that the broker is running properly since I was able to run

mosquitto_sub -t /sensor/rfid

and get messages sent from the MyMQTT app on my android phone. I also forgot to mention this is all on the raspberry pi with mosquitto, mosquitto-clients, and paho-mqtt installed.

Anthony
  • 5
  • 5

1 Answers1

1

Try using the official example on the Paho site; https://eclipse.org/paho/clients/python/ or more here; http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.python.git/tree/examples

As it looks like you are missing a few parameters in the function on_connect()

Matt.
  • 1,043
  • 1
  • 12
  • 20
  • I modified my code to add the missing parameters and tried the official example. Neither of them worked. – Anthony Dec 13 '14 at 01:46
  • What version do you have installed? ```pip show paho-mqtt``` ? Also if you use the official example with the public mqtt server ```iot.eclipse.org``` do you have the same issue? And what version is your mqtt broker? – Matt. Dec 13 '14 at 06:25
  • That command doesn't work for me and i'm able to connect to the public mqtt server just fine. The broker i'm using is mosquitto version 0.15 – Anthony Dec 14 '14 at 23:39
  • Upgrade the broker then, that is an older one. (latest is 1.3.5) Follow this guide on adding the repo and upgrading it, http://jpmens.net/2013/09/01/installing-mosquitto-on-a-raspberry-pi/ should work after that. – Matt. Dec 15 '14 at 00:25
  • I updated it just like you said and it worked! Thanks for your help! – Anthony Dec 15 '14 at 00:48