当前位置:网站首页>Mqtt protocol application: external network mobile phone control internal network raspberry pie
Mqtt protocol application: external network mobile phone control internal network raspberry pie
2022-04-22 15:10:00 【Charming Sun】
The experiment purpose : Use the serial port of raspberry pie to output the string sent by the external network mobile phone
Don't understand MQTT For the of the agreement, please refer to :MQTT Protocol experience Mosquitto Installation and use
Don't know how to use Python For the operation of raspberry pie serial port, please refer to :Raspberry Pi B Of UART Minimalist routine
One 、 Preparation
- The server is installed Mosquitto(MQTT Agent software );
- Raspberry pie installed Python( Used to write raspberry pie subscription code ), And then use pip install paho-mqtt( Realized MQTT Agreed Python expanded memory bank )、wiringpi2( Control raspberry pie IO Oral Python expanded memory bank );
- The phone is installed MQTT Android client .
Two 、 Edit and run the raspberry pie subscription code
Edit and save the following code in raspberry pie as MQTT.py file , And add executable permissions , Then run with super user MQTT.py.
#!/usr/bin/env python
import paho.mqtt.client as mqtt
import wiringpi
# Connect successfully callback function
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# After the connection is completed, subscribe serial The theme
client.subscribe("serial")
# Message push callback function
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
# Send the load through the serial port
wiringpi.serialPuts(serial,str(msg.payload))
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
wiringpi.wiringPiSetup()
serial = wiringpi.serialOpen('/dev/ttyAMA0',9600)
try:
# Please change to yourself MQTT Public network of proxy server IP
client.connect("192.168.1.107", 1883, 60)
client.loop_forever()
except KeyboardInterrupt:
client.disconnect()
wiringpi.serialClose(serial)
3、 ... and 、 Turn on the server MQTT Agency service , And use the mobile client to publish messages
Execute on server mosquitto command , function MQTT Agency service . Then open the MQTT Client mobile phone APP, Input MQTT Proxy server IP Address 、serial Subject and message content , Finally, click Publish, At this point, the serial port of raspberry pie will output the message content .
Four 、 Further study
Want to know paho-mqtt For more functions of the extension library, please refer to :paho-mqtt 1.1 : Python Package Index
Want to know wiringpi2 For more functions of the extension library, please refer to :WiringPi_WiringPi-Python: Python-wrapped version of Gordon Henderson’s WiringPi version 2.
Want to get more open source on the platform MQTT For the of the client, please refer to :Paho - Open Source messaging for M2M
版权声明
本文为[Charming Sun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221504282650.html
边栏推荐
- Pat class a 1012: the best rank (25)
- 【深入理解TcaplusDB技术】更替列表指定位置数据接口说明——[List表]
- How to import Cisco evng image simulator
- 查看自己的系统激活类型
- Web automation testing framework realizes the operation of web elements through JS
- [Mysql] CHAR_ Length function
- 数学史有没有必要读?关于数学教育,我们忽视了太多
- ROS communication mechanism II - service communication
- Development trend of C in 2022
- ODPS SQL节点参数怎么设置呢?
猜你喜欢
随机推荐
查看自己的系统激活类型
这两种人是做不好自媒体的,一辈子都赚不到大钱
中国海油上交所上市:市值6515亿 年利润703亿
数据库操作
靠谱的教育金理财产品哪个最合适?
【深入理解TcaplusDB技术】读取列表所有数据示例代码——[List表]
【深入理解TcaplusDB技术】示例代码——异步调用接口
web自动化测试框架通过js来实现对web元素的操作
How to set ODPs SQL node parameters?
【深入理解TcaplusDB技术】读取列表指定位置数据示例代码——[List表]
There are Chinese characters in the input parameter, and an error of 500 is reported. There is an internal error in the server
Bootsrap 模态框文字超出处理
C语言的基本练习(002-1)
Android的UI---ZoomControls放大缩小图片,android面试简历模板
Introduction notes to golang - redis
The GNU build system体验教程:Hello world example with Autoconf and Automake
Memcpy() function copies two-dimensional array & memcmp() function compares two-dimensional array
Take you to understand the principle of highly flexible spark architecture
Introduction to openvino
带你了解极具弹性的Spark架构的原理






![【深入理解TcaplusDB技术】批量读取数据示例代码——[Generic表]](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)

