====== 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: In this scenario, you will learn how to set up your device in STA (client to the AP) mode, connect to the AP, and read the obtained IP address. This scenario does not contain a UI part; it is up to the developer to implement it using one of the scenarios presented below. ===== Prerequisites ===== 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:esp32:emb6_1|]], * [[en:iot-open:practical:hardware:sut:esp32: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 ===== * [[en:iot-open:introductiontoembeddedprogramming2:cppfundamentals]], * [[en:iot-open:hardware2:esp32|]], * [[en:iot-open:practical:hardware:sut:esp32|]], * [[en:iot-open:iotprogramming2:espressif_networking|]]. ===== Hands-on Lab Scenario ===== ==== 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., 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 ==== 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()...''. ==== Steps ==== === Step 1 === Include the WiFi management library in your source code: #include The WiFi library automatically initialises a singleton class, ''WiFi,'' which you can use to set up working mode, read MAC and IP, and perform many other operations. === Step 2 === Declare some constants, including AP SSID and passphrase and a variable to store IP: const char* ssid = "REPLACE_WITH_CORRECT_SSID"; const char* pass = "REPLACE_WITH_CORRENT_PASSPHRASE"; IPAddress localIP; Both ''ssid'' and ''pass'' are to be obtained from the hardware reference, available here: [[en:iot-open:practical:hardware:sut:esp32|]]. === Step 3 === Set your device in the STA mode and connect to the WiFi AP: WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { //Drop some info on display about connecting delay(1000); } 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 === Reading the IP as a ''String'' is as easy as simply calling: localIP = WiFi.localIP(); 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: WiFi.disconnect(); 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 ==== You should be able to connect to the WiFi and present the dynamically assigned IP address by the DHCP server. Note that due to the dynamic nature of the lab, IP addresses can change both during connection (on lease refresh) or between consecutive connects. ===== FAQ ===== **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.\\ **Can I use a manually configured IP?**: Actually, you can, but we strongly discourage it. You may accidentally overlap this IP address with some other device, router, or infrastructure, blocking its operation. ===== Project information ===== {{:en:iot-open:logo_iot_200_px.png?200|}}\\ This Intellectual Output was implemented under the Erasmus+ KA2.\\ Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, companies and labour force in the global IoT market.\\ Project number: 2022-1-PL01-KA220-HED-000085090. **__Erasmus+ Disclaimer__**\\ This project has been funded with support from the European Commission. \\ This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein. **__Copyright Notice__**\\ This content was created by the IOT-OPEN.EU Reloaded consortium, 2022,2024.\\ The content is Copyrighted and distributed under CC BY-NC [[https://en.wikipedia.org/wiki/Creative_Commons_license|Creative Commons Licence]], free for Non-Commercial use.
{{:en:iot-open:ccbync.png?100|}}