This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:iot-open:practical:hardware:sut:stm32:iot_4 [2024/04/27 12:40] – [FAQ] ktokarz | en:iot-open:practical:hardware:sut:stm32:iot_4 [2024/04/27 12:41] (current) – ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== STM_IoT_4: Connecting to the MQTT broker and subscribing to the topic ===== | ||
| + | In the following scenario, you will learn how to connect to the MQTT broker and subscribe to the chosen topic to receive messages. | ||
| + | |||
| + | ===== Prerequisites ===== | ||
| + | To implement this scenario, it is necessary to get familiar with the LED controlling scenario: | ||
| + | * [[en: | ||
| + | |||
| + | and at least one of the following scenarios first: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | |||
| + | The requirement is to pass the scenarios | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | |||
| + | To be able to connect to the WiFi network and MQTT broker. | ||
| + | |||
| + | ===== Suggested Readings and Knowledge Resources ===== | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | |||
| + | ===== Hands-on Lab Scenario ===== | ||
| + | Note - this scenario can be used in pair with [[[en: | ||
| + | ==== Task to be implemented ==== | ||
| + | Connect to the " | ||
| + | |||
| + | <note warning> | ||
| + | |||
| + | The steps below show the principles of the software operation. How to implement the full software please refer to the previous scenarios: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | |||
| + | ==== Start ==== | ||
| + | Check if you can see a full LCD in your video stream. Book a device and create a dummy Arduino file with '' | ||
| + | |||
| + | ==== Steps ==== | ||
| + | |||
| + | === Step 1 === | ||
| + | The big part of the code is the same as in the previous scenario, so make a copy of it: | ||
| + | * [[en: | ||
| + | |||
| + | We will use the code template from [[en: | ||
| + | <code c> | ||
| + | WiFiSerial.println(" | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | do { | ||
| + | response = WiFiSerial.readStringUntil(0x0A); | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(response); | ||
| + | } while (!(response.startsWith(compOK))); | ||
| + | |||
| + | delay(1000); | ||
| + | </ | ||
| + | |||
| + | === Step 2 === | ||
| + | In the firmware for the ESP32-C3 board, there are AT commands to work with the MQTT protocol. In this scenario we will use three of them: | ||
| + | |||
| + | * " | ||
| + | * " | ||
| + | * " | ||
| + | |||
| + | Below we briefly describe the commands mentioning only the parameters important to us. Please refer to the Espressif documentation((https:// | ||
| + | < | ||
| + | If the parameter is taken within quotation marks it should be in such a form sent in a command.\\ | ||
| + | Please refer to the MQTT description chapter for the meaning of some protocol details. | ||
| + | </ | ||
| + | **AT+MQTTUSERCFG** | ||
| + | This command accepts the list of parameters: | ||
| + | < | ||
| + | AT+MQTTUSERCFG=< | ||
| + | </ | ||
| + | * LinkID - currently should be 0 | ||
| + | * scheme - 1 (MQTT over TCP) | ||
| + | * " | ||
| + | * " | ||
| + | * " | ||
| + | * cert_key-ID - should be 0 | ||
| + | * CA_ID - should be 0 | ||
| + | * " | ||
| + | |||
| + | In our case, the command can look like this: | ||
| + | < | ||
| + | AT+MQTTUSERCFG=0, | ||
| + | </ | ||
| + | |||
| + | **AT+MQTTCONN** | ||
| + | This command accepts the list of parameters: | ||
| + | < | ||
| + | AT+MQTTCONN=< | ||
| + | </ | ||
| + | * LinkID - currently should be 0 | ||
| + | * " | ||
| + | * port - the TCP port number, 1883 for most of the brokers | ||
| + | * reconnect - 1 for automatic reconnection (recommended in our case) | ||
| + | |||
| + | The command can look like this: | ||
| + | < | ||
| + | AT+MQTTCONN=0, | ||
| + | </ | ||
| + | |||
| + | **AT+MQTTSUB** | ||
| + | < | ||
| + | AT+MQTTSUB=< | ||
| + | </ | ||
| + | |||
| + | The list of parameters: | ||
| + | * LinkID - currently should be 0 | ||
| + | * " | ||
| + | * qos - mode of the quality of service, 0, 1 or 2, default 0. | ||
| + | The command can look like this in the example below: | ||
| + | < | ||
| + | AT+MQTTSUB=0, | ||
| + | </ | ||
| + | |||
| + | After a successful subscription, | ||
| + | < | ||
| + | +MQTTSUBRECV:< | ||
| + | </ | ||
| + | * LinkID - always 0 | ||
| + | * " | ||
| + | * data_length - number of bytes of the following data packet | ||
| + | * data - message payload | ||
| + | |||
| + | === Step 3 === | ||
| + | Implement the MQTT configuration, | ||
| + | |||
| + | === Step 4 === | ||
| + | Implement handling of the MQTT messages. Here we present how to control LED with messages sent with the topic " | ||
| + | Declare variables to store the strings for comparison and define LED pin. | ||
| + | <code c> | ||
| + | #define LED_GREEN D3 | ||
| + | String compend0, compend1; | ||
| + | String response; | ||
| + | </ | ||
| + | |||
| + | Prepare string variables and set the mode of the LED pin. | ||
| + | <code c> | ||
| + | compend0 = " | ||
| + | compend1 = " | ||
| + | pinMode(LED_GREEN, | ||
| + | </ | ||
| + | |||
| + | In the " | ||
| + | <code c> | ||
| + | response = WiFiSerial.readStringUntil(0x0A); | ||
| + | if (response.startsWith(compend0)) | ||
| + | { | ||
| + | digitalWrite(LED_GREEN, | ||
| + | }; | ||
| + | if (response.startsWith(compend1)) | ||
| + | { | ||
| + | digitalWrite(LED_GREEN, | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | ==== 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 topic. After a successful subscription, | ||
| + | <note info> | ||
| + | Because LCD can't properly display some non-visible characters the presented code sometimes shows additional, non-letter characters. It is out of the scope of this scenario to filter these characters out. We leave the task of making visual improvements to your invention. | ||
| + | </ | ||
| + | |||
| + | ===== 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.\\ | ||
| + | **Can I subscribe to more than one topic?**: Yes, you can. For every incoming message you would receive the information on the topic of this specific message, so you would be able to recognise them.\\ | ||
| + | **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> | ||
| + | {{: | ||
| + | </ | ||
| + | |||
| + | </ | ||