It is possible to setup ESP32’s Wi-Fi to access point and station. ESP32 can connect to another hotspot and share the connection. An access point (AP) is a networking hardware device which allows other Wi-Fi devices to connect to a network. Some basic theory required to know for making basic code better.
An AP is different from a hotspot.
Hotspot is the physical location (and the device) where Wi-Fi access to a WLAN is available. SoftAP or software enabled access point is software enabling a
device which is not router but creates access point by software/coding. Wireless access has special security considerations. Many wired networks base the security on physical access control. Modern access points come with built-in encryption. The first generation encryption scheme, WEP, proved easy to crack; the second and third generation schemes, WPA and WPA2, are considered secure if a strong enough password or passphrase is used.
What is the usage? If we have one ESP32 running as an access point (AP) and a second ESP32 running as a station (STA). When the access point (AP) ESP32 starts up and the STA ESP32 connects to it, then we should be able to form a connection from a client application running on AP. This actually increase the range in some specific use-case.
We can set a soft AP using the ESP32 and the Arduino core. This way, other devices can connect to the ESP32 without the need to connect to a router. This is soft AP :
---
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include "WiFi.h" const char *ssid = "MyESP32AP"; const char *password = "testpassword"; void setup() { Serial.begin(115200); WiFi.softAP(ssid, password); Serial.println(); Serial.print("IP address: "); Serial.println(WiFi.softAPIP()); } void loop() {} |
Basic example code for ESP32 Arduino is like below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <WiFi.h> #include <WiFiAP.h> const char* ssid = "wifi name"; // the esp will be connected to const char* password = "wifi password"; // the password of it const char* assid = "AccessPoint"; // devices will connect const char* asecret = "something"; // password for it void setup(){ Serial.begin(115200); WiFi.mode(WIFI_AP_STA); //access point Serial.println("Creating Accesspoint"); WiFi.softAP(assid,asecret,7,0,5); Serial.print("IP address:\t"); Serial.println(WiFi.softAPIP()); //station Serial.print("connecting to..."); Serial.println(ssid); WiFi.begin(ssid,password); while(WiFi.status() != WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop(){ } |
ESP32 WIFI_AP_STA mode has known issues which results to the condition of not working as intended. This is related to the fact that STA will switch to the channel of the AP it is trying to connect to, and SoftAP will have to switch to the same channel. So the client will have to reconnect to the SoftAP on its new channel. In most cases this causes TCP connections to be reset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <WiFi.h> #include <WiFiClient.h> #include <WebServer.h> #include "esp_log.h" const char* ssid = "your ssid"; const char* password = "password"; WebServer server(80); void setup(void) { Serial.begin(115200); esp_log_level_set("*", ESP_LOG_VERBOSE); WiFi.mode(WIFI_AP_STA); WiFi.softAP("new_TEST","43255234",6); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void loop(void) { } |