Arduino MQTT同步性
我尝试在Arduino WifireV2和 在Ubuntu20-System上的Python脚本。
出于测试目的,Arduino与3个超声传感器和一个陀螺仪传感器连接。
MQTT连接似乎效果很好,Arduino确实发布了传感器数据,并且 还从Python脚本接收命令。
但是传感器数据不是同步发送的。 首先发送的传感器更频繁(关于Faktor 4) 比其他传感器数据。
该脚本是从此示例代码中采用的: https:https:// docs:// docs。 arduino.cc/tutorials/uno-wifi-rev2/uno-wifi-r2-mqtt-device-to-device
这是我的Arduino脚本中的循环:
const long interval = 30;
unsigned long previousMillis = 0;
void loop() {
mqttClient.poll();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
//record random value from A0, A1 and A2
//int Rvalue = analogRead(A0);
int gyroSCL = digitalRead(gryoSCL_PIN);
int gyroSDA = digitalRead(gryoSDA_PIN);
int l_distance = getUSensDistance(USens_L_Trig_Pin,USens_L_Echo_Pin);
int r_distance = getUSensDistance(USens_R_Trig_Pin,USens_R_Echo_Pin);
int f_distance = getUSensDistance(USens_F_Trig_Pin,USens_F_Echo_Pin);
int g_value = getGyro();
mqttClient.beginMessage(topicL);
mqttClient.print(l_distance);
mqttClient.endMessage();
mqttClient.beginMessage(topicR);
mqttClient.print(r_distance);
mqttClient.endMessage();
mqttClient.beginMessage(topicF);
mqttClient.print(f_distance);
mqttClient.endMessage();
mqttClient.beginMessage(topicG);
mqttClient.print(g_value);
mqttClient.endMessage();
//Serial.println();
}
}
Python脚本基于此示例: https://www.emqx.com/en/ Blog/how-to-use-mqtt-in-python
python订户的打印示例就是这样:
Connected to MQTT Broker!
Received `44` from `usens_l` topic (timeDiff: 678024
Received `25` from `usens_f` topic (timeDiff: 882899
Received `44` from `usens_l` topic (timeDiff: 87380
Received `49` from `usens_l` topic (timeDiff: 274183
Received `44` from `usens_l` topic (timeDiff: 501763
Received `44` from `usens_l` topic (timeDiff: 702241
Received `44` from `usens_l` topic (timeDiff: 911118
Received `44` from `usens_l` topic (timeDiff: 113206
Received `44` from `usens_l` topic (timeDiff: 316174
Received `45` from `usens_l` topic (timeDiff: 521477
Received `45` from `usens_l` topic (timeDiff: 725778
Received `45` from `usens_l` topic (timeDiff: 930363
Received `-1` from `gyro` topic (timeDiff: 135167
Received `45` from `usens_l` topic (timeDiff: 354054
Received `54` from `usens_l` topic (timeDiff: 647140
Received `42` from `usens_r` topic (timeDiff: 647306
Received `41` from `usens_r` topic (timeDiff: 852423
Received `55` from `usens_l` topic (timeDiff: 58828
Received `55` from `usens_l` topic (timeDiff: 261107
Received `55` from `usens_l` topic (timeDiff: 465823
Received `55` from `usens_l` topic (timeDiff: 671458
因此,显然没有发送某些传感器数据(或未收到)。 我尝试了不同的间隔,但是无论如何 USENS_L比其他数据更频繁。
可能有一种更简单的方法可以同步发送数据 但是构建字符串或计算为单个值以集成 数据并立即发送。也许这是解决方案 我当前的问题。但是我想明白,为什么 沟通无法正常工作。
谢谢您的一些提示!
I try to establish a MQTT-connection between Arduino WifiRev2 and
a python script on an ubuntu20-system.
For test purposes the arduino is connected with 3 ultrasonic sensors and a gyro sensor.
The mqtt-connection seems to work fine, arduino does publish the sensor data and
also receives commands from the python scripts.
But the sensor data are not sent synchronously.
The sensor sent first is received much more frequent (about faktor 4)
than the other sensor data.
The script is adopted from this example code:
https://docs.arduino.cc/tutorials/uno-wifi-rev2/uno-wifi-r2-mqtt-device-to-device
Here is the loop in my arduino script:
const long interval = 30;
unsigned long previousMillis = 0;
void loop() {
mqttClient.poll();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
//record random value from A0, A1 and A2
//int Rvalue = analogRead(A0);
int gyroSCL = digitalRead(gryoSCL_PIN);
int gyroSDA = digitalRead(gryoSDA_PIN);
int l_distance = getUSensDistance(USens_L_Trig_Pin,USens_L_Echo_Pin);
int r_distance = getUSensDistance(USens_R_Trig_Pin,USens_R_Echo_Pin);
int f_distance = getUSensDistance(USens_F_Trig_Pin,USens_F_Echo_Pin);
int g_value = getGyro();
mqttClient.beginMessage(topicL);
mqttClient.print(l_distance);
mqttClient.endMessage();
mqttClient.beginMessage(topicR);
mqttClient.print(r_distance);
mqttClient.endMessage();
mqttClient.beginMessage(topicF);
mqttClient.print(f_distance);
mqttClient.endMessage();
mqttClient.beginMessage(topicG);
mqttClient.print(g_value);
mqttClient.endMessage();
//Serial.println();
}
}
The python script is based on this example:
https://www.emqx.com/en/blog/how-to-use-mqtt-in-python
A print example from the python subscriber is like this:
Connected to MQTT Broker!
Received `44` from `usens_l` topic (timeDiff: 678024
Received `25` from `usens_f` topic (timeDiff: 882899
Received `44` from `usens_l` topic (timeDiff: 87380
Received `49` from `usens_l` topic (timeDiff: 274183
Received `44` from `usens_l` topic (timeDiff: 501763
Received `44` from `usens_l` topic (timeDiff: 702241
Received `44` from `usens_l` topic (timeDiff: 911118
Received `44` from `usens_l` topic (timeDiff: 113206
Received `44` from `usens_l` topic (timeDiff: 316174
Received `45` from `usens_l` topic (timeDiff: 521477
Received `45` from `usens_l` topic (timeDiff: 725778
Received `45` from `usens_l` topic (timeDiff: 930363
Received `-1` from `gyro` topic (timeDiff: 135167
Received `45` from `usens_l` topic (timeDiff: 354054
Received `54` from `usens_l` topic (timeDiff: 647140
Received `42` from `usens_r` topic (timeDiff: 647306
Received `41` from `usens_r` topic (timeDiff: 852423
Received `55` from `usens_l` topic (timeDiff: 58828
Received `55` from `usens_l` topic (timeDiff: 261107
Received `55` from `usens_l` topic (timeDiff: 465823
Received `55` from `usens_l` topic (timeDiff: 671458
So obviously some sensor data are not sent (or not received).
I tried with different intervals, but in any case it seems
that usens_l is receveid much more frequent than the other data.
There could be a more simple way to send the data synchronously
but building a string or calculate to a single value to integrate
the data and send at once. Maybe this is the solution for
my current issue. But I would like to understand, why this
communication does not work as expected.
Thank you for some hints!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我找到了解决方案,这比预期的要容易得多:
我只需要安装本地蚊子服务器(作为Ubuntu服务),现在的日志如下:
O.k., I found the solution, it was far more easy than expected:
I just had to install a local mosquitto server (as ubuntu service), now the log looks like this: