3.0 KiB
Control your nightlight for Internet - Wio Terminal
For dis part of di lesson, you go send telemetry wey get light levels from your Wio Terminal go di MQTT broker.
Install di JSON Arduino libraries
One popular way wey people dey use send message for MQTT na JSON. Arduino get one library for JSON wey dey make am easy to read and write JSON documents.
Task
Install di Arduino JSON library.
-
Open di nightlight project for VS Code.
-
Add dis one line join di
lib_depslist for diplatformio.inifile:bblanchon/ArduinoJson @ 6.17.3Dis one go import ArduinoJson, wey be Arduino JSON library.
Publish telemetry
Di next step na to create one JSON document wey get telemetry and send am go di MQTT broker.
Task - publish telemetry
Publish telemetry go di MQTT broker.
-
Add dis code for di bottom of di
config.hfile to define di telemetry topic name for di MQTT broker:const string CLIENT_TELEMETRY_TOPIC = ID + "/telemetry";Di
CLIENT_TELEMETRY_TOPICna di topic wey di device go dey use publish light levels. -
Open di
main.cppfile -
Add dis
#includedirective for di top of di file:#include <ArduinoJSON.h> -
Add dis code inside di
loopfunction, just before didelay:int light = analogRead(WIO_LIGHT); DynamicJsonDocument doc(1024); doc["light"] = light; string telemetry; serializeJson(doc, telemetry); Serial.print("Sending telemetry "); Serial.println(telemetry.c_str()); client.publish(CLIENT_TELEMETRY_TOPIC.c_str(), telemetry.c_str());Dis code go read di light level, create one JSON document wey get di light level using ArduinoJson. Di document go then serialize to string and publish am for di telemetry MQTT topic by di MQTT client.
-
Upload di code go your Wio Terminal, and use di Serial Monitor to see di light levels wey dem dey send go di MQTT broker.
Connecting to WiFi.. Connected! Attempting MQTT connection...connected Sending telemetry {"light":652} Sending telemetry {"light":612} Sending telemetry {"light":583}
💁 You fit find dis code for di code-telemetry/wio-terminal folder.
😀 You don successfully send telemetry from your device.
Disclaimer:
Dis dokyument don use AI translation service Co-op Translator do di translation. Even as we dey try make am correct, abeg make you sabi say machine translation fit get mistake or no dey accurate well. Di original dokyument wey dey for di native language na di main source wey you go trust. For important mata, e good make professional human translator check am. We no go fit take blame for any misunderstanding or wrong interpretation wey fit happen because you use dis translation.