Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:practical:hardware:sut:stm32:iot_3 [2024/04/26 12:44] – [Task to be implemented] ktokarzen:iot-open:practical:hardware:sut:stm32:iot_3 [2024/04/27 09:42] (current) ktokarz
Line 56: Line 56:
 delay(1000); delay(1000);
 </code> </code>
 +
 === Step 2 === === Step 2 ===
-The procedure of connecting to the WiFi requires some steps. Below we present the AT commands only, your task is to implement the full code which sends the command and waits for the response for each command. +In the firmware for the ESP32-C3 board, there are AT commands to work with the MQTT protocolIn this scenario we will use three of them:
-<code c> +
-// Test if module is available +
-"AT"+
  
-// Reset the module +  * "AT+MQTTUSERCFG..." - to configure MQTT c 
-"AT+RST"+  * "AT+MQTTCONN..."    - to establish the connection to the broker 
 +  "AT+MQTTPUB...    - to publish the message on the topic
  
-// Prevent from storing the WiFi join parameters in non-volatile memory +Below we briefly describe the commands mentioning only the parameters important to us. Please refer to the Espressif documentation((https://docs.espressif.com/projects/esp-at/en/latest/esp32c3/index.html)) for the details of commands. 
-"AT+SYSSTORE=0"+<note> 
 +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. 
 +</note> 
 +**AT+MQTTUSERCFG** 
 +This command accepts the list of parameters: 
 +<code> 
 +AT+MQTTUSERCFG=<LinkID>,<scheme>,<"client_id">,<"username">,<"password">,<cert_key_ID>,<CA_ID>,<"path"> 
 +</code> 
 +  * LinkID - currently should be 0 
 +  * scheme - 1 (MQTT over TCP) 
 +  * "client_id" - the unique ID of the MQTT client 
 +  * "username" - user to connect to the broker 
 +  * "password" - password for the user 
 +  * cert_key-ID - should be 0 
 +  * CA_ID - should be 0 
 +  * "path" - should remain empty
  
-// Start module in station mode (which can join the access point) +In our case, the command can look like this: 
-"AT+CWMODE=1"+<code> 
 +AT+MQTTUSERCFG=0,1,\"STM32#0001\",\"username\",\"password\",0,0,\"\" 
 +</code>
  
-// Join the access point. Use SSID and password. +**AT+MQTTCONN** 
-"AT+CWJAP=\"SSID\",\"password\""+This command accepts the list of parameters: 
 +<code> 
 +AT+MQTTCONN=<LinkID>,<"host">,<port>,<reconnect> 
 +</code> 
 +  * LinkID - currently should be 0 
 +  * "host- the IP address or URL of the MQTT broker 
 +  * 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?"+<code> 
 +AT+MQTTCONN=0,\"192.168.1.100\",1883,1
 </code> </code>
  
-<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=<LinkID>,<"topic">,<"data">,<qos>,<retain> 
-Some explanation can be needed for the "AT+RST" and "AT+SYSSTORE=0" commands.  +</code
-If we don't use the "AT+SYSSTORE=0command our module stores the WiFi credentials in non-volatile memory and automatically connects to the network after powering on. In our laboratory, it is not recommended because the next commands can return "ERROR" if we try to connect the network and MQTT broker if we are already connected. + 
-If we don't use the "AT+RSTcommand our module will keep the WiFi connection active, even if we upload the new version of the software. It would result in the same error in the case of WiFi or MQTT reconnecting.+The list of parameters: 
 +  * LinkID - currently should be 0 
 +  "topic" - MQTT topic 
 +  "data- payload of the message 
 +  * qos - mode of the quality of service, 0, 1 or 2, default 0. 
 +  * retain - retain flag, 0 or 1.
  
 === Step 3 === === Step 3 ===
-The command for receiving the IP address is "AT+CIPSTA?"It returns in the response message the IP addressIP address of the gateway and IP address mask: +Implement the MQTT configuration and connection to the broker. Use the template for the "AT" command from the first stepEnsure that your node is successfully connected. 
-<code c> + 
-+CIPSTA:ip:"192.168.1.117" +=== Step 4 === 
-+CIPSTA:gateway:"192.168.1.1" +Implement publishing of the MQTT message. You can do it just once in the "setup();" functionor periodically in the "loop();"While working periodically be sure not to send messages too oftenReasonable period is 1-5 secondsYou can send the content of the counter to observe value changes in the concurrent messages.
-+CIPSTA:netmask:"255.255.255.0" +
-</code>+
  
-While LCD use we have to filter out unwanted elements of the response. We can do it by displaying the answer containing "+CIPSTA:IP:" only removing the first 12 characters. Look into the following code: 
-<code c> 
-if (response.startsWith("+CIPSTA:ip:")){ 
-  response.remove(0,12); 
-  lcd.setCursor(0,0); 
-  lcd.print(response); 
-} 
-</code> 
 ==== 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:practical:hardware:sut:stm32:iot_4|]]) or use MQTT Explorer (or any other application capable of connecting to our MQTT Broker) to observe messages that you publish
-<note> +<note info
-Using another node ar even the same node another time can change the IP readYou can book another device and discover its IP.+Because LCD can't properly display some non-visible characters the presented code sometimes shows additional, non-letter charactersIt is out of the scope of this scenario to filter these characters out. We leave the task of making visual improvements to your invention.
 </note> </note>
  
 ===== FAQ ===== ===== FAQ =====
-**Can I change the IP address?**: Normally IP addresses are assigned by the server known as DHCP. In some situations, you can use static IP assigned manually in the stationIt is not advised, however, because you may accidentally generate an overlapping address that will collide with another device in the same network.+**Can I publish messages on different topics?**: Certainly you can. You can publish the readings of the temperature sensor with one topic, and readings of the humidity with anotherThe limit of the number of different topics comes only from the available resources of the MQTT broker.\\ 
 +**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.\\ 
 +**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>
en/iot-open/practical/hardware/sut/stm32/iot_3.1714135466.txt.gz · Last modified: 2024/04/26 12:44 by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0