Open vzorglub opened 6 years ago
I've run across a similar error (I think). I'm getting the following error trace
Traceback (most recent call last):
File "psmqtt.py", line 228, in <module>
mqttc.connect(cf.get('mqtt_broker', 'localhost'), mqtt_port, 60)
File "/home/pi/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 937, in connect
return self.reconnect()
File "/home/pi/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1071, in reconnect
sock = self._create_socket_connection()
File "/home/pi/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 3522, in _create_socket_connection
return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
File "/usr/lib/python2.7/socket.py", line 575, in create_connection
raise err
socket.error: [Errno 111] Connection refused
Before I started digging around in the code I wanted to see if I've done something obviously wrong. The mqtt_broker has been tried with and without single quote; with and without using the hostname vs. IP address. The mqtt user and password work on another system - so those appear to be correct - I've used single quote for both of those as well. Any education would be appreciated.
Thanks -
Well, like so many things in my programming endeavors, it turns out I can't type...
TL; DR -
I mistyped the MQTT broker address in the config file (multiple times - it wasn't a static address - my bad).
As a very quick test of the base imports you can run the following MVP solution:
# mytest.py
import paho.mqtt.client as mqtt
broker_address="xxx.xxx.xxx.xxx" # obviously, replace xxx.xxx.xxx.xxx with MQTT IP address
client = mqtt.Client("P1")
client.connect(broker_address)
client.publish("Hello/World","Yep - Working")
So, to answer @vzorglub question, the information within the config file should be within quotes. As you can see from the example above, the minimal working code simply requires an instance of the paho
client with a unique name (this example uses "P1") and the broker_address
. I used the variable to confirm exactly how the variable is passed. Note that the Config()
method pulls the quoted information from the variable out of the .config
file not the portion after the =
for strings.
As a general observation, I'm not a professional programer but could follow @eschava coding very easily - I actually made some of my own modifications to how the MQTT reporting works for a more customized application with ease.
Hello, Documentation for psmqtt.conf is a little parse.
For example how to input another address that 'localhost' for the broker Will it be: mqtt_broker = 192.168.0.34 or mqtt_broker = '192.168.0.34'
Neither work localhost on the other pi running mosquitto works just fine...