Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:iot-open:practical:hardware:sut:esp32:iot_4 [2024/04/21 19:00] – created pczekalskien: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:iot-open:practical:hardware:sut:esp32:iot_3|]] to build a publish-subscribe solution using two devices (sender and receiver). You need to book two devices then and develop them in parallel. +Notethis scenario can be used in tandem with [[[en:iot-open:practical:hardware:sut:esp32:iot_3|]] to build a publish-subscribe solution using two devices (two laboratory nodes: publisher and subscriber). You need to book two devices then and develop them in parallel. Watch the result validation section for more options.
 ==== Task to be implemented ==== ==== Task to be implemented ====
 Connect to the "internal IoT" WiFI access point as presented in the scenario [[[en:iot-open:practical:hardware:sut:esp32:iot_2|]]—present connection status on display. Once connected to the networking layer (WiFi), connect the MQTT client to the MQTT broker and present the connection status on the display, then subscribe to the MQTT message of your choice, identified by the topic (may include wildcards). Present the message payload on the display. Connect to the "internal IoT" WiFI access point as presented in the scenario [[[en:iot-open:practical:hardware:sut:esp32:iot_2|]]—present connection status on display. Once connected to the networking layer (WiFi), connect the MQTT client to the MQTT broker and present the connection status on the display, then subscribe to the MQTT message of your choice, identified by the topic (may include wildcards). Present the message payload on the display.
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:iot-open:practical:hardware:sut:esp32:iot_3|]] or to connect to the MQTT broker using some software, e.g+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:iot-open:practical:hardware:sut:esp32:iot_3|]]or to connect to the MQTT broker using some software, as described in the results validation section below
  
 === Step 2 === === Step 2 ===
Line 56: Line 55:
 #define mqtt_client_id "some_unique_client_id" #define mqtt_client_id "some_unique_client_id"
 #define mqtt_topic "/sample/topic/comes/here/change/it/please" #define mqtt_topic "/sample/topic/comes/here/change/it/please"
-#define mqtt_payload "sample payload" 
 </code> </code>
 Refer to the technical documentation (nodes) or the supervisor's guidance if working in the interactive mode to obtain the ''mqttServer'' IP address, the ''mqtt_user'' and ''mqtt_password''.\\ Refer to the technical documentation (nodes) or the supervisor's guidance if working in the interactive mode to obtain the ''mqttServer'' IP address, the ''mqtt_user'' and ''mqtt_password''.\\
-**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's resources.</note>
  
 === 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
 +}
 +</code>
 +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 ''memcpy'').\\ 
 +Also, note that you may distinguish which message you're handling if you subscribe using wildcards: the ''topic'' parameter provided the exact topic as it was published to the MQTT broker by the publisher.
 +=== Step 6 ===
 Set MQTT client's configuration (proposed in the ''void setup()'' function: Set MQTT client's configuration (proposed in the ''void setup()'' function:
 <code c> <code c>
   ...   ...
   client.setServer(mqttServer,1883); //default port is 1883, change if needed   client.setServer(mqttServer,1883); //default port is 1883, change if needed
 +  client.setCallback(callback); //a callback function to handle incoming messages
 +                                //as declared in Step 5
   ...   ...
 </code> </code>
  
-=== Step ===+=== Step ===
 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,mqtt_payload);+        client.subscribe(mqtt_topic);
       }       }
       else       else
Line 111: Line 121:
 </code> </code>
  
 +=== 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();  //process incoming MQTT messages
 +}
 +</code>
 ==== 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:iot-open:practical:hardware:sut:esp32:iot_3|]]) or use MQTT Explorer (or any other application capable of connecting to our MQTT Broker) to send messages that you subscribe to. 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:iot-open:practical:hardware:sut:esp32:iot_3|]]) or use MQTT Explorer (or any other application capable of connecting to our MQTT Broker) to send messages that you subscribe to.
en/iot-open/practical/hardware/sut/esp32/iot_4.1713726020.txt.gz · Last modified: 2024/04/21 19:00 by pczekalski
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0