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:stm32:iot_3 [2024/04/26 12:44] – [Task to be implemented] ktokarz | en:iot-open:practical:hardware:sut:stm32:iot_3 [2024/04/27 09:42] (current) – ktokarz | ||
|---|---|---|---|
| Line 56: | Line 56: | ||
| delay(1000); | delay(1000); | ||
| </ | </ | ||
| + | |||
| === Step 2 === | === Step 2 === | ||
| - | The procedure of connecting to the WiFi requires some steps. Below we present | + | 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: |
| - | <code c> | + | |
| - | // Test if module is available | + | |
| - | " | + | |
| - | // Reset the module | + | * " |
| - | "AT+RST" | + | * " |
| + | | ||
| - | // Prevent from storing | + | Below we briefly describe the commands mentioning only the parameters important to us. Please refer to the Espressif documentation((https: |
| - | "AT+SYSSTORE=0" | + | < |
| + | 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) | ||
| + | * "client_id" | ||
| + | * " | ||
| + | * " | ||
| + | * cert_key-ID - should be 0 | ||
| + | * CA_ID - should be 0 | ||
| + | * " | ||
| - | // Start module in station mode (which can join the access point) | + | In our case, the command can look like this: |
| - | "AT+CWMODE=1" | + | < |
| + | AT+MQTTUSERCFG=0,1,\"STM32# | ||
| + | </ | ||
| - | // Join the access point. Use SSID and password. | + | **AT+MQTTCONN** |
| - | "AT+CWJAP=\"SSID\",\" | + | This command accepts |
| + | < | ||
| + | AT+MQTTCONN=< | ||
| + | </ | ||
| + | * LinkID - currently should be 0 | ||
| + | * "host" | ||
| + | * port - the TCP port number, 1883 for most of the brokers | ||
| + | * reconnect - 1 for automatic reconnection (recommended in our case) | ||
| - | // Get the IP address | + | The command can look like this: |
| - | "AT+CIPSTA?" | + | < |
| + | AT+MQTTCONN=0, | ||
| </ | </ | ||
| - | <note> | + | **AT+MQTTPUB** |
| - | Texts sent as part of the message are delimited with double quotation marks. In C++ we need to mark them with backslash characters inside the string constants. Examples above include these markings. | + | <code> |
| - | </note> | + | AT+MQTTPUB=< |
| - | Some explanation can be needed for the " | + | </code> |
| - | If we don't use the "AT+SYSSTORE=0" | + | |
| - | If we don't use the "AT+RST" | + | The list of parameters: |
| + | * LinkID - currently should | ||
| + | | ||
| + | | ||
| + | * qos - mode of the quality | ||
| + | * retain - retain flag, 0 or 1. | ||
| === Step 3 === | === Step 3 === | ||
| - | The command | + | Implement the MQTT configuration and connection to the broker. Use the template |
| - | <code c> | + | |
| - | +CIPSTA:ip:"192.168.1.117" | + | === Step 4 === |
| - | +CIPSTA: | + | Implement publishing of the MQTT message. You can do it just once in the " |
| - | +CIPSTA: | + | |
| - | </ | + | |
| - | While LCD use we have to filter out unwanted elements of the response. We can do it by displaying the answer containing " | ||
| - | <code c> | ||
| - | if (response.startsWith(" | ||
| - | response.remove(0, | ||
| - | lcd.setCursor(0, | ||
| - | lcd.print(response); | ||
| - | } | ||
| - | </ | ||
| ==== Result validation ==== | ==== Result validation ==== | ||
| - | You should be able to see the IP address of the ESP32-C3 module. | + | 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:iot-open: |
| - | < | + | < |
| - | Using another node ar even the same node another time can change | + | Because LCD can't properly display some non-visible characters |
| </ | </ | ||
| ===== FAQ ===== | ===== FAQ ===== | ||
| - | **Can I change the IP address?**: Normally IP addresses are assigned by the server known as DHCP. In some situations, | + | **Can I publish messages on different topics?**: Certainly |
| + | **My MQTT client disconnects randomly**: The most common reason | ||
| + | **Do I need to authorise to publish and subscribe? | ||
| <WRAP noprint> | <WRAP noprint> | ||