This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot-open:practical:hardware:sut:esp32:iot_3 [2024/04/21 11:27] – created 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' |
| - | <code c> | + | Refer to the hardware documentation and ensure an understanding of the network infrastructure you're interfacing with.\\ |
| - | #include <WiFi.h> | + | Implement the code to display on the selected device.\\ |
| - | </code> | + | Connect |
| - | The WiFi library automatically initialises a singleton class, '' | + | |
| === Step 2 === | === Step 2 === | ||
| - | Declare some constants, including AP SSID and passphrase and a variable to store IP: | + | Include the MQTT implementation library header in your code: |
| <code c> | <code c> | ||
| - | const char* ssid = " | + | #include < |
| - | const char* pass = " | + | |
| - | IPAddress localIP; | + | |
| </ | </ | ||
| - | |||
| - | Both '' | ||
| - | |||
| === Step 3 === | === Step 3 === | ||
| - | Set your device in the STA mode and connect to the WiFi AP: | + | Declare necessary addresses, constants, etc.: |
| <code c> | <code c> | ||
| - | WiFi.mode(WIFI_STA); | + | IPAddress mqttServer(127,0,0,1); //change it to the MQTT broker IP |
| - | WiFi.begin(ssid, pass); | + | #define mqtt_user "mqtt user" |
| - | while (WiFi.status() != WL_CONNECTED) { | + | #define mqtt_password "mqtt password" |
| - | //Drop some info on display about connecting | + | #define mqtt_client_id " |
| - | delay(1000); | + | #define mqtt_topic "/sample/topic/ |
| - | } | + | #define mqtt_payload " |
| </ | </ | ||
| - | + | Refer to the technical documentation | |
| - | The '' | + | **Remember |
| - | * 0, '' | + | |
| - | * 1, '' | + | |
| - | * 2, '' | + | |
| - | * 3, '' | + | |
| - | | + | |
| - | | + | |
| - | * 6, '' | + | |
| === Step 4 === | === Step 4 === | ||
| - | Reading the IP as a '' | + | Declare WiFi communication client and MQTT communication client: |
| <code c> | <code c> | ||
| - | | + | WiFiClient espClient; |
| + | PubSubClient client(espClient); | ||
| </ | </ | ||
| - | The '' | + | Note that your clients are not yet online! |
| === Step 5 === | === Step 5 === | ||
| - | Explicitly disconnect from the WiFi AP to free resources: | + | Set MQTT client' |
| <code c> | <code c> | ||
| - | | + | ... |
| + | client.setServer(mqttServer, | ||
| + | ... | ||
| </ | </ | ||
| - | Some useful WiFi functions are listed below: | + | === Step 6 === |
| - | * '' | + | Finally, connect the MQTT client to the MQTT broker |
| - | | + | <code c> |
| - | * '' | + | |
| - | ==== Result validation ==== | + | { |
| - | You should be able to connect to the WiFi and present the dynamically assigned IP address by the DHCP server. < | + | 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 | ||
| + | //present | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | 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> | ||