Arduino MQTT synchronicity



我尝试在Arduino WifiRev2和ubuntu20系统上的python脚本

为了测试,arduino连接了3个超声波传感器和一个陀螺仪传感器。

mqtt连接似乎工作良好,arduino确实发布传感器数据和还接收来自python脚本的命令。

但是传感器数据不是同步发送的。首先发送的传感器接收频率要高得多(约为4倍)。小于其他传感器的数据

脚本采用以下示例代码:https://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_1比其他数据被接收的频率要高得多。

可能有一种更简单的方法来同步发送数据但构建一个字符串或计算到一个单一的值来集成数据立即发送。也许这就是解我现在的问题。但我想知道,为什么会这样通讯不正常

谢谢你的提示!

好的。,我找到了解决办法,它比想象的要容易得多:

我刚刚安装了一个本地的蚊子服务器(作为ubuntu服务),现在日志看起来像这样:

Received `49` from `usens_l` topic (timeDiff: 305325
Received `82` from `usens_r` topic (timeDiff: 305449
Received `16` from `usens_f` topic (timeDiff: 307354
Received `-1` from `gyro` topic (timeDiff: 309461
Received `49` from `usens_l` topic (timeDiff: 334474
Received `82` from `usens_r` topic (timeDiff: 337135
Received `16` from `usens_f` topic (timeDiff: 337257
Received `-1` from `gyro` topic (timeDiff: 339283

相关内容

  • 没有找到相关文章

最新更新