1

I'm using a Raspberry Pi with a Debian Wheezy image. I have installed Mosquitto (the broker of MQTT protocol), mosquitto client and python mosquitto to use mosquitto in my Python script, I have run a very simple example to test if all my packages work fine or not

import mosquitto
mqttc = mosquitto.Mosquitto("python_pub")
mqttc.will_set("/event/dropped", "Sorry, I seem to have died.")
mqttc.connect("127.0.0.1", 1883, 60, True)

mqttc.publish("hello/world", "Hello, World!")

For some reason I get the following error though.

Traceback (most recent call last):
File "test_1.py", line 1, in <module>
import mosquitto
File "/usr/lib/pymodules/python2.7/mosquitto.py", line 484, in <module>
_mosquitto_log_init = _libmosq.mosquitto_log_init
File "/usr/lib/python2.7/ctypes/__init__.py", line 378, in __getattr__
func = self.__getitem__(name)
File "/usr/lib/python2.7/ctypes/__init__.py", line 383, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /usr/lib/libmosquitto.so.1: undefined symbol: mosquitto_log

Can somebody explain why this isn't working and a possible solution.

Kian Cross
  • 1,818
  • 2
  • 21
  • 41
Mohamed
  • 61
  • 3
  • 3
  • 11

2 Answers2

3

I am actually developing a proyect for my university using mosquitto as a broker of MQTT. I recommend you to use paho as the python module to publish and subscribe using MQTT.

The official page:

https://pypi.python.org/pypi/paho-mqtt

Here is a very simple example that subscribes to the broker $SYS topic tree and prints out the resulting messages (taken from https://pypi.python.org/pypi/paho-mqtt):

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("$SYS/#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
ederollora
  • 1,165
  • 2
  • 11
  • 29
  • I trying again sudo easy_install pip but same error :( – Mohamed May 26 '15 at 00:40
  • Are you using python 3? Tell me both the output of `readlink -f $(which python)` and `python -V` please – ederollora May 26 '15 at 00:41
  • pi@raspberrypi ~ $ readlink -f $(which python) /usr/bin/python2.7 pi@raspberrypi ~ $ python -V Python 2.7.3 – Mohamed May 26 '15 at 00:43
  • try `sudo easy_install Distribute`. If that goes bad try `sudo easy_install -U Distribute`. Then try to install pip again with `sudo easy_install pip`. – ederollora May 26 '15 at 00:44
  • both of code give the same error " sudo: easy_install: command not found " – Mohamed May 26 '15 at 00:46
  • As a final try please these commands, first: `sudo apt-get install python-setuptools python-dev build-essential` and then `sudo easy_install pip`. – ederollora May 26 '15 at 00:47
  • " Successfully installed paho-mqtt-1.1 " but when i run " cd org.eclipse.paho.mqtt.python.git " after running " git clone http://git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.python.git " -bash: cd: org.eclipse.paho.mqtt.python.git: No such file or directory is shown – Mohamed May 26 '15 at 01:00
  • if you installed paho then you just have to import it in the script that you write `import paho.mqtt.client as mqtt` and follow the script I provided you in my original post. Also look at these examples (https://eclipse.googlesource.com/paho/org.eclipse.paho.mqtt.python/+/1.0/examples) – ederollora May 26 '15 at 01:02
  • So you think that I don't need to run this lignes: - git clone http://git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.python.git - cd org.eclipse.paho.mqtt.python.git - python setup.py install – Mohamed May 26 '15 at 01:05
  • If you did already run: `sudo pip install paho-mqtt` and it all went ok then python will recognise it when you import the paho module in your script. Please try to run the example I provided you before like the one of my original post and look at the code while you see what outputs in the console. – ederollora May 26 '15 at 01:10
  • hi @ederollora, please can you help me because i want to implement the broker on my raspberry and with this library I can't understand how to use it :( – Mohamed May 26 '15 at 17:27
  • It's better to speak in some other channel that this comments.Please send me a private message in stackoverflow with youremail and wewill be in touch – ederollora May 27 '15 at 00:25
  • sorry @ederollora, i don't know how to send a private message in stackoverflow so this is my gmail: med.chatti.1@gmail.com , i hope that we can be in touch; Thanks – Mohamed May 27 '15 at 16:02
2

The Mosquitto Python module has been donated to the Eclipse Paho project. It can be installed using “pip install paho-mqtt” and there is documentation available at https://pypi.python.org/pypi/paho-mqtt

Existing users of the Mosquitto Python module should find it very easy to port their code to the Paho version.

http://mosquitto.org/documentation/python/

Official example from https://eclipse.org/paho/clients/python/:

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("$SYS/#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

Have a try!

Community
  • 1
  • 1
Darius
  • 10,762
  • 2
  • 29
  • 50
  • i found this example and I try to install the paho mqtt client from this guide https://eclipse.org/paho/clients/python/ but this error is shown -bash: pip: command not found – Mohamed May 26 '15 at 00:27
  • Okay, that's another issue. Which version of Python do you use? Just type ```python --version```. And please check out that question: http://stackoverflow.com/a/9781752/1293700, ```sudo easy_install pip``` should help you to install pip. After that you can use it to install ```pip install paho-mqtt```. – Darius May 26 '15 at 01:06
  • thanks @Darius M for your help but i had the solution and my problem is solved :) – Mohamed May 26 '15 at 01:12
  • Yeah, I didn't refresh the site before I answered. I'm glad that it works. – Darius May 26 '15 at 01:15
  • please can you help me because i want to implement the broker on my raspberry and with this library I can't understand how to use it :( – Mohamed May 26 '15 at 17:26