Maria Borbonés
Maria Borbonés

Reputation: 153

Using mqtt paho for Python in a raspberry pi

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?

Upvotes: 2

Views: 13593

Answers (1)

Matt.
Matt.

Reputation: 1033

I just quickly tired your code and it is publishing to messagesight.demos.ibm.com fine.

example

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/

Upvotes: 1

Related Questions