英文:
Arduino Uno MQTT over USB
问题
我是中文翻译:
总的来说,我是个完全的脚本小白。我一直在尝试通过 ChatGPT 的帮助,将 LM35 传感器的数据从 Arduino Uno 发布到 MQTT 上,以监测阁楼中的温度,但始终不成功。
目前我的设置包括 Windows 客户端 PC、Arduino Uno R3 和 LM35 温度传感器(没有 ESP 或以太网扩展板)。
Arduino 需要将 LM35 传感器的数据通过 USB 传输到 Windows 客户端的 COM3 端口,该端口被用作 Broker。
但我似乎无法得到任何实质性的输出。每当我尝试运行这个程序时,都会得到一些乱码(请参见附图)。
[乱码输出](https://i.stack.imgur.com/Yt5lT.png)
当我自己通过命令行发布数据时,数据会显示在订阅端上。
我尝试过很多方法,但以下是我当前的程序草稿,请帮忙看看吧!
英文:
total scripting noob here. I, with the help of chatgpt, have been trying to publish data from my LM35 on Arduino Uno over MQTT to monitor the temps in my attic but without any luck.
Currently my setup consists of Windows Client PC, my Arduino Uno R3 and the LM35 (no esp or ethernetshield).
The Arduino has to send the data from the LM35 to the Windows Client over USB on Port COM3 which is supposed to act as the Broker.
I cannot seem to get it to output anything really. When i try to run the sketch, i always get scrambled gibberish (see attached image)
Gibberish output
When i publish something myself over the command line the data shows up on the sub side
I have tried a bunch of things but here is my current sketch, if anyone could help that would be awsome!
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PubSubClient.h>
const char* mqtt_server = "10.6.53.4";
const int mqtt_port = 1883;
const char* mqtt_topic = "temp/attic";
const int oneWireBus = A0;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
PubSubClient mqttClient;
void setup() {
Serial.begin(9600);
sensors.begin();
mqttClient.setServer(mqtt_server, mqtt_port);
while (!mqttClient.connected()) {
Serial.println("Connecting to MQTT broker...");
if (mqttClient.connect("arduino_client")) {
Serial.println("Connected to MQTT broker");
} else {
Serial.print("Failed with state ");
Serial.print(mqttClient.state());
delay(2000);
}
}
}
void loop() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
char payload[6];
dtostrf(temperature, 5, 2, payload);
String topic = "temp/attic";
if (!mqttClient.connected()) {
Serial.print("Connecting to MQTT broker...");
if (mqttClient.connect("ArduinoUnoClient")) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.println(mqttClient.state());
delay(2000);
return;
}
}
Serial.println("Connected to MQTT broker");
Serial.println("Publishing temperature data...");
Serial.print("Topic: ");
Serial.println(topic);
Serial.print("Payload: ");
Serial.println(payload);
mqttClient.publish(topic.c_str(), payload);
delay(5000);
}
答案1
得分: 1
除非您的Arduino充当USB-Ethernet适配器,否则这将不起作用。 PubSub客户端需要一个活动的TCP堆栈才能运行,它不能只是通过USB发送"MQTT数据包"。
在技术上可以通过几乎任何双向连接在两台设备之间发送MQTT数据包,但您必须自己从头编写整个客户端和整个经纪人/usb2broker桥接端。
这里一个更好的选择是从Arduino代码中删除所有MQTT代码,将传感器读数写入串行输出,并编写一个简短的Python脚本来读取这些数据,然后将其发布到MQTT经纪人。
英文:
Unless your Arduino is acting as a USB-Ethernet adaptor this will NOT work. The PubSub client needs an active TCP stack to work, it can not just send "MQTT Packets" over USB.
While technically possible to send MQTT packets between 2 devices over nearly any 2 way connection, you would have to write the whole client side and the whole broker/usb2broker bridge side from scratch yourself.
A much better option here is to just remove ALL the MQTT code from the Arduino code and write the sensor readings to the serial out and write a short Python script to read those in and then publish them to a MQTT broker.
答案2
得分: 0
请查看https://github.com/vortex314/serial2mqtt,其目标是“为所有小型微控制器提供MQTT发布者/订阅者功能。那些只有UART或USB接口的微控制器。”
另一个更通用的选项可能是https://github.com/mycontroller-org/2mqtt:“2mqtt是MQTT桥接器。您可以将串口、以太网转换为MQTT。”
我没有使用过它们,所以不能保证。
英文:
Take a look at https://github.com/vortex314/serial2mqtt, which stated goal is to "offer MQTT publisher/subscriber functionality to all small micro controllers. Those with just a UART or USB interface."
Another, more general option, could be https://github.com/mycontroller-org/2mqtt: "2mqtt is MQTT bridge. You can convert the serial, ethernet to MQTT"
I have not used any of them, so no guarantees.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论