Using mqtt paho for Python in a raspberry pi
I just quickly tired your code and it is publishing to messagesight.demos.ibm.com
fine.
The only thing I did was comment out the userID, password.
#client.username_pw_set(userID, password)
Have you installed Paho Python Client correctly, also good example there as well. http://www.eclipse.org/paho/clients/python/
Related videos on Youtube
![Maria Borbonés](https://i.stack.imgur.com/XwoHd.jpg?s=256&g=1)
Comments
-
Maria Borbonés about 2 years
I'm trying to connect to message broker using python 2.7 in raspbian as follows:
import paho.mqtt.client as paho host="messagesight.demos.ibm.com" port=1883 def on_connect(pahoClient, obj, rc): # Once connected, publish message print "Connected Code = %d"%(rc) client.publish("prueba/123", "Hello World", 0) def on_log(pahoClient, obj, level, string): print string def on_publish(pahoClient, packet, mid): # Once published, disconnect print "Published" pahoClient.disconnect() def on_disconnect(pahoClient, obj, rc): print "Disconnected" # Create a client instance client=paho.Client() # Register callbacks client.on_connect = on_connect client.on_log = on_log client.on_publish = on_publish client.on_disconnnect = on_disconnect #Set userid and password client.username_pw_set(userID, password) #connect x = client.connect(host, port, 60) client.loop_forever()
When I run the script, Im getting the following error:
Traceback (most recent call last): File "ejemplo.py", line 27, in client=paho.Client() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 410, in init self._sockpairR, self._sockpairW = _socketpair_compat() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 255, in _socketpair_compat listensock.bind(("localhost", 0)) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 99] Cannot assign requested address
How can I fix it?
-
ralight over 9 yearsFeels a bit odd writing this, but do you have IPv4 support on your Pi?
-
ralight over 9 yearsOr perhaps your "lo" interface is down. Try
ifconfig lo down; ifconfig lo up
-
-
Maria Borbonés over 9 yearsyes, Paho Python client is correctly installed... it is still not working. We are still getting the same issue " cannot assing requested address". Which is the content of your client.py file? Can you locate line 260 "listensock.bind(("127.0.0.1", 0))"? Thanks!
-
Andy Piper over 9 yearscan you provide the output of
ifconfig -a
-
Matt. over 9 years@maria sounds like what the other guys are mentioning , chi network config.
-
Maria Borbonés over 9 yearsthanks, you are right! The problem was the /etc/network/interfaces file, it was not configured properly ...
-
Isa A over 6 yearsFor other people, what @MariaBorbones and @Matt was referring to was the comment on the question. The problem is that
lo
or loopback interface is down. you can enable it usingsudo ifconfig lo up