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_3 [2024/04/21 11:38] – [Task to be implemented] pczekalski | en: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> | ||
| ====== 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: | * [[[en: | ||
| - | <todo @pczekalski> | + | There are many implementations of the MQTT protocol, but we will use the following library: |
| + | <code ini> | ||
| + | lib_deps = | ||
| + | knolleary/ | ||
| + | </code> | ||
| ===== Suggested Readings and Knowledge Resources ===== | ===== Suggested Readings and Knowledge Resources ===== | ||
| Line 17: | Line 20: | ||
| * [[en: | * [[en: | ||
| * [[en: | * [[en: | ||
| - | * [[en: | + | * [[en: |
| + | * [[en: | ||
| ===== Hands-on Lab Scenario ===== | ===== Hands-on Lab Scenario ===== | ||
| + | Note - this scenario can be used in pair with [[[en: | ||
| ==== Task to be implemented ==== | ==== Task to be implemented ==== | ||
| - | Connect to the " | + | Connect to the " |
| <note warning> | <note warning> | ||
| 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' |
| + | Refer to the hardware documentation and ensure an understanding of the network infrastructure you're interfacing with.\\ | ||
| + | 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. | ||
| + | |||
| + | === Step 2 === | ||
| + | Include | ||
| + | <code c> | ||
| + | #include < | ||
| + | </ | ||
| + | |||
| + | === Step 3 === | ||
| + | Declare necessary addresses, constants, etc.: | ||
| + | <code c> | ||
| + | IPAddress mqttServer(127, | ||
| + | #define mqtt_user "mqtt user" | ||
| + | #define mqtt_password "mqtt password" | ||
| + | #define mqtt_client_id " | ||
| + | #define mqtt_topic "/ | ||
| + | #define mqtt_payload " | ||
| + | </ | ||
| + | Refer to the technical documentation (nodes) or the supervisor' | ||
| + | **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); | ||
| + | </ | ||
| + | Note that your clients are not yet online! | ||
| + | |||
| + | === Step 5 === | ||
| + | Set MQTT client' | ||
| + | <code c> | ||
| + | ... | ||
| + | client.setServer(mqttServer, | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | === 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, | ||
| + | { | ||
| + | // Drop some info on the display that the MQTT broker is connected | ||
| + | client.publish(mqtt_topic, | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | int status = client.state(); | ||
| + | //present it on the display to trace/ | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | In the case, the client does not connect to the MQTT broker, the '' | ||
| + | <code c> | ||
| + | |||
| + | // Possible values for client.state() | ||
| + | #define MQTT_CONNECTION_TIMEOUT | ||
| + | #define MQTT_CONNECTION_LOST | ||
| + | #define MQTT_CONNECT_FAILED | ||
| + | #define MQTT_DISCONNECTED | ||
| + | #define MQTT_CONNECTED | ||
| + | #define MQTT_CONNECT_BAD_PROTOCOL | ||
| + | #define MQTT_CONNECT_BAD_CLIENT_ID | ||
| + | #define MQTT_CONNECT_UNAVAILABLE | ||
| + | #define MQTT_CONNECT_BAD_CREDENTIALS 4 | ||
| + | #define MQTT_CONNECT_UNAUTHORIZED | ||
| + | </ | ||
| + | |||
| + | <note tip>In many code samples, including those provided along with this MQTT client library, there is a '' | ||
| ==== 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 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: | 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: | ||
| ===== 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:// | + | **How do I observe messages that I send?**: Use a software client, such as [[http:// |
| **Do I need to authorise to publish and subscribe? | **Do I need to authorise to publish and subscribe? | ||