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_at [2024/04/25 09:53] – [Task to be implemented] ktokarzen:iot-open:practical:hardware:sut:stm32:iot_at [2024/04/27 08:14] (current) – [Result validation] ktokarz
Line 1: Line 1:
-====== STM_IoT_AT:  =====+====== STM_IoT_AT: Programming of the WiFi interface with AT commands =====
 The STM32WB55 SoC doesn't have a WiFi network controller so our STM laboratory stands have the WiFi module based on ESP32-C3 SoC connected by serial port and controlled with AT commands. In this scenario, you will learn how to use these commands. The STM32WB55 SoC doesn't have a WiFi network controller so our STM laboratory stands have the WiFi module based on ESP32-C3 SoC connected by serial port and controlled with AT commands. In this scenario, you will learn how to use these commands.
  
Line 17: Line 17:
  
 ==== Task to be implemented ==== ==== Task to be implemented ====
-This introductory scenario shows how to use AT commands to control the ESP32-C3 WiFi module connected to the STM laboratory stand. In this scenario, we will use simple AT commands and try to display if their execution finished properly. We will use the LCD to observe the behaviour of the device.+This introductory scenario shows how to use AT commands to control the ESP32-C3 WiFi module connected to the STM laboratory stand. In this scenario, we will use simple AT commands and try to display if their execution finished properly. We will use the LCD to observe the behaviour of the device. You can use this as the template for the single step of the procedure of establishing a WiFi connection.
  
 ==== Start ==== ==== Start ====
Line 24: Line 24:
  
 ==== Steps ==== ==== Steps ====
 +In this scenario, we just send one command and display the result. In further scenarios, you will learn how to connect to the WiFi access point, connect to the MQTT broker, subscribe to the MQTT topic, and publish the message on a specific topic.
  
 === Step 1 === === Step 1 ===
-Include the WiFi management library in your source code:+Include the LiquidCrystal library in your source code to control LCD:
 <code c> <code c>
-#include <WiFi.h>+#include "LiquidCrystal.h"
 </code> </code>
-The WiFi library automatically initialises a singleton class ''WiFi'' that you can use to set up working mode, read MAC, and perform many other operations.+
  
 === Step 2 === === Step 2 ===
-Reading the MAC as a ''String'' is as easy as simply calling:+We will connect to the ESP32-C3 WiFi module with hardware serial port. We need to instantiate the object of the ''HardwareSerial'' class. The ''LiquidCrystal'' object allows us to display data on LCD. 
 <code c> <code c>
-  WiFi.macAddress();+// Pins definition for Hardware Serial 
 +#define RxD_PIN   PC_0  //STM numbering 
 +#define TxD_PIN   PC_1  //STM numbering 
 +HardwareSerial WiFiSerial(RxD_PIN, TxD_PIN, NC, NC); 
 + 
 +// LCD class 
 +const int rs = PC5, en = PB11, d4 = PB12, d5 = PB13, d6 = PB14, d7 = PB15; 
 +LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
 </code> </code>
  
-==== Result validation ==== +We will also declare two strings for comparison with the responses from the WiFi module 
-Using another node should change the MAC readBook another device and discover its MAC.+<code c> 
 +String compOK; 
 +String compERROR; 
 +</code> 
 +=== Step 3 === 
 +In the ''setup();'' function we need to initialise both devices: 
 +<code c> 
 +WiFiSerial.begin(115200); 
 +lcd.begin(16, 2); 
 +</code> 
 +And set the two strings for comparison: 
 +<code c> 
 +compOK = "OK"; 
 +compERROR = "ERROR"; 
 +</code>
  
-===== FAQ ===== +=== Step 4 === 
-**Can I change MAC?**: Actuallyyes, you can. It is not advised, however, because you may accidentally generate an overlapping address that will collide with another device in the same networkYou must first explicitly configure the ESP32 chip to work as an AP (Access Point, Server) or STA (WiFi Client) to do itSample stub code (for STA) may look as follows:+The AT commands are sent via a serial port connected to the ESP32-C3 module. At the beginningwe can send an empty command - "AT"The module should respond "OK" if everything works properly or "ERROR" if something goes wrong
 <code c> <code c>
-#include <WiFi.h> +// Display the message that the software has started 
-#include <esp_wifi.h>+lcd.setCursor(0,0); 
 +lcd.print("Started"); 
 +delay(1000);
  
-uint8_t newMAC[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE}; //Array of bytes with new MAC +// Send the "AT" command to the module 
-void setup() +WiFiSerial.println("AT"); 
-+ 
-  WiFi.mode(WIFI_STA); +// Display the command on 1-st line of LCD 
-  esp_wifi_set_mac(WIFI_IF_STA, &newMAC[0]); +lcd.setCursor(0,0); 
-}+lcd.print("AT              "); 
 +do { 
 + 
 +  // Read the response till the LF character 
 +  response = WiFiSerial.readStringUntil(0x0A); 
 +   
 +  // Display the response on 2-nd line of LCD 
 +  lcd.setCursor(0,1); 
 +  lcd.print(response); 
 +   
 +  // Repeat until "OK" comes 
 +while (!(response.startsWith(compOK)));
 </code> </code>
 +
 +This code allows us to observe what AT command has been sent and all the responses. It waits until the "OK" comes which is the text sent by the module after every successful command. If something goes wrong we will see the AT command on the first line and "ERROR" on the second line of LCD, and the program stops. This can help with debugging.
 +<note>
 +You can use this part of the code in the following scenarios for every AT command to observe their behaviour and easily debug possible errors.
 +</note>
 +
 +==== Result validation ====
 +You should be able to see the "AT" displayed on the upper line of the LCD, and "OK" on the lower line of the LCD. If any error in the communication with the ESP32-C3 module occurs the second line shows "ERROR".
 +<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.
 +</note>
 +
 +===== FAQ =====
 +**Do I need to write the program for the WiFi module?** No you don't have to. The ESP32-C3 module is flashed with the firmware from Espressif which implements all functionality for WiFi and MQTT. There are other modules which offer similar functionality.\\
 +**Can I use a similar WiFi module in my own IoT projects?** Certainly! The universal serial port connects the module so the module can be used with almost every microcontroller as the WiFi network controller.\\
 +**Are AT commands the same for all modules available?** The basing AT command set is generally the same, but advanced functionality such as WiFi, MQTT and others can use different syntax for modules from different vendors. Always refer to the original documentation for details.
  
 <WRAP noprint> <WRAP noprint>
en/iot-open/practical/hardware/sut/stm32/iot_at.1714038809.txt.gz · Last modified: 2024/04/25 09:53 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