Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:practical:hardware:sut:esp32:iot_3 [2024/04/21 11:34] – [FAQ] pczekalskien:iot-open:practical:hardware:sut:esp32:iot_3 [2024/04/28 16:45] (current) – [Suggested Readings and Knowledge Resources] pczekalski
Line 1: Line 1:
-<todo @pczekalski>Dokończyć</todo> 
 ====== IOT3: Connecting to the MQTT and publishing data ====== ====== IOT3: Connecting to the MQTT and publishing data ======
 In the following scenario, you will learn how to connect to the MQTT broker and publish a message. In the following scenario, you will learn how to connect to the MQTT broker and publish a message.
Line 11: Line 10:
   * [[[en:iot-open:practical:hardware:sut:esp32:iot_2|]].   * [[[en:iot-open:practical:hardware:sut:esp32:iot_2|]].
  
-<todo @pczekalski>Tu trzeba dodać bibliotekę do MQTT, pewnie pub-sub client</todo>+There are many implementations of the MQTT protocolbut we will use the following library: 
 +<code ini> 
 +lib_deps =  
 +    knolleary/PubSubClient@^2.8 
 +</code>
  
 ===== Suggested Readings and Knowledge Resources ===== ===== Suggested Readings and Knowledge Resources =====
Line 17: Line 20:
   * [[en:iot-open:hardware2:esp32|]],   * [[en:iot-open:hardware2:esp32|]],
   * [[en:iot-open:practical:hardware:sut:esp32|]],   * [[en:iot-open:practical:hardware:sut:esp32|]],
-  * [[en:iot-open:iotprogramming2:espressif_networking|]].+  * [[en:iot-open:iotprogramming2:espressif_networking|]] 
 +  * [[en:iot-open:networking2:applicationnetworkprotocols|]].
  
 ===== 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_4|]] to build a publish-subscribe solution using two devices (sender and receiver). You need to book two devices then and develop them in parallel.
  
 ==== 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 the display (for tracing). Once connected to the networking layer, connect to the MQTT broker and present the connection status on the display, then publish an MQTT message. +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 publish an MQTT message of your choice
-<note warning>MQTT clients are identified by their name, so use a unique one, e.g., the end of the IP address assigned, your unique name, etc. It is essential because if you accidentally use someone else's name then you will mess with messages, and your MQTT client will be instantly disconnected when another one with the same name connects!</note>+<note warning>MQTT clients are identified by their name, so use a unique one, e.g., the end of the IP address assigned, your unique name, etc. It is essential because if you accidentally use someone else's namethen you will mess with messages, and your MQTT client will be instantly disconnected when another one with the same name connects!</note>
  
 ==== Start ==== ==== Start ====
Line 32: Line 37:
  
 === Step 1 === === Step 1 ===
-Include  +Once the device is booked, check if your display of choice is visible in the camera's FOV.\\  
-==== Result validation ==== +Refer to the hardware documentation and ensure an understanding of the network infrastructure you're interfacing with.\\ 
-You should be able to connect to the WiFi and present the dynamically assigned IP address by the DHCP server<note>Note that due to the dynamic nature of the lab, IP addresses can change both during connection (on lease refreshor between consecutive connects.</note>+Implement the code to display on the selected device.\\ 
 +Connect to the WiFi in the STA mode (as a clientand ensure the connection is OK and you got an IP from the DHCP server.
  
 +=== Step 2 ===
 +Include the MQTT implementation library header in your code:
 +<code c>
 +#include <PubSubClient.h>
 +</code>
 +
 +=== Step 3 ===
 +Declare necessary addresses, constants, etc.:
 +<code c>
 +IPAddress mqttServer(127,0,0,1); //change it to the MQTT broker IP
 +#define mqtt_user "mqtt user"
 +#define mqtt_password "mqtt password"
 +#define mqtt_client_id "some_unique_client_id"
 +#define mqtt_topic "/sample/topic/comes/here/change/it/please"
 +#define mqtt_payload "sample payload"
 +</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''.\\
 +**Remember to choose some unique client ID and topic!**
 +
 +=== Step 4 ===
 +Declare WiFi communication client and MQTT communication client:
 +<code c>
 +  WiFiClient espClient;
 +  PubSubClient client(espClient);
 +</code>
 +Note that your clients are not yet online!
 +
 +=== Step 5 ===
 +Set MQTT client's configuration (proposed in the ''void setup()'' function:
 +<code c>
 +  ...
 +  client.setServer(mqttServer,1883); //default port is 1883, change if needed
 +  ...
 +</code>
 +
 +=== Step 6 ===
 +Finally, connect the MQTT client to the MQTT broker and publish a message (sample code, adjust to your case):
 +<code c>
 +  while (!client.connected())
 +    {
 +      if (client.connect(mqtt_client_id,mqtt_user,mqtt_password))
 +      {
 +        // Drop some info on the display that the MQTT broker is connected
 +        client.publish(mqtt_topic,mqtt_payload);
 +      }
 +      else
 +      {
 +        int status = client.state(); //read error code
 +        //present it on the display to trace/troubleshoot
 +      }
 +    }
 +</code>
 +
 +In the case, the client does not connect to the MQTT broker, the ''client.state();'' returns an int that can be interpreted as below:
 +<code c>
 +
 +// Possible values for client.state()
 +#define MQTT_CONNECTION_TIMEOUT     -4
 +#define MQTT_CONNECTION_LOST        -3
 +#define MQTT_CONNECT_FAILED         -2
 +#define MQTT_DISCONNECTED           -1
 +#define MQTT_CONNECTED               0
 +#define MQTT_CONNECT_BAD_PROTOCOL    1
 +#define MQTT_CONNECT_BAD_CLIENT_ID   2
 +#define MQTT_CONNECT_UNAVAILABLE     3
 +#define MQTT_CONNECT_BAD_CREDENTIALS 4
 +#define MQTT_CONNECT_UNAUTHORIZED    5
 +</code>
 +
 +<note tip>In many code samples, including those provided along with this MQTT client library, there is a ''client.loop();'' function executed in the main ''void loop()'' body. While it is obligatory in the case of the use of asynchronous sending and receiving of the MQTT messages, in the simple case as above, it can be abandoned because ''client.publish(topic,payload);'' makes a blocking call and ensures sending of the message to the MQTT broker.</note>
 +==== 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 publish a message (once or periodically). Depending on whether you're fully remote or able to access our networks with an additional device, you need to implement a subscriber (as present in the scenario [[[en:iot-open:practical:hardware:sut:esp32:iot_4|]]) or use MQTT Explorer (or any other application capable of connecting to our MQTT Broker) to observe messages that you publish.
 ===== FAQ ===== ===== FAQ =====
 **My MQTT client disconnects randomly**: The most common reason is you're using a non-unique MQTT client name. Please change it to some other (even random generated) and give it another try.\\ **My MQTT client disconnects randomly**: The most common reason is you're using a non-unique MQTT client name. Please change it to some other (even random generated) and give it another try.\\
-**How do I observe messages that I send?**: Use a software client, such as [[http://mqtt-explorer.com/| MQTT Explorer]], if you're able to access the "internal IoT" network (you're in the range of the network). If you're remote, the only way is to book another device and implement a client subscribing to your message. Our MQTT broker is also visible in the campus network on the wired interfaces so that you can access it, e.g. via EduVPN or from the laboratory computers. Refer to the supervisor for IP and credentials.\\+**How do I observe messages that I send?**: Use a software client, such as [[http://mqtt-explorer.com/| MQTT Explorer]], if you're able to access the "internal IoT" network (you're in the range of the network). If you're remote, the only way is to book another device and implement a client subscribing to your message, as presented in the scenario [[[en:iot-open:practical:hardware:sut:esp32:iot_4|]]. Our MQTT broker is also visible in the campus network on the wired interfaces so that you can access it, e.g. via EduVPN or from the laboratory computers. Refer to the supervisor for IP and credentials.\\
 **Do I need to authorise to publish and subscribe?**: Yes, you do. The supervisor provides the user and password on demand, also presented in the Node's technical documentation. **Do I need to authorise to publish and subscribe?**: Yes, you do. The supervisor provides the user and password on demand, also presented in the Node's technical documentation.
  
en/iot-open/practical/hardware/sut/esp32/iot_3.1713699251.txt.gz · Last modified: 2024/04/21 11:34 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