This is an old revision of the document!
[pczekalski]Finish IOT2 scenario
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.
To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first:
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.
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.
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()…
.
Include the WiFi management library in your source code:
#include <WiFi.h>
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.
Declare some constants, including AP SSID and passphrase:
const char* ssid = "REPLACE_WITH_CORRECT_SSID"; const char* pass = "REPLACE_WITH_CORRENT_PASSPHRASE";
Both ssid
and pass
are to be obtained from the hardware reference, available here:
[pczekalski]Add reference to the hardware
Set your device in the STA mode:
WiFi.mode(WIFI_STA);
Reading the IP as a String
is as easy as simply calling:
WiFi.macAddress();
Using another node should change the MAC read. Book another device and discover its MAC.
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:
#include <WiFi.h> #include <esp_wifi.h> uint8_t newMAC[] = {0xDE, 0xAD, 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]); }