3.6 KiB
Control your nightlight for Internet - Wio Terminal
For dis part of di lesson, you go subscribe to commands wey dem send from MQTT broker go your Wio Terminal.
Subscribe to commands
Di next step na to subscribe to di commands wey dem send from di MQTT broker, and respond to dem.
Task
Subscribe to commands.
-
Open di nightlight project for VS Code.
-
Add dis code for di bottom of di
config.hfile to define di topic name for di commands:const string SERVER_COMMAND_TOPIC = ID + "/commands";Di
SERVER_COMMAND_TOPICna di topic wey di device go subscribe to so e fit receive LED commands. -
Add dis line for di end of di
reconnectMQTTClientfunction to subscribe to di command topic when di MQTT client reconnect:client.subscribe(SERVER_COMMAND_TOPIC.c_str()); -
Add dis code below di
reconnectMQTTClientfunction.void clientCallback(char *topic, uint8_t *payload, unsigned int length) { char buff[length + 1]; for (int i = 0; i < length; i++) { buff[i] = (char)payload[i]; } buff[length] = '\0'; Serial.print("Message received:"); Serial.println(buff); DynamicJsonDocument doc(1024); deserializeJson(doc, buff); JsonObject obj = doc.as<JsonObject>(); bool led_on = obj["led_on"]; if (led_on) digitalWrite(D0, HIGH); else digitalWrite(D0, LOW); }Dis function na di callback wey di MQTT client go call when e receive message from di server.
Di message dey come as array of unsigned 8-bit integers, so e need to change am to character array so e fit treat am as text.
Di message get JSON document inside, and e dey decode am using di ArduinoJson library. Di
led_onproperty for di JSON document go dey read, and based on di value, di LED go turn on or off. -
Add dis code to di
createMQTTClientfunction:client.setCallback(clientCallback);Dis code go set di
clientCallbackas di callback wey dem go call when message dey received from di MQTT broker.💁 Di
clientCallbackhandler dey call for all topics wey you subscribe to. If later you write code wey dey listen to plenty topics, you fit get di topic wey dem send di message to from ditopicparameter wey dem pass to di callback function. -
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.
-
Adjust di light levels wey your physical or virtual device dey detect. You go see messages wey dem dey receive and commands wey dem dey send for di terminal. You go also see di LED dey turn on and off based on di light level.
💁 You fit find dis code for di code-commands/wio-terminal folder.
😀 You don successfully code your device to respond to commands from one MQTT broker.
Disclaimer:
Dis dokyument don use AI translation service Co-op Translator take translate am. Even though we dey try make sure say e correct, abeg sabi say automated translation fit get mistake or no dey accurate well. Di original dokyument for im native language na di main correct source. For important information, e go beta make professional human translator check am. We no go fit take blame for any misunderstanding or wrong interpretation wey fit happen because of dis translation.