2938| 0
|
[求助] arduino怎样在PubSubClient库里回调函数中读取json字符 [ |
本帖最后由 sunboy25 于 2023-3-13 14:05 编辑 我想问一下如果Arduino的PubSubClient库的回调函数中的参数payload里面字符的编码用的是json,我在callback函数里怎样使用ArduinoJson库函数修改下面的代码才能正确提取json字符? void callback(char *topic, byte *payload, unsigned int length) { Serial.print("Message arrived in topic: "); Serial.println(topic); Serial.print("Message:"); String message; for (int i = 0; i < length; i++) { message = message + (char) payload; // convert *byte to string } Serial.print(message); if (message == "on") { digitalWrite(LED, LOW); } // LED on if (message == "off") { digitalWrite(LED, HIGH); } // LED off Serial.println(); Serial.println("-----------------------"); } |
© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed