This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:iot-open:practical:hardware:sut:esp32:iot_4 [2024/04/21 22:17] – [Steps] pczekalski | en:iot-open:practical:hardware:sut:esp32:iot_4 [2024/04/21 22:18] (current) – [Steps] pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== IOT4: Connecting to the MQTT and subscribing to the messages ====== | ||
| + | In the following scenario, you will learn how to connect to the MQTT broker and subscribe to the message. | ||
| + | |||
| + | ===== Prerequisites ===== | ||
| + | To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | and obligatory: | ||
| + | * [[[en: | ||
| + | |||
| + | There are many implementations of the MQTT protocol, but we will use the following library: | ||
| + | <code ini> | ||
| + | lib_deps = | ||
| + | knolleary/ | ||
| + | </ | ||
| + | |||
| + | ===== Suggested Readings and Knowledge Resources ===== | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | |||
| + | ===== Hands-on Lab Scenario ===== | ||
| + | Note—this scenario can be used in tandem with [[[en: | ||
| + | ==== Task to be implemented ==== | ||
| + | Connect to the " | ||
| + | <note warning> | ||
| + | |||
| + | ==== Start ==== | ||
| + | Check if you can clearly see a full display (of your choice) in your video stream. Book a device and create a dummy Arduino file with '' | ||
| + | Implement a connection to the " | ||
| + | |||
| + | ==== Steps ==== | ||
| + | |||
| + | === Step 1 === | ||
| + | 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.\\ | ||
| + | Note - as you're consuming messages here, you either need to have implemented and running publisher as a separate device (as described in the scenario [[[en: | ||
| + | |||
| + | === 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 "/ | ||
| + | </ | ||
| + | Refer to the technical documentation (nodes) or the supervisor' | ||
| + | **Remember to choose some unique client ID and topic corresponding to the one that is being published to the MQTT broker!** | ||
| + | <note tip>You can use wildcards when subscribing to the broker. Still, please note that using a single star to subscribe to ALL MQTT messages is not a good option as there are many technical topics published by the broker internally, so the number of messages incoming every second to your solution can quickly overwhelm your device' | ||
| + | |||
| + | === 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 === | ||
| + | Declare an asynchronous handler function that will be called every time the client receives the MQTT message: | ||
| + | <code c> | ||
| + | void callback(char* topic, byte* payload, unsigned int length) { | ||
| + | //prepare a code to display the message (payload) to the display | ||
| + | } | ||
| + | </ | ||
| + | Note that the payload is a byte array, so you may need to copy it to process it outside the callback function (e.g. using '' | ||
| + | Also, note that you may distinguish which message you're handling if you subscribe using wildcards: the '' | ||
| + | === Step 6 === | ||
| + | Set MQTT client' | ||
| + | <code c> | ||
| + | ... | ||
| + | client.setServer(mqttServer, | ||
| + | client.setCallback(callback); | ||
| + | //as declared in Step 5 | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | === Step 7 === | ||
| + | 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.subscribe(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 | ||
| + | </ | ||
| + | |||
| + | === Step 8 === | ||
| + | Run client handling routine in the loop to receive messages: | ||
| + | <code c> | ||
| + | void loop() | ||
| + | { | ||
| + | if (!client.connected()) { | ||
| + | //reconnect the client (and eventually the WiFi if gone) | ||
| + | } | ||
| + | client.loop(); | ||
| + | } | ||
| + | </ | ||
| + | ==== 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 subscribe to the message (identified by topic). Depending on whether you're fully remote or able to access our networks with an additional device, you need to implement a publisher on another laboratory node (as present in the scenario [[[en: | ||
| + | ===== 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.\\ | ||
| + | **How do I send messages to which I am subscribed? | ||
| + | **Do I need to authorise to publish and subscribe? | ||
| + | |||
| + | <WRAP noprint> | ||
| + | ===== Project information ===== | ||
| + | {{: | ||
| + | This Intellectual Output was implemented under the Erasmus+ KA2.\\ | ||
| + | Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, | ||
| + | Project number: 2022-1-PL01-KA220-HED-000085090. | ||
| + | |||
| + | **__Erasmus+ Disclaimer__**\\ | ||
| + | This project has been funded with support from the European Commission. \\ | ||
| + | This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein. | ||
| + | |||
| + | **__Copyright Notice__**\\ | ||
| + | This content was created by the IOT-OPEN.EU Reloaded consortium, 2022, | ||
| + | The content is Copyrighted and distributed under CC BY-NC [[https:// | ||
| + | <figure label> | ||
| + | {{: | ||
| + | </ | ||
| + | |||
| + | </ | ||