【问题标题】:Why can't I use python-mosquitto on the Raspberry Pi?为什么我不能在树莓派上使用 python-mosquitto?
【发布时间】:2015-05-25 23:20:20
【问题描述】:

我正在使用带有 Debian Wheezy 映像的 Raspberry Pi。我已经安装了 Mosquitto(MQTT 协议的代理)、mosquitto 客户端和 python mosquitto,以便在我的 Python 脚本中使用 mosquitto,我运行了一个非常简单的示例来测试我的所有包是否都可以正常工作

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!")

由于某种原因,我收到以下错误。

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

有人可以解释为什么这不起作用以及可能的解决方案。

【问题讨论】:

    标签: python raspberry-pi raspbian mqtt mosquitto


    【解决方案1】:

    我实际上正在为我的大学开发一个项目,使用 mosquitto 作为 MQTT 的代理。我建议你使用 paho 作为 python 模块来使用 MQTT 发布和订阅。

    官方页面:

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

    这是一个非常简单的示例,它订阅了代理 $SYS 主题树并打印出结果消息(取自 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()
    

    【讨论】:

    • 我再次尝试 sudo easy_install pip 但同样的错误:(
    • 你用的是python 3吗?请告诉我readlink -f $(which python)python -V 的输出
    • pi@raspberrypi ~ $ readlink -f $(which python) /usr/bin/python2.7 pi@raspberrypi ~ $ python -V Python 2.7.3
    • 试试sudo easy_install Distribute。如果出现问题,请尝试sudo easy_install -U Distribute。然后尝试使用sudo easy_install pip 再次安装 pip。
    • 两个代码都给出相同的错误“ sudo: easy_install: command not found”
    【解决方案2】:

    Mosquitto Python 模块已捐赠给 Eclipse Paho 项目。它可以使用“pip install paho-mqtt”安装,在https://pypi.python.org/pypi/paho-mqtt有可用的文档

    Mosquitto Python 模块的现有用户应该会发现将他们的代码移植到 Paho 版本非常容易。

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

    来自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()
    

    试一试!

    【讨论】:

    • 我找到了这个示例,我尝试从本指南eclipse.org/paho/clients/python 安装 paho mqtt 客户端,但显示此错误 -bash: pip: command not found
    • 好的,这是另一个问题。你使用哪个版本的 Python?只需输入python --version。请查看该问题:stackoverflow.com/a/9781752/1293700sudo easy_install pip 应该可以帮助您安装 pip。之后你可以使用它来安装pip install paho-mqtt
    • 感谢@Darius M 的帮助,但我找到了解决方案,我的问题也解决了:)
    • 是的,我在回答之前没有刷新网站。我很高兴它有效。
    • 请您帮帮我,因为我想在我的树莓派上实现代理,并且使用这个库我无法理解如何使用它:(
    猜你喜欢
    • 2020-07-14
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    相关资源
    最近更新 更多