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:esp32:iot_2 [2024/04/10 12:25] – [Steps] pczekalskien:iot-open:practical:hardware:sut:esp32:iot_2 [2024/04/21 11:39] (current) pczekalski
Line 1: Line 1:
-<todo @pczekalski>Finish IOT2 scenario</todo> 
 ====== IOT2: Reading IP address of the WiFi ===== ====== IOT2: Reading IP address of the WiFi =====
 On the networking level, IP devices are identified by MAC. In the case of our laboratory and network, you will obtain the IP address from the DHCP server integrated with the WiFi access point. To connect to the WiFi network, one needs to use credentials that are present in the general laboratory description, available here:  On the networking level, IP devices are identified by MAC. In the case of our laboratory and network, you will obtain the IP address from the DHCP server integrated with the WiFi access point. To connect to the WiFi network, one needs to use credentials that are present in the general laboratory description, available here: 
Line 22: Line 21:
  
 ==== Task to be implemented ==== ==== Task to be implemented ====
-Connect to the "internal IoT" WiFI access point. Present the obtained IP address on the selected display. Handle critical situations such as no access and present an appropriate message on the screen (e.g. error message). The steps below present only the reading part, not a display. Handling a display is presented in the EMBx scenarios, as listed above.+Connect to the "internal IoT" WiFI access point. Present the obtained IP address on the selected display. Handle critical situations such as no access and present an appropriate message on the screen (e.g., an error message). The steps below present only the reading part, not a display. Handling a display is presented in the EMBx scenarios, as listed above.
  
 ==== 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 clearly see a full display (of your choice) in your video stream. Book a device and create a dummy Arduino file with ''void setup()...'' and ''void loop()...''.
  
  
Line 38: Line 37:
  
 === Step 2 === === Step 2 ===
-Declare some constants, including AP SSID and passphrase:+Declare some constants, including AP SSID and passphrase and a variable to store IP:
 <code c> <code c>
 const char* ssid = "REPLACE_WITH_CORRECT_SSID"; const char* ssid = "REPLACE_WITH_CORRECT_SSID";
 const char* pass = "REPLACE_WITH_CORRENT_PASSPHRASE"; const char* pass = "REPLACE_WITH_CORRENT_PASSPHRASE";
 +IPAddress localIP;
 </code> </code>
  
-Both ''ssid'' and ''pass'' are to be obtained from the hardware reference, available here:  +Both ''ssid'' and ''pass'' are to be obtained from the hardware reference, available here: [[en:iot-open:practical:hardware:sut:esp32|]]. 
-<todo @pczekalski>Add reference to the hardware</todo>+
  
 === Step 3 === === Step 3 ===
-Set your device in the STA mode:+Set your device in the STA mode and connect to the WiFi AP:
 <code c> <code c>
   WiFi.mode(WIFI_STA);   WiFi.mode(WIFI_STA);
 +  WiFi.begin(ssid, pass);
 +  while (WiFi.status() != WL_CONNECTED) {
 +    //Drop some info on display about connecting
 +    delay(1000);
 +  }
 </code> </code>
 +
 +The ''WiFi.begin(...)'' returns the following statuses (value, enumerator: meaning):
 +  * 0, ''WL_IDLE_STATUS:'' temporary status assigned when WiFi.begin() is called
 +  * 1, ''WL_NO_SSID_AVAIL:'' when no SSID are available
 +  * 2, ''WL_SCAN_COMPLETED:'' scan networks is completed
 +  * 3, ''WL_CONNECTED:'' when connected to a WiFi network
 +  * 4, ''WL_CONNECT_FAILED:'' when the connection fails for all the attempts
 +  * 5, ''WL_CONNECTION_LOST:'' when the connection is lost
 +  * 6, ''WL_DISCONNECTED:'' when disconnected from a network
  
 === Step 4 === === Step 4 ===
 Reading the IP as a ''String'' is as easy as simply calling: Reading the IP as a ''String'' is as easy as simply calling:
 <code c> <code c>
-  WiFi.macAddress();+   localIP = WiFi.localIP();
 </code> </code>
 +The ''IPAddress'' class contains the ''toString()'' method which formats the IP and returns in standard, dot-separated, four-octet text format.
  
 +=== Step 5 ===
 +Explicitly disconnect from the WiFi AP to free resources:
 +<code c>
 +  WiFi.disconnect();  
 +</code>
 +
 +Some useful WiFi functions are listed below:
 +  * ''WiFi.reconnect()'' - reconnects the connection that was dropped, note it uses ''ssid'' and ''pass'' previously specified in the ''WiFi.begin(...)'', here you do not provide one. 
 +  * ''WiFi.RSSI()'' - once connected, you can get signal strength as ''int8_t'' integer.
 +  * ''WiFi.setHostname(const char * hostname)'' - set host name for the ESP32 chip. Remember to use this function before connecting to the AP.
 ==== Result validation ==== ==== Result validation ====
-Using another node should change the MAC read. Book another device and discover its MAC.+You should be able to connect to the WiFi and present the dynamically assigned IP address by the DHCP server. <note>Note that due to the dynamic nature of the lab, IP addresses can change both during connection (on lease refresh) or between consecutive connects.</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. You must first explicitly configure the ESP32 chip to work as an AP (Access PointServer) or STA (WiFi Client) to do it. Sample stub code (for STA) may look as follows: +**I set a hostname, but it does appear on the router level.**: There are many possible reasons; one is an active registration in the router (AP) that keeps an old IP address, so you need to wait until it expires; another reason is you have changed the hostname when you were connected to the AP.\\ 
-<code c> +**Can I use a manually configured IP?**: Actually, you can, but we strongly discourage itYou may accidentally overlap this IP address with some other device, router, or infrastructureblocking its operation.
-#include <WiFi.h> +
-#include <esp_wifi.h> +
- +
-uint8_t newMAC[] = {0xDE0xAD, 0xBE, 0xEF, 0xCA, 0xFE}; //Array of bytes with new MAC +
-void setup() +
-+
-  WiFi.mode(WIFI_STA); +
-  esp_wifi_set_mac(WIFI_IF_STA, &newMAC[0]); +
-+
-</code>+
  
 <WRAP noprint> <WRAP noprint>
en/iot-open/practical/hardware/sut/esp32/iot_2.1712751946.txt.gz · Last modified: 2024/04/10 12:25 by pczekalski
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