1. 网络传输的方式以及不同
(1)socket通讯
(2)Flask
(3)多节点的传输:MQTT、目标检测的算法
ESP也可以运行MQTT的客户端
2. MQTT原理
适用于物联网架构
2. mosquitto服务器
Paho:客户端
MQTTbox:window测试
ws:9001\1883
刚装不带ws功能->修改(加三行)
就可以连接了
websocket:两个网页:一个心跳包、一个渲染
(高版本的mosquitto修改)
实验过程
安装
sudo apt-get install mosquitto mosquitto-clients
mosquitto 常用命令https://www.cnblogs.com/xcsn/p/11360413.html
查看mosquitto的运行结果:
查看mosquitto的运行结果:
ps -A | grep mosquitto
开启接收进程
mosquitto_sub -h 192.168.243.128 -p 1883 -u root -P root -t 'top2'
发送pub
mosquitto_pub -h 192.168.243.128 -p 1883 -u root -P root -t 'top2' -m '12345678'
通讯实例1:
Ubuntu运行:mosquitto_sub -d -h 192.168.243.128 -t 'txt'
MQTTBox运行:
通讯实例2(webserver版):
注意修改mosquitto服务端,注意要加9001
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript" src="../static/js/paho-mqtt.js"></script>
<script>
// Create a client instance
client = new Paho.MQTT.Client("192.168.243.128", Number(9001), "clientId2");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({
onSuccess: onConnect
});
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
setInterval(function() {
var data = {
photosensitive: Math.floor(Math.random() * 100),
};
message = new Paho.MQTT.Message(JSON.stringify(data));
message.destinationName = "sensor/photosensitive";
client.send(message);
}, 2000);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
}
</script>
</body>
</html>
接收端:渲染折线图
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- Load c3.css -->
<link type="text/css" href="../static/css/c3.css" rel="stylesheet">
</head>
<body>
<div id="real-chart"></div>
<!-- Load d3.js and c3.js -->
<script type="text/javascript" src="../static/js/d3.v4.min.js" charset="utf-8"></script>
<script type="text/javascript" src="../static/js/c3.min.js"></script>
<script type="text/javascript" src="../static/js/paho-mqtt.js"></script>
<script>
// 初始化图表
var chart = c3.generate({
bindto: '#real-chart',
data: {
json: [],
type: 'area-spline', // line,spline,step,area,area-spline,area-step,bar,scatter,pie,donut,gauge
labels: true,
},
axis: {
x: {
type: 'timeseries',
tick: { // 设置刻度轴
count: 12,
culling: {
max: 12
},
format: '%Y-%m-%d %H:%M:%S',
}
}
},
});
// 生成图表数据
var metrics = ['光强'];
var addData = (function() {
var datas = [];
return function(data) {
datas.push(data);
if (datas.length < 12) {
chart.load({
json: datas,
keys: {
x: 'date',
value: metrics,
},
});
} else {
chart.flow({
json: [data],
keys: {
x: 'date',
value: metrics,
},
duration: 1000,
});
}
}
})();
// Create a client instance
client = new Paho.MQTT.Client("192.168.243.128", Number(9001), "clientId-sub");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({
onSuccess: onConnect
});
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe("sensor/photosensitive");
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
var obj = JSON.parse(message.payloadString);
var data = {
date: new Date(),
光强: Math.floor(obj.photosensitive),
};
addData(data);
}
</script>
</body>
</html>
通讯实例3(python收发版):
发送端:
import paho.mqtt.client as mqtt
import math
import json
import time
client = mqtt.Client("name1") # 连接MQTT服务器
client.connect("192.168.243.128", 1883)
# 循环发布数据
while True:
data = {
'photosensitive': int(math.sin(time.time())*100),
}
data_json = json.dumps(data)
# # 将光感值发布到MQTT服务器
client.publish("sensor/photosensitive", data_json)
time.sleep(1)
代码接收端:
import paho.mqtt.client as mqtt
def on_message(client, userdata, message):
print("Received message: " + str(message.payload.decode("utf-8")))
broker_address = "192.168.243.128"
client = mqtt.Client('name2')
client.on_message = on_message
client.connect(broker_address)
client.subscribe("sensor/photosensitive")
client.loop_forever()