3.2 KiB
Control your nightlight over the Internet - Wio Terminal
In this part of the lesson, you will send telemetry data with light levels from your Wio Terminal to the MQTT broker.
Install the JSON Arduino libraries
A common way to send messages over MQTT is by using JSON. There is an Arduino library for JSON that simplifies reading and writing JSON documents.
Task
Install the Arduino JSON library.
-
Open the nightlight project in VS Code.
-
Add the following as an additional line to the
lib_deps
list in theplatformio.ini
file:bblanchon/ArduinoJson @ 6.17.3
This imports ArduinoJson, an Arduino JSON library.
Publish telemetry
The next step is to create a JSON document with telemetry data and send it to the MQTT broker.
Task - publish telemetry
Send telemetry data to the MQTT broker.
-
Add the following code to the bottom of the
config.h
file to define the telemetry topic name for the MQTT broker:const string CLIENT_TELEMETRY_TOPIC = ID + "/telemetry";
The
CLIENT_TELEMETRY_TOPIC
is the topic where the device will publish light levels. -
Open the
main.cpp
file. -
Add the following
#include
directive to the top of the file:#include <ArduinoJSON.h>
-
Add the following code inside the
loop
function, just before thedelay
: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());
This code reads the light level and creates a JSON document using ArduinoJson that contains this level. It is then serialized into a string and published to the telemetry MQTT topic by the MQTT client.
-
Upload the code to your Wio Terminal, and use the Serial Monitor to observe the light levels being sent to the MQTT broker.
Connecting to WiFi.. Connected! Attempting MQTT connection...connected Sending telemetry {"light":652} Sending telemetry {"light":612} Sending telemetry {"light":583}
💁 You can find this code in the code-telemetry/wio-terminal folder.
😀 You have successfully sent telemetry data from your device.
Disclaimer:
This document has been translated using the AI translation service Co-op Translator. While we aim for accuracy, please note that automated translations may include errors or inaccuracies. The original document in its native language should be regarded as the authoritative source. For critical information, professional human translation is advised. We are not responsible for any misunderstandings or misinterpretations resulting from the use of this translation.