查看Mqtt的源代码
←
Mqtt
跳转到:
导航
,
搜索
因为以下原因,你没有权限编辑本页:
您刚才请求的操作只有这个用户组中的用户才能使用:
用户
您可以查看并复制此页面的源代码:
== Python == === Install Python3 === * Install python3.8 from https://www.python.org/downloads/ ** Windows: https://www.python.org/ftp/python/3.8.1/python-3.8.1-amd64.exe ** Mac OS X: https://www.python.org/ftp/python/3.8.1/python-3.8.1-macosx10.9.pkg Install pip: <source lang=bash> $ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py $ sudo python get-pip.py </source> === Install mqtt package === <source lang=bash> $ python3 --version Python 3.5.3rc1 $ pip3 install paho-mqtt Collecting paho-mqtt Downloading paho-mqtt-1.2.tar.gz (49kB) 100% |████████████████████████████████| 51kB 308kB/s Building wheels for collected packages: paho-mqtt Running setup.py bdist_wheel for paho-mqtt ... done Stored in directory: /home/comcat/.cache/pip/wheels/fa/db/fb/b495e37057e2f40534726b3c00ab26a58fc80fb8d17223df07 Successfully built paho-mqtt Installing collected packages: paho-mqtt Successfully installed paho-mqtt-1.2 </source> More infor: https://pypi.python.org/pypi/paho-mqtt <br> === mqtt client === <source lang=python> import json import paho.mqtt.client as mqtt import sys # replace it to your id ########################################### mqtt_uname = 'test' mqtt_pass = 'test' devid = '12020030001' ########################################### def help_msg(): print("Supported cmd:") print("\tmqtt.py message") if len(sys.argv) >= 2: set_state = sys.argv[1] else: help_msg() sys.exit(-2) mqtt_tx_topic = 'dev/gw' mqtt_rx_topic = 'app2dev/' + devid mqtt_host = '192.168.1.97' mqtt_port = 1883 mqtt_msg = set_state def on_connect(client, u_dat, rc): if rc == 0: print("Connected successfully") else: print("Connection failed. rc = "+str(rc)) def on_publish(client, u_dat, rc): print("Message "+str(rc)+" published.") def on_subscribe(client, u_dat, mid, qos): print("Subscribe with "+str(mid)+" received") def on_message(client, udat, msg): print("Message received on topic "+msg.topic+" and payload "+str(msg.payload)) mqttc = mqtt.Client() mqttc.on_connect = on_connect mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe mqttc.on_message = on_message mqttc.username_pw_set(mqtt_uname, mqtt_pass) mqttc.connect(mqtt_host, mqtt_port) mqttc.publish(mqtt_tx_topic, mqtt_msg) </source> <br><br>
返回到
Mqtt
。
个人工具
登录
名字空间
页面
讨论
变换
查看
阅读
查看源代码
查看历史
操作
搜索
导航
首页
社区专页
新闻动态
最近更改
随机页面
帮助
工具箱
链入页面
相关更改
特殊页面