I use this python script to implement the Paho base subscriber (MQTT), but under certain circumstances it does not call on_connect .
I tried the following, all with exactly the same code:
- Run on Raspbery pi, desktop broker (Ubuntu). Work .
- Running on a PC (Ubuntu), a broker on the same PC. Does not work .
- Running in Pc (Ubuntu), a broker on another PC (Ubuntu). Does not work .
- Run on PC (Ubuntu), broker.hivemq.com as a broker. Work .
- Powered by PC (Ubuntu), broker.hivemq.com (this is IP) as a broker. Work .
I also usually do:
- mosquitto_sub / pub in the terminal in all previous combinations. Work .
- The official paho C ++ shell in all previous combinations. Work .
Also checked all the configuration methods listed on the official website. Thus, the pahon paho library does not seem to get along with my local broker unless I started it from my raspberries. But using the same broker works well with the paho C ++ library and when using MQTT from the terminal. Any ideas on what could happen?
import paho.mqtt.client as mqtt
def on_connect(mqtt_client, obj, flags, rc):
mqtt_client.subscribe("test")
print "on_connect"
def on_message(mqtt_client, obj, msg):
print "on_message"
mqtt_client = mqtt.Client()
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.connect("127.0.0.1",1883)
mqtt_client.loop_forever()
source
share