{"id":823,"date":"2026-04-06T21:05:03","date_gmt":"2026-04-06T19:05:03","guid":{"rendered":"https:\/\/avlos.gr\/wpress\/?p=823"},"modified":"2026-04-06T21:10:53","modified_gmt":"2026-04-06T19:10:53","slug":"esp32-webserver-with-automatic-wi-fi-setup","status":"publish","type":"post","link":"https:\/\/avlos.gr\/wpress\/en\/esp32-webserver-with-automatic-wi-fi-setup\/","title":{"rendered":"ESP32 WebServer with automatic Wi-Fi Setup"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The ESP32 microcontroller has the ability to create a wifi network in two ways, either by creating an Access Point (AP) where it functions as a Wi-Fi hotspot (router) or by creating a Station (STA) where it functions as a client.<br><br>Creating an Access Point is usually used to connect a mobile phone or a computer browser to the device that has the ESP32. On the mobile phone or computer we must search for available wifi and connect to that of the ESP32 device. Usually when it functions as an Access Point it gets by default ip address 192.168.4.1 Through this connection, various parameters such as ssid, password, or anything else considered critical to start operating autonomously are provided to the ESP32 device. When the device operates in Access Point Mode it is not connected to the internet.<br>In case the ESP32 operates in Station Mode (STA) i.e. as a client, then it connects to an existing wi-fi, gets an IP address from the router and can access the internet or other devices.<br>There is also the complex STA + AP connection mode which is rarely used or in debugging cases.<br>Microcontrollers like the ESP32 use NVS (Non-Volatile Storage), where data can be stored such as settings, numbers or strings that are not lost when the device is turned off. Usually the \u201cPreferences.h\u201d library is used to implement this function.<br>An expression of the following form is used to retrieve the saved ssid and passs values \u200b\u200blocated in the area we have named wifi. True means that the area has been opened for reading only.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>prefs.begin(\"wifi\", true); \nsavedSSID = prefs.getString(\"ssid\", \"\"); \nsavedPASS = prefs.getString(\"pass\", \"\"); \nprefs.end();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><br>Accordingly, an expression of the following form is used to store the ssid and pass values \u200b\u200bin the area we define with the name wifi. The false in .begin means that the area has been opened for writing and reading.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>prefs.begin(\"wifi\", false);\nprefs.putString(\"ssid\", ssid);\nprefs.putString(\"pass\", pass);\nprefs.end();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the example, I chose to create a WebServer with the ESP32 to show how with all the functions I developed above I can ensure an automatic wifi setup. So every time my device changes installation location I can simply connect it to the new wifi network. Besides, this is a part of the upgrade of the \u201cESP32 WebServer\u201d i.e. the weather station, which I present to you in the relevant article.<br><br>In the code that follows we see that the setup tries to retrieve ssid and pass from the Non-Volatile memory area with the name wifi. If these elements exist then it calls connectToWiFi() and proceeds to connect to the wifi. If these elements do not exist (or are incorrect, you will see this in connectToWiFi) then it calls startAP() which puts the device in Access Point Mode (AP) in order to search for and save the necessary elements.<br>The first commands of startAP() are WiFi.mode(WIFI_AP) which activates the AP mode and WiFi.softAP(&#8220;ESP32_Setup&#8221;, &#8220;12345678&#8221;); which gives the name &#8220;ESP32_Setup&#8221; to the wifi and sets the password to &#8220;12345678&#8221;.<br>After startAP() has completed its work, it calls ESP.restart() which restarts the ESP32. This time, and since the elements we saved are correct, connectToWiFi() will be called to connect the device to the wifi of the router we want. We notice that connectToWiFi() after giving the device an ip address ends with the call to startServer(). startServer() starts the web server, in the case of the example the call to the server is made at the address http:\/\/ipaddress\/hello and the response from the server is \u201cHello from ESP32 .-\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The program you are reading below can be downloaded from <a href=\"https:\/\/avlos.gr\/wpress\/download\/esp32-webserver-with-automatic-wi-fi-setup\/\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ESP32 microcontroller has the ability to create a wifi network in two ways, either by creating an Access Point (AP) where it functions as a Wi-Fi hotspot (router) or by creating a Station (STA) where it functions as a&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,199],"tags":[],"class_list":["post-823","post","type-post","status-publish","format-standard","hentry","category-home","category-iot"],"_links":{"self":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/comments?post=823"}],"version-history":[{"count":3,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/823\/revisions"}],"predecessor-version":[{"id":827,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/823\/revisions\/827"}],"wp:attachment":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/media?parent=823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/categories?post=823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/tags?post=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}