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 14:42] – [Prerequisites] 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 21: | 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 ==== | ||
| Line 50: | Line 51: | ||
| Declare necessary addresses, constants, etc.: | Declare necessary addresses, constants, etc.: | ||
| <code c> | <code c> | ||
| - | #define mqtt_server "here comes MQTT broker IP on the internal IoT network" | + | IPAddress mqttServer(127, |
| #define mqtt_user "mqtt user" | #define mqtt_user "mqtt user" | ||
| #define mqtt_password "mqtt password" | #define mqtt_password "mqtt password" | ||
| + | #define mqtt_client_id " | ||
| #define mqtt_topic "/ | #define mqtt_topic "/ | ||
| #define mqtt_payload " | #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!** | ||
| + | |||
| + | === 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? | ||