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:27] – [Steps] 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> |
==== 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' |
- | ==== Result validation ==== | + | Refer to the hardware documentation |
- | You should be able to connect | + | Implement |
+ | Connect | ||
+ | === Step 2 === | ||
+ | Include the MQTT implementation library header in your code: | ||
+ | <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 ==== | ||
+ | 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 ===== | ||
- | **I set a hostname, but it does appear on the router level.**: There are many possible reasons; one is an active registration in the router | + | **My MQTT client disconnects randomly**: The most common reason is you're using a non-unique MQTT client name. Please change |
- | **Can I use a manually configured IP?**: Actually, you can, but we strongly discourage it. This is because you may accidentally overlap this IP address with some other device, router, or other infrastructure, | + | **How do I observe messages that I send?**: Use a software client, such as [[http:// |
+ | **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. | ||
<WRAP noprint> | <WRAP noprint> |