This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:practical:hardware:sut:esp32:iot_4 [2024/04/21 19:04] – [Steps] pczekalski | en:iot-open:practical:hardware:sut:esp32:iot_4 [2024/04/21 19:18] (current) – [Steps] pczekalski | ||
---|---|---|---|
Line 57: | Line 57: | ||
</ | </ | ||
Refer to the technical documentation (nodes) or the supervisor' | Refer to the technical documentation (nodes) or the supervisor' | ||
- | **Remember to choose some unique client ID and topic!** | + | **Remember to choose some unique client ID and topic corresponding to the one that is being published to the MQTT broker!** |
+ | <note tip>You can use wildcards when subscribing to the broker. Still, please note that using a single star to subscribe to ALL MQTT messages is not a good option as there are many technical topics published by the broker internally, so the number of messages incoming every second to your solution can quickly overwhelm your device' | ||
=== Step 4 === | === Step 4 === | ||
Line 70: | Line 71: | ||
Declare an asynchronous handler function that will be called every time the client receives the MQTT message: | Declare an asynchronous handler function that will be called every time the client receives the MQTT message: | ||
<code c> | <code c> | ||
+ | void callback(char* topic, byte* payload, unsigned int length) { | ||
+ | //prepare a code to display the message (payload) to the display | ||
+ | } | ||
</ | </ | ||
+ | Note that the payload is a byte array, so you may need to copy it to process it outside the callback function (e.g. using '' | ||
+ | Also, note that you may distinguish which message you're handling if you subscribe using wildcards: the '' | ||
=== Step 6 === | === Step 6 === | ||
Set MQTT client' | Set MQTT client' | ||
Line 78: | Line 82: | ||
... | ... | ||
client.setServer(mqttServer, | client.setServer(mqttServer, | ||
+ | client.setCallback(callback); | ||
+ | //as declared in Step 5 | ||
... | ... | ||
</ | </ | ||
Line 89: | Line 95: | ||
{ | { | ||
// Drop some info on the display that the MQTT broker is connected | // Drop some info on the display that the MQTT broker is connected | ||
+ | client.subscribe(mqtt_topic); | ||
} | } | ||
else | else | ||
Line 114: | Line 121: | ||
</ | </ | ||
+ | === Step 8 === | ||
+ | Run client handling routine in the loop to receive messages: | ||
+ | <code c> | ||
+ | void loop() | ||
+ | { | ||
+ | if (!client.connected()) { | ||
+ | //reconnect the client (and eventually the WiFi if gone) | ||
+ | } | ||
+ | client.loop(); | ||
+ | } | ||
+ | </ | ||
==== Result validation ==== | ==== Result validation ==== | ||
You should be able to connect to the WiFi and MQTT broker (verified by the status present on the selected display) and then subscribe to the message (identified by topic). Depending on whether you're fully remote or able to access our networks with an additional device, you need to implement a publisher on another laboratory node (as present in the scenario [[[en: | You should be able to connect to the WiFi and MQTT broker (verified by the status present on the selected display) and then subscribe to the message (identified by topic). Depending on whether you're fully remote or able to access our networks with an additional device, you need to implement a publisher on another laboratory node (as present in the scenario [[[en: |