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_1 [2024/04/25 07:51] – [STM_IoT_1: Reading MAC address of the WiFi] ktokarzen:iot-open:practical:hardware:sut:stm32:iot_1 [2024/04/27 08:15] (current) – [Result validation] ktokarz
Line 1: Line 1:
 ====== STM_IoT_1: Reading MAC address of the WiFi ===== ====== STM_IoT_1: Reading MAC address of the WiFi =====
 Each network card is supposed to have a unique physical address called a MAC address. MAC abbreviation stands for Medium Access Control protocol, which provides access to the physical link in the network layer. 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 additional ESP32-C3 module and controlled with AT commands. To learn how to use these commands please refer to the STM_IoT_Intro chapter. Each network card is supposed to have a unique physical address called a MAC address. MAC abbreviation stands for Medium Access Control protocol, which provides access to the physical link in the network layer. 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 additional ESP32-C3 module and controlled with AT commands. To learn how to use these commands please refer to the STM_IoT_Intro chapter.
-// +\\ 
-ESP32 chip has built-in MAC. MAC can be used to identify devices, but note that it is not a "strong" ID: it can be programmatically changed and easily discovered. In the following scenario, we only present how to read the MAC address. A part regarding displaying on the selected screen is up to the developer. You can refer to the appropriate scenario, as listed below.+ESP32 chip has built-in MAC. MAC can be used to identify devices, but note that it is not a "strong" ID: it can be programmatically changed and easily discovered. In the following scenario, we present how to read and display the MAC address on LCDDisplaying on other display than LCD is up to the developer. You can refer to the appropriate scenario, as listed below.
  
 ===== Prerequisites ===== ===== Prerequisites =====
 To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first: To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first:
-  * [[en:iot-open:practical:hardware:sut:esp32:emb5_1|]], +  * [[en:iot-open:practical:hardware:sut:stm32:IoT_AT]], 
-  * [[en:iot-open:practical:hardware:sut:esp32:emb6_1|]], +  * [[en:iot-open:practical:hardware:sut:stm32:emb5_1|]], 
-  * [[en:iot-open:practical:hardware:sut:esp32:emb7_1|]]+  * [[en:iot-open:practical:hardware:sut:stm32:emb6_1|]], 
- +  * [[en:iot-open:practical:hardware:sut:stm32:emb7_1|]].
-A WiFi library is already included in the Arduino framework for ESP32, so there is no need to add it to the ''platformio.ini''  explicitly.+
  
 ===== Suggested Readings and Knowledge Resources ===== ===== Suggested Readings and Knowledge Resources =====
   * [[en:iot-open:introductiontoembeddedprogramming2:cppfundamentals]],   * [[en:iot-open:introductiontoembeddedprogramming2:cppfundamentals]],
 +  * [[en:iot-open:hardware2:stm32|]],
   * [[en:iot-open:hardware2:esp32|]],   * [[en:iot-open:hardware2:esp32|]],
-  * [[en:iot-open:practical:hardware:sut:esp32|]],+  * [[en:iot-open:practical:hardware:sut:stm32|]],
   * [[en:iot-open:iotprogramming2:espressif_networking|]].   * [[en:iot-open:iotprogramming2:espressif_networking|]].
  
Line 21: Line 21:
  
 ==== Task to be implemented ==== ==== Task to be implemented ====
-Present a MAC address on the selected display. The steps below present only the reading part, not a display. Handling a display is presented in the EMBx scenarios, as listed above+Present a MAC address on the selected display. The steps below present the reading part and display it on LCDThis display has 16 characters per linebut 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. 
 +<note> 
 +The MAC addresses usually are expressed as six hexadecimal numbers separated by colons eg. "84:fc:e6:88:69:d5"
 +</note>
 ==== Start ==== ==== Start ====
 Check if you can see a full LCD in your video stream. Book a device and create a dummy Arduino file with ''void setup()...'' and ''void loop()...''. Check if you can see a full LCD in your video stream. Book a device and create a dummy Arduino file with ''void setup()...'' and ''void loop()...''.
- 
  
 ==== Steps ==== ==== Steps ====
  
 === Step 1 === === Step 1 ===
-Include the WiFi management library in your source code:+Include the LCD library in your source code:
 <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. 
  
 +Create objects of the LCD and Hardware Serial classes:
 +<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 === === Step 2 ===
-Reading the MAC as a ''String'' is as easy as simply calling:+In the Setup() function initialise the serial port, display, and strings.
 <code c> <code c>
-  WiFi.macAddress();+WiFiSerial.begin(115200)
 +lcd.begin(16, 2); 
 +compOK = "OK"; 
 +compERROR = "ERROR";
 </code> </code>
  
-==== Result validation ==== +We will use the code template from [[en:iot-open:practical:hardware:sut:stm32:IoT_AT]] to communicate with the WiFi module. The first command we will send "AT" to confirm that everything works properly: 
-Using another node should change the MAC readBook another device and discover its MAC.+<code c> 
 +WiFiSerial.println("AT"); 
 +lcd.setCursor(0,0); 
 +lcd.print("AT              "); 
 +do { 
 +  response WiFiSerial.readStringUntil(0x0A); 
 +  lcd.setCursor(0,1); 
 +  lcd.print(response); 
 +} while (!(response.startsWith(compOK)));
  
-===== FAQ ===== +delay(1000); 
-**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. You must first explicitly configure the ESP32 chip to work as an AP (Access Point, Server) or STA (WiFi Client) to do it. Sample stub code (for STA) may look as follows:+</code> 
 + 
 +The next command can be the "AT+CIPSTAMAC?" which returns the MAC address in the response message which looks like this:
 <code c> <code c>
-#include <WiFi.h> ++CIPSTAMAC:"84:fc:e6:88:63:d1" 
-#include <esp_wifi.h>+</code>
  
-uint8_t newMAC[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE}; //Array of bytes with new MAC +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 "AT" command, and add inside the code the following lines: 
-void setup() +<code c> 
-+if (response.startsWith("+CIPSTAMAC")) { 
-  WiFi.mode(WIFI_STA); +  response.remove(0,12); 
-  esp_wifi_set_mac(WIFI_IF_STA&newMAC[0]);+  lcd.setCursor(0,0); 
 +  lcd.print(response);
 } }
 </code> </code>
 +==== Result validation ====
 +You should be able to see the MAC address of the ESP32-C3 module.
 +<note>
 +Using another node should change the MAC read. Book another device and discover its MAC.
 +</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>
 +===== 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.
  
 <WRAP noprint> <WRAP noprint>
en/iot-open/practical/hardware/sut/stm32/iot_1.1714031498.txt.gz · Last modified: 2024/04/25 07:51 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