NodeMCU erhält den Befehl als byte* payload, und atoi liefert mir 0
Von English mit KI übersetzt ·
Hallo, ich lerne noch. Ich habe ein NodeMCU (ESP8266) mit PubSubClient, das einen LED-Streifen über einen MOSFET steuert. Auf meinem Hermetry-Board gibt es einen Schieberegler, der einen Befehl namens light.set sendet. Ich sehe die Nachricht im seriellen Monitor ankommen:
topic: v1/rpc/req/17
payload: {"method":"light.set","params":{"level":42}}
In meinem Callback bekomme ich die 42 aber nicht heraus. Ich habe Folgendes versucht:
void callback(char* topic, byte* payload, unsigned int length) {
int level = atoi((char*)payload);
analogWrite(D5, level);
}
level ist immer 0, manchmal eine zufällige große Zahl. Der Streifen ändert sich nicht. Außerdem zeigt der Schieberegler anscheinend dauerhaft „Sent; waiting for the device“ an. Was mache ich falsch?
Hello, still learning. I have a NodeMCU (ESP8266) with PubSubClient controlling a LED strip through a MOSFET. On my Hermetry board there’s a slider that sends a command called light.set. I can see the message arrive in the serial monitor:
topic: v1/rpc/req/17
payload: {"method":"light.set","params":{"level":42}}
But in my callback I can’t get the 42 out. I tried:
void callback(char* topic, byte* payload, unsigned int length) {
int level = atoi((char*)payload);
analogWrite(D5, level);
}
level is always 0, sometimes a random big number. The strip doesn’t change. I also think the slider shows „Sent; waiting for the device“ forever. What am I doing wrong?
Von English mit KI übersetzt ·
Vielleicht zuerst in einen String casten? String s = (char*)payload;
Maybe cast it to a String first? String s = (char*)payload;
Von English mit KI übersetzt ·
Das payload ist nicht null-terminiert, daher läuft das Lesen als C-String über das Ende hinaus. Das ist die zufällige Zahl.
The payload isn’t null-terminated, so reading it as a C string runs past the end. That’s the random number.
Von English mit KI übersetzt ·
Auch mit einem Null-Byte am Ende liefert atoi auf {„method“:… eine 0, weil das erste Zeichen eine geschweifte Klammer ist. Du musst den JSON parsen.
Even with a null at the end, atoi on {„method“:… gives 0 because the first character is a brace. You need to parse the JSON.
Von English mit KI übersetzt ·
Kevin A und Maria haben beide recht. Ich habe letztes Jahr fast denselben Lichtcontroller für unser Büro gebaut. Parse das JSON mit ArduinoJson (es nimmt Puffer und Länge entgegen, daher ist kein null nötig), hole die Request-ID vom Ende des Topics und antworte auf v1/rpc/res/ mit dieser ID. Das „Warten auf das Gerät“ bleibt, weil das Board nie eine Antwort bekommt.
#include <ArduinoJson.h>
void callback(char* topic, byte* payload, unsigned int length) {
String t(topic);
String id = t.substring(t.lastIndexOf('/') + 1); // "17"
StaticJsonDocument<256> doc;
if (deserializeJson(doc, payload, length)) return;
if (doc["method"] == "light.set") {
int level = doc["params"]["level"] | -1; // -1 if missing
if (level >= 0 && level <= 100) {
analogWrite(D5, map(level, 0, 100, 0, 1023));
client.publish(("v1/rpc/res/" + id).c_str(), "{"ok":true}");
return;
}
}
client.publish(("v1/rpc/res/" + id).c_str(), "{"ok":false}");
}
ESP8266 analogWrite ist standardmäßig 0-1023, daher das map. Und abonniere v1/rpc/req/+ nach jeder Wiederverbindung, nicht nur im setup, sonst bleiben die Befehle nach dem ersten Wi-Fi-Abbruch aus.
Die Topic-Liste findest du unter MQTT topics and payload reference.
Kevin A and Maria have both bits. I did nearly the same light controller for our office last year. Parse the JSON with ArduinoJson (it takes the buffer and length, so no null needed), pull the request id off the end of the topic, and answer on v1/rpc/res/ with that id. The „waiting for the device“ stays because the board never gets an answer.
#include <ArduinoJson.h>
void callback(char* topic, byte* payload, unsigned int length) {
String t(topic);
String id = t.substring(t.lastIndexOf('/') + 1); // "17"
StaticJsonDocument<256> doc;
if (deserializeJson(doc, payload, length)) return;
if (doc["method"] == "light.set") {
int level = doc["params"]["level"] | -1; // -1 if missing
if (level >= 0 && level <= 100) {
analogWrite(D5, map(level, 0, 100, 0, 1023));
client.publish(("v1/rpc/res/" + id).c_str(), "{"ok":true}");
return;
}
}
client.publish(("v1/rpc/res/" + id).c_str(), "{"ok":false}");
}
ESP8266 analogWrite is 0-1023 by default, hence the map. And subscribe to v1/rpc/req/+ after every reconnect, not only in setup, or commands stop after the first Wi-Fi drop.
The topic list is on MQTT topics and payload reference.
Von English mit KI übersetzt ·
Es funktioniert!! Der Streifen wird jetzt mit dem Schieberegler gedimmt und das Board zeigt Done an. Ich hatte außerdem auf v1/rpc/req/17 statt res geantwortet, deshalb kam auch dann nichts zurück, wenn ich die Zahl richtig hatte. Danke Emily.
It works!! The strip dims with the slider now and the board says Done. I had also been answering on v1/rpc/req/17 instead of res, so even when I got the number right nothing came back. Thank you Emily.
- Du musst angemeldet sein, um auf dieses Thema antworten zu können.
Mitdiskutieren
Melde dich an, um Fragen zu stellen und zu beantworten, hilfreiche Beiträge mit „Gefällt mir“ zu markieren und die Antwort zu kennzeichnen, die dein Problem gelöst hat. Jeder Beitrag bringt Punkte und Stufen.

