当前位置: 首页 > 知识库问答 >
问题:

在raspberry pi中为Python使用mqtt paho

黄弘盛
2023-03-14
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()

我该怎么修好它?

共有1个答案

荆梓
2023-03-14

我只是很快地使代码疲劳,它发布到messagesight.demos.ibm.com

我所做的唯一一件事就是注释掉用户ID和密码。

#client.username_pw_set(userID, password)

您是否正确安装了Paho Python客户端,也是很好的例子。http://www.eclipse.org/paho/clients/python/

 类似资料:
  • 地址 https://mirrors.ustc.edu.cn/archive.raspberrypi.org/ 或 https://mirrors.ustc.edu.cn/raspberrypi/ 说明 树莓派的 archive.raspberrypi.org 软件源,也即 /etc/apt/sources.list.d/raspi.list , 是由树莓派基金会提供的软件源,包括 ui 相关程序

  • meta-raspberrypi 是针对树莓派的 OpenEmbedded BSP layer,树莓派是开源硬件的掌上电脑,OpenEmbedded是用于构建linux 操作系统的工具。

  • 太感谢你了,Pouya

  • This repo is outdated and is not maintained anymore. For better and cheaper devices see https://github.com/RavenSystem/esp-homekit-devices/wiki/Home-Accessory-Architect For better RPI camera see https

  • homebridge-raspberrypi-temperature a homebridge plugin that get RaspberryPi CPU temperature. Configuration simple configuration "accessories": [{ "accessory": "RaspberryPiTemperature", "name": "

  • 问题内容: 这两个类别有何不同? 有什么明显的区别吗? 问题答案: 是一个 类变量 。 的是一个 实例变量 。 即的被实例之间共享。 用可以像列表这样修改的东西更容易证明区别: 输出量