No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:aih:lab:stm32mqtt
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:edu:zmitac:s2:aih:lab:stm32mqtt [2024/01/15 10:32] – created ktokarz | en:edu:zmitac:s2:aih:lab:stm32mqtt [2024/01/17 14:46] (current) – ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== AT commands controlled WiFi module ====== | ====== AT commands controlled WiFi module ====== | ||
| - | The STM32WB55 microcontroller does not have the WiFi module built-in. There isn't also any additional WiFi-enabling element on the Nucleo board. The laboratory stand is equipped with a module based on ESP32-C3 SoC - super mini. It works as a WiFi modem connected | + | The STM32WB55 microcontroller does not have the WiFi module built-in. There isn't also any additional WiFi-enabling element on the Nucleo board. The laboratory stand is equipped with a module based on ESP32-C3 SoC - super mini. It works as a WiFi modem connected |
| <code c> | <code c> | ||
| // Serial port configuration (Constructor uses STM port numbering) | // Serial port configuration (Constructor uses STM port numbering) | ||
| Line 6: | Line 6: | ||
| </ | </ | ||
| < | < | ||
| - | HardwareSerial class object constructor uses STM-like pin numbering, not Arduino numbering. | + | HardwareSerial class object constructor uses STM-like pin numbering, not Arduino numbering. Pins for RxD and TxD lines are specified. Because no hardware flow control is used the lines RTS and CTS remain unconnected. |
| </ | </ | ||
| In the setup function, the serial port should be opened and the bitrate specified. | In the setup function, the serial port should be opened and the bitrate specified. | ||
| Line 14: | Line 14: | ||
| </ | </ | ||
| < | < | ||
| - | ESP32-C3 working as a WiFi modem uses 115200 bps and 8N1 frame format (8 bits of data, no parity control, one stop bit). No hardware flow control is used so RTS and CTS lines remain unconnected. | + | ESP32-C3 working as a WiFi modem uses 115200 bps and 8N1 frame format (8 bits of data, no parity control, one stop bit). |
| </ | </ | ||
| + | The WiFi modem can connect to the wireless network by using AT commands. The list of all supported commands is available on the Espessif website ((https:// | ||
| + | < | ||
| + | AT | ||
| + | |||
| + | OK | ||
| + | </ | ||
| + | If the command is wrong, contains errors, or is not allowed in the actual mode of the modem' | ||
| + | The execution time of commands can be quite long, up to a few seconds, especially if commands rely on other devices, eg. establishing the connection to the WiFi Access Point or receiving data from a remote server. It is always a good way to wait for " | ||
| + | An example of the code that sends an " | ||
| + | <code c> | ||
| + | String response; | ||
| + | String compOK; | ||
| + | |||
| + | compOK = " | ||
| + | WiFiSerial.println(" | ||
| + | do { | ||
| + | response = WiFiSerial.readStringUntil(0x0A); | ||
| + | } while (!(response.startsWith(compOK))); | ||
| + | </ | ||
| + | The sequence of commands that establishes the connection to the WiFi Access Point contains two of them. | ||
| + | < | ||
| + | AT+CWMODE=1 | ||
| + | AT+CWJAP=" | ||
| + | </ | ||
| + | It is possible to list all available APs by using the command: | ||
| + | < | ||
| + | AT+CWLAP | ||
| + | </ | ||
| + | After connecting to a wireless network we can connect to the MQTT broker. ESP32-C3 supports MQTT protocol with some specialized commands. In all MQTT commands, the first parameter is the ID of the connection, it should be 0. Configuring the user data can be done with the following command: | ||
| + | < | ||
| + | AT+MQTTUSERCFG=0, | ||
| + | </ | ||
| + | MQTT_node_name should be the unique name of the device. MQTT_user and MQTT_password are the credentials to connect to the broker if required. | ||
| + | Assuming that the broker is available at IP address 192.168.1.100 and uses the default TCP port the connection with the broker can be established with the command: | ||
| + | < | ||
| + | AT+MQTTCONN=0," | ||
| + | </ | ||
| + | < | ||
| + | The last parameter is the reconnect flag. If 1 the modem will reconnect automatically after the connection with the broker is lost, if 0 it will not reconnect automatically. | ||
| + | </ | ||
| + | After a successful connection to the MQTT broker, we can subscribe to the chosen topic or publish messages. | ||
| + | < | ||
| + | AT+MQTTSUB=0," | ||
| + | AT+MQTTPUB=0," | ||
| + | </ | ||
| + | < | ||
| + | The third parameter of the subscribe command as well as the fourth parameter of the publish command is the QoS. | ||
| + | The fifth parameter of the publish command is the retain flag. | ||
| + | Please refer to the MQTT lesson for the details. | ||
| + | </ | ||
| + | If some other device publishes the data using the topic we subscribe the modem will send the information including the topic' | ||
| + | < | ||
| + | +MQTTSUBRECV: | ||
| + | </ | ||
| + | for example: | ||
| + | < | ||
| + | +MQTTSUBRECV: | ||
| + | </ | ||
en/edu/zmitac/s2/aih/lab/stm32mqtt.1705311159.txt.gz · Last modified: by ktokarz
