This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot-open:practical:hardware:sut:esp32:iot_4 [2024/04/21 19:00] – created pczekalski | en:iot-open:practical:hardware:sut:esp32:iot_4 [2024/04/21 19:18] (current) – [Steps] pczekalski | ||
---|---|---|---|
Line 23: | Line 23: | ||
===== Hands-on Lab Scenario ===== | ===== Hands-on Lab Scenario ===== | ||
- | Note - this scenario can be used in pair with [[[en: | + | Note—this scenario can be used in tandem |
==== Task to be implemented ==== | ==== Task to be implemented ==== | ||
Connect to the " | Connect to the " | ||
Line 40: | Line 39: | ||
Implement the code to display on the selected device.\\ | Implement the code to display on the selected device.\\ | ||
Connect to the WiFi in the STA mode (as a client) and ensure the connection is OK and you got an IP from the DHCP server.\\ | Connect to the WiFi in the STA mode (as a client) and ensure the connection is OK and you got an IP from the DHCP server.\\ | ||
- | Note - as you're consuming messages here, you either need to have implemented and running publisher as a separate device (as described in the scenario [[[en: | + | Note - as you're consuming messages here, you either need to have implemented and running publisher as a separate device (as described in the scenario [[[en: |
=== Step 2 === | === Step 2 === | ||
Line 56: | Line 55: | ||
#define mqtt_client_id " | #define mqtt_client_id " | ||
#define mqtt_topic "/ | #define mqtt_topic "/ | ||
- | #define mqtt_payload " | ||
</ | </ | ||
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 69: | ||
=== Step 5 === | === Step 5 === | ||
+ | Declare an asynchronous handler function that will be called every time the client receives the MQTT message: | ||
+ | <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 === | ||
Set MQTT client' | Set MQTT client' | ||
<code c> | <code c> | ||
... | ... | ||
client.setServer(mqttServer, | client.setServer(mqttServer, | ||
+ | client.setCallback(callback); | ||
+ | //as declared in Step 5 | ||
... | ... | ||
</ | </ | ||
- | === Step 6 === | + | === Step 7 === |
Finally, connect the MQTT client to the MQTT broker and publish a message (sample code, adjust to your case): | Finally, connect the MQTT client to the MQTT broker and publish a message (sample code, adjust to your case): | ||
<code c> | <code c> | ||
Line 85: | 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.publish(mqtt_topic, | + | client.subscribe(mqtt_topic); |
} | } | ||
else | else | ||
Line 111: | 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: |