Differences

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

Link to this comparison view

Next revision
Previous revision
en:iot-open:practical:hardware:sut:stm32:iot_2 [2024/04/26 08:54] – created ktokarzen:iot-open:practical:hardware:sut:stm32:iot_2 [2024/04/27 08:15] (current) – [Result validation] ktokarz
Line 1: Line 1:
 ====== STM_IoT_2: Connecting to the WiFi Access Point and presenting IP ===== ====== STM_IoT_2: Connecting to the WiFi Access Point and presenting IP =====
-Each computer connected to the Internet is identified with the IP address. IP abbreviation stands for Internet Protocol, which is responsilbe to transmit data packets between computers in the whole global web - the Internet. The same mechanism is used to address and transmit packets among IP-capable IoT devices. The most popular local networks which support IP addressing are Ethernet and WiFi. The STM32WB55 SoC doesn't have Ethernet or WiFi network controller so our STM laboratory stands have the WiFi module based on ESP32-C3 SoC. It is connected by serial port and controlled with AT commandsTo learn how to use these commands please refer to the STM_IoT_Intro chapter.+Each computer connected to the Internet is identified with the IP address. IP abbreviation stands for Internet Protocol, which is responsible for transmitting data packets between computers in the whole global web - the Internet. The same mechanism is used for addressing and transmitting packets among IP-capable IoT devices. The most popular local networks which support IP addressing are Ethernet and WiFi. Currently, the transition from traditional IPv4 to the new IPv6 version is occurringOur laboratory supports IPv4.
 \\ \\
-ESP32 chip gets the IP address from DHCP server after establishing the connection wit FiFi access point. In this scenario, we present how to connect to WiFi networkread and display the IP address on LCD. Displaying on other display than LCD is up to the developer. You can refer to the appropriate scenario, as listed below.+The STM32WB55 SoC doesn't have Ethernet or WiFi network controller so our STM laboratory stands have the WiFi module based on ESP32-C3 SoC. It is connected by a serial port and controlled with AT commands. To learn how to use these commands please refer to the STM_IoT_Intro chapter. 
 +\\ 
 +ESP32 chip gets the IP address from the DHCP server after establishing the connection with the WiFi access point. In this scenario, we present how to connect to WiFi network and read and display the IP address on an LCD. Displaying on other display than LCD is up to the developer. You can refer to the appropriate scenario, as listed below.
  
 ===== Prerequisites ===== ===== Prerequisites =====
Line 21: Line 23:
  
 ==== Task to be implemented ==== ==== Task to be implemented ====
-Present a MAC address on the selected display. The steps below present the reading part and display it on LCD. This display has 16 characters per line, but the commonly used format for MAC addresses requires 17 characters. For seeing the full MAC address modify the example and use the display other than LCD.+Join the WiFi network. Present an IP address on the selected display. The commonly used format for IPv4 addresses requires 15 characters, so LCD having 16 characters is sufficient to present the address. The steps below show the starting part of the softwareHow to implement the full software please refer to the previous scenarios: 
 +  * [[en:iot-open:practical:hardware:sut:stm32:IoT_AT]] 
 +  * [[en:iot-open:practical:hardware:sut:stm32:IoT_1]]
 <note> <note>
-The MAC addresses usually are expressed as six hexadecimal numbers separated by colons eg. "84:fc:e6:88:69:d5".+The IPv4 addresses are usually expressed as four decimal numbers ranging from 0 to 255 separated by dots eg. "192.168.1.100".
 </note> </note>
 ==== Start ==== ==== Start ====
Line 31: Line 35:
  
 === Step 1 === === Step 1 ===
-Include the LCD library in your source code: +The beginning of the code is the same as in the previous scenario, so make a copy of it
-<code c> +  * [[en:iot-open:practical:hardware:sut:stm32:IoT_1]]
-#include "LiquidCrystal.h" +
-</code>+
  
-Create objects of the LCD and Hardware Serial classes: +We will use the code template from [[en:iot-open:practical:hardware:sut:stm32:IoT_AT]] repeated for every AT command
-<code c> +
-// Serial port class and configuration (Constructor uses STM port 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> +
- +
-Declare two variables with the strings to be compared with the responses from the WiFi module. +
-<code c> +
-String compOK; +
-String compERROR; +
-</code> +
-=== Step 2 === +
-In the Setup() function initialise the serial port, display, and strings. +
-<code c> +
-WiFiSerial.begin(115200); +
-lcd.begin(16, 2); +
-compOK = "OK"; +
-compERROR = "ERROR"; +
-</code> +
- +
-We will use the code template from [[en:iot-open:practical:hardware:sut:stm32:IoT_AT]] to communicate with the WiFi moduleThe first command we will send "AT" to confirm that everything works properly:+
 <code c> <code c>
 WiFiSerial.println("AT"); WiFiSerial.println("AT");
Line 73: Line 51:
 delay(1000); delay(1000);
 </code> </code>
 +=== 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.
 +<code c>
 +// Test if module is available
 +"AT"
 +
 +// Reset the module
 +"AT+RST"
 +
 +// Prevent from storing the WiFi join parameters in non-volatile memory
 +"AT+SYSSTORE=0"
 +
 +// Start module in station mode (which can join the access point)
 +"AT+CWMODE=1"
 +
 +// Join the access point. Use SSID and password.
 +"AT+CWJAP=\"SSID\",\"password\""
 +
 +// Get the IP address
 +"AT+CIPSTA?"
 +</code>
 +
 +<note>
 +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.
 +</note>
 +Some explanation can be needed for the "AT+RST" and "AT+SYSSTORE=0" commands. 
 +If we don't use the "AT+SYSSTORE=0" command 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+RST" command 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 next command can be the "AT+CIPSTAMAC?" which returns the MAC address in the response message which looks like this:+=== Step 3 === 
 +The command for receiving the IP address is "AT+CIPSTA?". It returns in the response message the IP address, IP address of the gateway and IP address mask:
 <code c> <code c>
-+CIPSTAMAC:"84:fc:e6:88:63:d1"++CIPSTA:ip:"192.168.1.117" 
 ++CIPSTA:gateway:"192.168.1.1" 
 ++CIPSTA:netmask:"255.255.255.0"
 </code> </code>
  
-We can implement the part of displaying the MAC by repeating the block of the code similar to the one presented above with two modifications. Change the "ATcommand, and add inside the code the following lines:+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> <code c>
-if (response.startsWith("+CIPSTAMAC")) {+if (response.startsWith("+CIPSTA:ip:")){
   response.remove(0,12);   response.remove(0,12);
   lcd.setCursor(0,0);   lcd.setCursor(0,0);
Line 88: Line 97:
 </code> </code>
 ==== Result validation ==== ==== Result validation ====
-You should be able to see the MAC address of the ESP32-C3 module.+You should be able to see the IP address of the ESP32-C3 module.
 <note> <note>
-Using another node should change the MAC read. Book another device and discover its MAC.+Using another node ar even the same node another time can change the IP read. You can book another device and discover its IP. 
 +</note> 
 +<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> </note>
  
 ===== FAQ ===== ===== FAQ =====
-**Can I change MAC?**: Actually, yes, you can. It is not advised, however, because you may accidentally generate an overlapping address that will collide with another device in the same network.+**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 station. It is not advised, however, because you may accidentally generate an overlapping address that will collide with another device in the same network.
  
 <WRAP noprint> <WRAP noprint>
en/iot-open/practical/hardware/sut/stm32/iot_2.1714121660.txt.gz · Last modified: 2024/04/26 08:54 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