Wifi disconnect arduino
Copy raw contents
The number of features provided by ESP8266 in the station mode is far more extensive than covered in original Arduino WiFi library. Therefore, instead of supplementing original documentation, we have decided to write a new one from scratch.
Description of station class has been broken down into four parts. First discusses methods to establish connection to an access point. Second provides methods to manage connection like e.g. reconnect or isConnected . Third covers properties to obtain information about connection like MAC or IP address. Finally the fourth section provides alternate methods to connect like e.g. Wi-Fi Protected Setup (WPS).
Table of Contents
Points below provide description and code snippets how to use particular methods.
For more code samples please refer to separate section with examples dedicated specifically to the Station Class.
Switching the module to Station mode is done with begin function. Typical parameters passed to begin include SSID and password, so module can connect to specific Access Point.
By default, ESP will attempt to reconnect to Wi-Fi network whenever it is disconnected. There is no need to handle this by separate code. A good way to simulate disconnection would be to reset the access point. ESP will report disconnection, and then try to reconnect automatically.
There are several versions (called function overloads in C++) of begin function. One was presented just above: WiFi.begin(ssid, password) . Overloads provide flexibility in number or type of accepted parameters.
The simplest overload of begin is as follows:
Calling it will enable station mode and connect to the last used access point based on configuration saved in flash memory.
- It is possible that calling begin will result in the module being in STA + softAP mode if the module was previously placed into AP mode.
- If you notice strange behavior with DNS or other network functionality, check which mode your module is in (see WiFi.mode() in the Generic Class Documentation).
Below is the syntax of another overload of begin with the all possible parameters:
Meaning of parameters is as follows:
- ssid — a character string containing the SSID of Access Point we would like to connect to, may have up to 32 characters
- password to the access point, a character string that should be minimum 8 characters long and not longer than 64 characters
- channel of AP, if we like to operate using specific channel, otherwise this parameter may be omitted
- bssid — mac address of AP, this parameter is also optional
- connect — a boolean parameter that if set to false , will instruct module just to save the other parameters without actually establishing connection to the access point
Disable DHCP client (Dynamic Host Configuration Protocol) and set the IP configuration of station interface to user defined arbitrary values. The interface will be a static IP configuration instead of values provided by DHCP.
Function will return true if configuration change is applied successfully. If configuration can not be applied, because e.g. module is not in station or station + soft access point mode, then false will be returned.
The following IP configuration may be provided:
- local_ip — enter here IP address you would like to assign the ESP station’s interface
- gateway — should contain IP address of gateway (a router) to access external networks
- subnet — this is a mask that defines the range of IP addresses of the local network
- dns1 , dns2 — optional parameters that define IP addresses of Domain Name Servers (DNS) that maintain a directory of domain names (like e.g. www.google.co.uk) and translate them for us to IP addresses
Please note that station with static IP configuration usually connects to the network faster. In the above example it took about 500ms (one dot . displayed). This is because obtaining of IP configuration by DHCP client takes time and in this case this step is skipped. If you pass all three parameter as 0.0.0.0 (local_ip, gateway and subnet), it will re enable DHCP. You need to re-connect the device to get new IPs.
Reconnect the station. This is done by disconnecting from the access point an then initiating connection back to the same AP.
Notes: 1. Station should be already connected to an access point. If this is not the case, then function will return false not performing any action. 2. If true is returned it means that connection sequence has been successfully started. User should still check for connection status, waiting until WL_CONNECTED is reported:
Sets currently configured SSID and password to null values and disconnects the station from an access point.
The wifioff is an optional boolean parameter. If set to true , then the station mode will be turned off.
Returns true if Station is connected to an access point or false if not.
Configure module to automatically connect on power on to the last used access point.
The autoConnect is an optional parameter. If set to false then auto connection functionality up will be disabled. If omitted or set to true , then auto connection will be enabled.
This is «companion» function to setAutoConnect() . It returns true if module is configured to automatically connect to last used access point on power on.
If auto connection functionality is disabled, then function returns false .
Set whether module will attempt to reconnect to an access point in case it is disconnected.
If parameter autoReconnect is set to true , then module will try to reestablish lost connection to the AP. If set to false then module will stay disconnected.
Note: running setAutoReconnect(true) when module is already disconnected will not make it reconnect to the access point. Instead reconnect() should be used.
Wait until module connects to the access point. This function is intended for module configured in station or station + soft access point mode.
Function returns one of the following connection statuses:
- WL_CONNECTED after successful connection is established
- WL_NO_SSID_AVAIL in case configured SSID cannot be reached
- WL_CONNECT_FAILED if connection failed
- WL_CONNECT_WRONG_PASSWORD if password is incorrect
- WL_IDLE_STATUS when Wi-Fi is in process of changing between statuses
- WL_DISCONNECTED if module is not configured in station mode
- -1 on timeout
Get the MAC address of the ESP station’s interface.
Function should be provided with mac that is a pointer to memory location (an uint8_t array the size of 6 elements) to save the mac address. The same pointer value is returned by the function itself.
If you do not feel comfortable with pointers, then there is optional version of this function available. Instead of the pointer, it returns a formatted String that contains the same mac address.
Function used to obtain IP address of ESP station’s interface.
The type of returned value is IPAddress. There is a couple of methods available to display this type of data. They are presented in examples below that cover description of subnetMask , gatewayIP and dnsIP that return the IPAdress as well.
Get the subnet mask of the station’s interface.
Module should be connected to the access point to obtain the subnet mask.
ESP32 Useful Wi-Fi Library Functions (Arduino IDE)
This article is a compilation of useful Wi-Fi functions for the ESP32. We’ll cover the following topics: scan Wi-Fi networks, connect to a Wi-Fi network, get Wi-Fi connection strength, check connection status, reconnect to the network after a connection is lost, Wi-Fi status, Wi-Fi modes, get the ESP32 IP address, set a fixed IP address and more.
This is not a novelty. There are plenty of examples of how to handle Wi-Fi with the ESP32. However, we thought it would be useful to compile some of the most used and practical Wi-Fi functions for the ESP32.
Table of Contents
Here’s a list of what will be covered in this tutorial (you can click on the links to go to the corresponding section):
Including the Wi-Fi Library
The first thing you need to do to use the ESP32 Wi-Fi functionalities is to include the WiFi.h library in your code, as follows:
This library is automatically “installed” when you install the ESP32 add-on in your Arduino IDE. If you don’t have the ESP32 installed, you can follow the next tutorial:
If you prefer to use VS Code + PaltformIO, you just need to start a new project with an ESP32 board to be able to use the WiFi.h library and its functions.
ESP32 Wi-Fi Modes
The ESP32 board can act as Wi-Fi Station, Access Point or both. To set the Wi-Fi mode, use WiFi.mode() and set the desired mode as argument:
|WiFi.mode(WIFI_STA)||station mode: the ESP32 connects to an access point|
|WiFi.mode(WIFI_AP)||access point mode: stations can connect to the ESP32|
|WiFi.mode(WIFI_STA_AP)||access point and a station connected to another access point|
When the ESP32 is set as a Wi-Fi station, it can connect to other networks (like your router). In this scenario, the router assigns a unique IP address to your ESP board. You can communicate with the ESP using other devices (stations) that are also connected to the same network by referring to the ESP unique IP address.
Set the ESP32 as a Station and Connect to Wi-Fi Network
Go to “Connect to Wi-Fi Network” to learn how to set the ESP32 as station and connect it to a network.
In some cases, this might not be the best configuration – when you don’t have a network nearby and want you still want to connect to the ESP to control it. In this scenario, you must set your ESP board as an access point.
When you set your ESP32 board as an access point, you can be connected using any device with Wi-Fi capabilities without connecting to your router. When you set the ESP32 as an access point, you create its own Wi-Fi network, and nearby Wi-Fi devices (stations) can connect to it, like your smartphone or computer. So, you don’t need to be connected to a router to control it.
This can be also useful if you want to have several ESP32 devices talking to each other without the need for a router.
Because the ESP32 doesn’t connect further to a wired network like your router, it is called soft-AP (soft Access Point). This means that if you try to load libraries or use firmware from the internet, it will not work. It also doesn’t work if you make HTTP requests to services on the internet to publish sensor readings to the cloud or use services on the internet (like sending an email, for example).
Set the ESP32 as an Access Point
To set the ESP32 as an access point, set the Wi-Fi mode to access point:
And then, use the softAP() method as follows:
ssid is the name you want to give to the ESP32 access point, and the password variable is the password for the access point. If you don’t want to set a password, set it to NULL .
There are also other optional parameters you can pass to the softAP() method. Here are all the parameters:
- ssid : name for the access point – maximum of 63 characters;
- password : minimum of 8 characters; set to NULL if you want the access point to be open;
- channel : Wi-Fi channel number (1-13)
- ssid_hidden : (0 = broadcast SSID, 1 = hide SSID)
- max_connection : maximum simultaneous connected clients (1-4)
We have a complete tutorial explaining how to set up the ESP32 as an access point:
Wi-Fi Station + Access Point
The ESP32 can be set as a Wi-Fi station and access point simultaneously. Set its mode to WIFI_AP_STA .
Scan Wi-Fi Networks
The ESP32 can scan nearby Wi-Fi networks within its Wi-Fi range. In your Arduino IDE, go to File > Examples > WiFi > WiFiScan. This will load a sketch that scans Wi-Fi networks within the range of your ESP32 board.
This can be useful to check if the Wi-Fi network you’re trying to connect is within the range of your board or other applications. Your Wi-Fi project may not often work because it may not be able to connect to your router due to insufficient Wi-Fi strength.
Here’s the example:
You can upload it to your board and check the available networks as well as the RSSI (received signal strength indicator).
WiFi.scanNetworks() returns the number of networks found.
After the scanning, you can access the parameters about each network.
WiFi.SSID() prints the SSID for a specific network:
WiFi.RSSI() returns the RSSI of that network. RSSI stands for Received Signal Strength Indicator. It is an estimated measure of power level that an RF client device is receiving from an access point or router.
Finally, WiFi.encryptionType() returns the network encryption type. That specific example puts a * in the case of open networks. However, that function can return one of the following options (not just open networks):
Connect to a Wi-Fi Network
To connect the ESP32 to a specific Wi-Fi network, you must know its SSID and password. Additionally, that network must be within the ESP32 Wi-Fi range (to check that, you can use the previous example to scan Wi-Fi networks).
You can use the following function to connect the ESP32 to a Wi-Fi network initWiFi() :
The ssid and password variables hold the SSID and password of the network you want to connect to.
Then, you simply need to call the initWiFi() function in your setup() .
How it Works?
Let’s take a quick look on how this function works.
First, set the Wi-Fi mode. If the ESP32 will connected to another network (access point/hotspot) it must be in station mode.
Then, use WiFi.begin() to connect to a network. You must pass as arguments the network SSID and its password:
Connecting to a Wi-Fi network can take a while, so we usually add a while loop that keeps checking if the connection was already established by using WiFi.status(). When the connection is successfully established, it returns WL_CONNECTED .
Get Wi-Fi Connection Status
To get the status of the Wi-Fi connection, you can use WiFi.status() . This returns one of the following values that correspond to the constants on the table:
|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|
Get WiFi Connection Strength
To get the WiFi connection strength, you can simply call WiFi.RSSI() after a WiFi connection.
Here’s an example:
Insert your network credentials and upload the code.
Open the Serial Monitor and press the ESP32 on-board RST button. It will connect to your network and print the RSSI (received signal strength indicator).
A lower absolute value means a strongest Wi-Fi connection.
Get ESP32 IP Address
When the ESP32 is set as a Wi-Fi station, it can connect to other networks (like your router). In this scenario, the router assigns a unique IP address to your ESP32 board. To get your board’s IP address, you need to call WiFi.localIP() after establishing a connection with your network.
Set a Static ESP32 IP Address
Instead of getting a randomly assigned IP address, you can set an available IP address of your preference to the ESP32 using WiFi.config() .
Outside the setup() and loop() functions, define the following variables with your own static IP address and corresponding gateway IP address. By default, the following code assigns the IP address 192.168.1.184 that works in the gateway 192.168.1.1.
Then, in the setup() you need to call the WiFi.config() method to assign the configurations to your ESP32.
The primaryDNS and secondaryDNS parameters are optional and you can remove them.
We recommend reading the following tutorial to learn how to set a static IP address:
Disconnect from Wi-Fi Network
To disconnect from a previously connected Wi-Fi network, use WiFi.disconnect() :
Reconnect to Wi-Fi Network After Lost Connection
To reconnect to Wi-Fi after a connection is lost, you can use WiFi.reconnect() to try to reconnect to the previously connected access point:
Or, you can call WiFi.disconnect() followed by WiFi.begin(ssid,password) .
Alternatively, you can also try to restart the ESP32 with ESP.restart() when the connection is lost.
You can add something like the snippet below to your loop() that checks once in a while if the board is connected.
Don’t forget to declare the previousMillis and interval variables. The interval corresponds to the period of time between each check in milliseconds (for example 30 seconds):
Here’s a complete example.
This example shows how to connect to a network and checks every 30 seconds if it is still connected. If it isn’t, it disconnects and tries to reconnect again.
Alternatively, you can also use WiFi Events to detected that the connection was lost and call a function to handle what to do when that happens (see the next section).
ESP32 Wi-Fi Events
The ESP32 can handle all the following Wi-Fi events:
|0||SYSTEM_EVENT_WIFI_READY||ESP32 Wi-Fi ready|
|1||SYSTEM_EVENT_SCAN_DONE||ESP32 finish scanning AP|
|2||SYSTEM_EVENT_STA_START||ESP32 station start|
|3||SYSTEM_EVENT_STA_STOP||ESP32 station stop|
|4||SYSTEM_EVENT_STA_CONNECTED||ESP32 station connected to AP|
|5||SYSTEM_EVENT_STA_DISCONNECTED||ESP32 station disconnected from AP|
|6||SYSTEM_EVENT_STA_AUTHMODE_CHANGE||the auth mode of AP connected by ESP32 station changed|
|7||SYSTEM_EVENT_STA_GOT_IP||ESP32 station got IP from connected AP|
|8||SYSTEM_EVENT_STA_LOST_IP||ESP32 station lost IP and the IP is reset to 0|
|9||SYSTEM_EVENT_STA_WPS_ER_SUCCESS||ESP32 station wps succeeds in enrollee mode|
|10||SYSTEM_EVENT_STA_WPS_ER_FAILED||ESP32 station wps fails in enrollee mode|
|11||SYSTEM_EVENT_STA_WPS_ER_TIMEOUT||ESP32 station wps timeout in enrollee mode|
|12||SYSTEM_EVENT_STA_WPS_ER_PIN||ESP32 station wps pin code in enrollee mode|
|13||SYSTEM_EVENT_AP_START||ESP32 soft-AP start|
|14||SYSTEM_EVENT_AP_STOP||ESP32 soft-AP stop|
|15||SYSTEM_EVENT_AP_STACONNECTED||a station connected to ESP32 soft-AP|
|16||SYSTEM_EVENT_AP_STADISCONNECTED||a station disconnected from ESP32 soft-AP|
|17||SYSTEM_EVENT_AP_STAIPASSIGNED||ESP32 soft-AP assign an IP to a connected station|
|18||SYSTEM_EVENT_AP_PROBEREQRECVED||Receive probe request packet in soft-AP interface|
|19||SYSTEM_EVENT_GOT_IP6||ESP32 station or ap or ethernet interface v6IP addr is preferred|
|20||SYSTEM_EVENT_ETH_START||ESP32 ethernet start|
|21||SYSTEM_EVENT_ETH_STOP||ESP32 ethernet stop|
|22||SYSTEM_EVENT_ETH_CONNECTED||ESP32 ethernet phy link up|
|23||SYSTEM_EVENT_ETH_DISCONNECTED||ESP32 ethernet phy link down|
|24||SYSTEM_EVENT_ETH_GOT_IP||ESP32 ethernet got IP from connected AP|
For a complete example on how to use those events, in your Arduino IDE, go to File > Examples > WiFi > WiFiClientEvents.
With Wi-Fi Events, you don’t need to be constantly checking the Wi-Fi state. When a certain event happens, it automatically calls the corresponding handling function.
Reconnect to Wi-Fi Network After Lost Connection (Wi-Fi Events)
Wi-Fi events can be useful to detect that a connection was lost and try to reconnect right after (use the SYSTEM_EVENT_AP_STADISCONNECTED event). Here’s a sample code:
How it Works?
In this example we’ve added three Wi-Fi events: when the ESP32 connects, when it gets an IP address, and when it disconnects: SYSTEM_EVENT_STA_CONNECTED , SYSTEM_EVENT_STA_GOT_IP , SYSTEM_EVENT_STA_DISCONNECTED .
When the ESP32 station connects to the access point ( SYSTEM_EVENT_STA_CONNECTED event), the WiFiStationConnected() function will be called:
The WiFiStationConnected() function simply prints that the ESP32 connected to an access point (for example, your router) successfully. However, you can modify the function to do any other task (like light up an LED to indicate that it is successfully connected to the network).
When the ESP32 gets its IP address, the WiFiGotIP() function runs.
That function simply prints the IP address son the Serial Monitor.
When the ESP32 loses the connection with the access point ( SYSTEM_EVENT_STA_DISCONNECTED ), the WiFiStationDisconnected() function is called.
That function prints a message indicating that the connection was lost and tries to reconnect:
The ESP32 WiFiMulti allows you to register multiple networks (SSID/password combinations). The ESP32 will connect to the Wi-Fi network with the strongest signal (RSSI). If the connection is lost, it will connect to the next network on the list. This requires that you include the WiFiMulti.h library (you don’t need to install it, it comes by default with the ESP32 package).
To learn how to use WiFiMulti, read the following tutorial:
Change ESP32 Hostname
To set a custom hostname for your board, call WiFi.setHostname(YOUR_NEW_HOSTNAME); before WiFi.begin();
The default ESP32 hostname is espressif .
There is a method provided by the WiFi.h library that allows you to set a custom hostname.
First, start by defining your new hostname. For example:
Then, call the WiFi.setHostname() function before calling WiFi.begin() . You also need to call WiFi.config() as shown below:
You can copy the complete example below:
You can use this previous snippet of code in your projects to set a custom hostname for the ESP32.
Important: you may need to restart your router for the changes to take effect.
After this, if you go to your router settings, you’ll see the ESP32 with the custom hostname.
This article was a compilation of some of the most used and useful ESP32 Wi-Fi functions. Although there are plenty of examples of using the ESP32 Wi-Fi capabilities, there is little documentation explaining how to use the Wi-Fi functions with the ESP32 using Arduino IDE. So, we’ve decided to put together this guide to make it easier to use ESP32 Wi-Fi related functions in your projects.
If you have other useful suggestions, you can share them on the comments’ section.
We hope you’ve found this tutorial useful.
Learn more about the ESP32 with our resources:
[eBook] Build Web Servers with ESP32 and ESP8266 (2nd Edition)
Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.
Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.
Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!
What to Read Next…
ESP8266 NodeMCU Web Server with BME680 – Weather Station (Arduino IDE)
ESP8266 Deep Sleep with Arduino IDE (NodeMCU)
ESP32 Pinout Reference: Which GPIO pins should you use?
Enjoyed this project? Stay updated by subscribing our newsletter!
81 thoughts on “ESP32 Useful Wi-Fi Library Functions (Arduino IDE)”
Your blog is very interresting and helps a lot, but i miss the information to change the hostname of the esp32. Is it possible to change the Espressif unit name to a project specifish name.
I know i can change the name in the wlan router but the unit name still is Espressif
You can change ESP32 hostname using commands like this:
String MA02_ID = “MA02_09”;
WiFi.setHostname(MA02_ID.c_str()); //define hostname
Try this, it should do the trick 😉
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
Unfortunately doesn’t work for me – tried it on 2 different boards and with different sized hostnames. Small hostnames just continue to show expressif on network scan and larger hostname just gives a blank 🙁
Sorry to hear that…
It works perfectly on my side!
Even with a rather long name with spaces.
Try to refresh the DHCP client list on your router.
Sorry for the delay.
I rebooted my router and lo and behold the changed hostnames appeared – thank you 🙂
I wanted to post an image to illustrate my point in my last comment, but it didn’t work. You can see it by clicking on the link.
The arduino-esp32 framework has just been updated to version 1.0.5. Some of the fixes include the following:
ad4cf146 Rework setHostname for WiFi STA
5de03a39 Fix WiFi STA config IP to INADDR_NONE results in 255.255.255.255
See if this solves your problem?
Is there a way to quickly do a WiFi Scan for a SPECIFIC SSID and, when it detects the WiFi SSID is available (or not), does something with this information in the sketch?
Something like that?
const char* SPECIFIC_SSID = «MyNetwork»;
const char* ENC_TYPE = <
struct WiFiInfo <
void findWiFi(const char *ssid, WiFiInfo *info) <
info->found = false;
int16_t n = WiFi.scanNetworks();
for (uint8_t i=0; i found = true;
info->channel = WiFi.channel(i);
info->rssi = WiFi.RSSI(i);
info->auth_mode = WiFi.encryptionType(i);
void setup() <
? «SSID: %s, channel: %i, RSSI: %i dBm, encryption: %s\n»
: «SSID: %s . could not be found\n»,
Sorry… formatting with Markdown didn’t work properly :-/
But you should be able to format the code in your favorite editor.
I fixed that by publishing the code on GitHub Gist 😉
Thanks for sharing this.
This worked perfectly. Thank you! I wonder if there is a faster way to get the SSID without having to scan for all networks first, and then isolating the network I’m searching for. Is there a way to do this?
hi, I hope someone can help me with this issue becasue I have weeks working on it and nothing looks to work….. I have a esp32 with my basic code just to connect with my wifi, not matter what I do, it does not connect, always shows me the error disconnected from AP (event 5)…. do you have any idea what is this happening? thanks
To use as a WiFi reference, there is a couple of things I would’ve liked to see included; i.e., commands to manipulate the MAC address of a board, and an example using the soft_AP and STA modes together.
As Werner noted, if there is a way to redefine the identifier, that would be great to know too!
That said, it is still a great article, and I would love to see it expanded with more examples of the more obscure commands’ responses. Maybe a downloadable table of the command/method/event, use format, possible responses, and any comments (such as “only valid in STA mode” or, a link to an example. Most of this is already in the article, just not well summarized, so hard to locate.
+1 for an example of how the combination WIFI_AP_STA works.
Thanks for your work.
I found the automatic reconnection feature after the card disconnected very interesting. However, they seem to understand that this doesn’t always work. Especially when using Blynk. Has anyone had any experience in this regard?
Great article. Woyld have liked to ser how to use both modes to make a WiFi extender on ESP32. Thete is little info on this on the web although i know it is possible.
Yes, Need this WiFi extender
Check this sentences pls: “Or, you can call WiFi.diconnect() followed by WiFi.begin(ssid,password)”. Must WiFi.disconnect
Thanks. You are right.
It’s fixed now!
Is it now possible to run ESP-Now together with WiFi in STA mode?
My last state was: ESP-Now and WiFi must share the same channel and the channel number must be 1. Even when the router starts a connection at channel 1 in STA mode, if the router changes the channel to avoid traffic collisions, the ESP-Now connection breaks.
Really good – thanks!
I would echo Dave’s request for an example using the soft_AP and STA modes together.
Thanks for the suggestion.
I’ll add that to my list.
I am having some trouble connecting to my local WiFi and I’m sure this info will help me understand what is happening.
Do you have something similar for the ESP8266?
What I would find most useful would be some sample code that:
attempted a connection with stored network ID and credentials
if this failed fall back to AP mode, so that a user can connect, login to a webUI, save new network details, and then reboot / attempt to reconnect.
No-one want to recompile code, just so a device can change networks.
Thanks for the suggestion.
In fact, we have a project like that in our most recent eBook: https://randomnerdtutorials.com/build-web-servers-esp32-esp8266-ebook/
I’ll probably release that project or a simplified free version of that project soon.
Very helpful artical. Thank you very much
It’s a pity that you omitted WiFi.persistent() command. I think, that this function is one of the most important when using WiFi and literally nobody knows about it.
In short: This function is true on default and causes, that every time (on every reboot) when calling WiFi.begin the ssid+pass are written into flash. This is a flash killer when using WiFi and deepsleep together.
Thanks for sharing that.
I’ll add it to the tutorial in the next update.
Thank you for publishing the article.
Regarding Reconnect to Wi-Fi Network After Lost Connection code
I have a need to modify the while loop so it checks for two conditions for example
(WiFi.status() != WL_CONNECTED) or (WiFi.status() != WL_NO_SSID_AVAIL)
Could you advice me of the best approach
I have found that setting a static IP address in Station mode sometimes works, and sometimes not. I guess it depends on the router and a bunch of advanced network stuff that I dont understand. Nevertheless: this means for a user who wants to access the ESP32 webserver page, he/she must know the IP. How do you solve this when that user does not have access to the serial print nor some ESP-attached display?
A static IP address would be a great solution, but it is just not reliable enough. I have also tried with mDNS and also that was unreliable (worked on iPhone but not Android).
This is a use case where I would give a project as a gift to someone who doesnt know Arduino and can’t expect them to read Serial monitor or something like that. I haven’t found a true solution to this problem anywhere. How do you solve it?
normally a DHCP server on the router supplies IP addresses to the clients from a list of allowed addresses. If you set a static IP address in Station mode, this address must be excluded from the list of addresses the router is allowed to supply, otherwise two same addresses clash. Look in the configuration of your router, search for a DHCP entry and exclude your static IP address. Then static IP addresses are really reliable.
Thank you for your reply Peter. That would totally make static IP addresses work. But my use case is when I give a project as a gift to someone to use in their home network. I can’t expect them (think your mother haha) to go in and mess in the config of their router. I am looking for a solid and simple solution that does not require reading the serial monitor, or attaching a screen (one time use only to find out the IP!) nor changing their router settings. Most people can’t do this and I want to build something for most people.
in your case you cannot go with a static IP address. You know the MAC (physical) address of your ESP32 board or you can set the MAC address of the ESP32 to your own address (see RUI’s tutorials for details). Then you need to identify the dynamic IP address corresponding to your MAC address within your client app. This is done by the ReverseARP protocol RARP. On Windows or Linux use “arp -a” which creates a list of all MAC addresses and the corresponding IP addresses in your network. See https://www.pcwdld.com/find-device-or-ip-address-using-mac-address for details.
If you don’t want to attach an LCD or TFT screen to your device, you can flash a simple LED to reveal the last byte of the IP address, going through the 3 decimal digits that make it up for example… assuming the person knows the address class of their private network (which they can view on any of their connected devices) 😋
Thank you for publishing the article. Regarding the while loop I have a need to modify the while loop so it checks for two conditions for example
(WiFi.status() != WL_CONNECTED) or (WiFi.status() != WL_NO_SSID_AVAIL)
Could you advice me of the best approach
Is there a way to show my gratitude, Rui and Sara? The books and tutorials that you produce are nothing short of fantastic. It’s a joy to work on a project that incorporates your work knowing that you have published information and code that is accurate and complete. You two are making a huge impact on the world of IOT and data networking that will advance the technology as well as advance the knowledge of thousands of us nerds! Thank you for what you do for the world every day!
Thank you so much for your nice words.
I’m glad you enjoy our tutorials and eBooks.
There are several ways to support our work. See here: https://randomnerdtutorials.com/about/.
I was looking for same type of content. Thanks for doing enhanced research for us. I will try it and give my feedback again.
Hi Sara & Rui,
Thank you for another great tutorial. You guys are really the best on the ESP tutorials for many reasons. Please forgive the length of this little comment. Note to all of you: I do not make a dime writing what I post below. I just love the work that Rui & Sara are doing and want them to keep doing it forever!
I have a stressful job that I actually love, (regulatory consultant helping people comply with impossible regulations), but as far as concentration zen time to re-load a kinder gentler me, reading & following the Random Nerds tutorials & courses is the best stress relief I have had in 50 years (neglecting some obvious things like swimming, fishing, time with wife, and family gatherings, etc.) No kidding!
Note to Bob above about support. You can’t go wrong purchasing RN courses at their https://rntlab.com/shop/.
The Random Nerd courses are very comprehensive, better organized, easy to follow, inexpensive, and just pure excellence compared to other courses on similar subjects. They have on-line and pdf version of the courses, as well as great videos for all the course material. They are the best organized courses with useful examples. Every time I have had a relevant question and email either Rui or Sara, they respond with really helpful info that is to the point and relevant. I just wish I had more time to spend reading their stuff and using it on my little projects.
As far as the boards used on their ESP32 courses, even though Rui & Sara mostly use the ESP-WROOM-32 varieties (30 pin and 38 pin Devkits mostly), their code runs on every ESP32 board that I have tried it on, by just paying attention to the pinout for the board and adapting the code a little.
Hope I did not miss Amin’s point, but maybe a suggestion for Amin: if you try out their code on a couple of ESP32’s with little on board displays, even though they are a little more expensive, like the M5Sticks, M5 Core, LILIGO TTGO T-Display, or LILIGO TTGO TS, or HelTec WiFi Kit V2, or even the MorphESP240, (Definitely use Rui & Sara’s Maker Advisor at https://makeradvisor.com/ for where to get a ESP32 board with display for the best price), then you will find one for a reasonable price that you can try your code on. Then when you are satisfied with how it works, just port it over to a very inexpensive DevKit V1 or whatever ESP board you like.
Anyway, they work mostly, but I pray that Rui & Sara do a tutorial on that, because it it will be far superior to what I have tried so far. Maybe they have & I just can’t find it. Anyway, I might be confusing it with another source. Don’t get me wrong, the other authors tutorials & githubs are fine. Nonetheless, the way Rui & Sara do their stuff blows everyone else out of the water.
Thanks again Random Nerds. I wish words were sufficient to express my appreciation. I’ve already purchase and read all you courses (except 2).
Thank you so much for your nice words and for supporting our work.
I’m speechless about your awesome testimonial. I just want to say “Thank you!”. It feels nice to know that people find value in our work.
The randomnerds website is not ours. I’ve deleted that line in your comment to not confused other people.