I'm playing with my new ESP32s. AP mode seems to work fine, but I cannot get them to connect to ANY AP in STA mode. I've tried both my home router and the mobile hotspot on my phone, both of which work perfectly with my other devices.
The code is as simple as it can be, but always hangs in the while loop in setup(), waiting for the connection:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
const char *STAssid = "RayL's Phone";
const char *STApassword = "79517515";
boolean LEDIsOn = false;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Configuring station...");
Serial.printf("STA SSID: \"%s\" Password: \"%s\"\n", STAssid, STApassword);
WiFi.begin(STAssid, STApassword);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
IPAddress myIP = WiFi.localIP();
Serial.print("STA IP address: ");
Serial.println(myIP);
}
void loop()
{
}
How can something so simple, not work? What am I missing?
OK, this is bizarre. I just discovered, by accident, that it WILL connect.... every OTHER time I press the reset button. It seems 100% consistent - Press reset, it won't connect. Press reset again, it connects instantly. Press reset again, it won't connect. Press reset again, it connects instantly.
WTH???
Making this change to the while loop has it connecting within a few seconds every time:
int cnt = 0;
while (WiFi.status() != WL_CONNECTED)
{
if (cnt == 0)
{
WiFi.begin(STAssid, STApassword);
delay(1000);
}
delay(500);
Serial.print(".");
if (++cnt == 10)
{
WiFi.disconnect();
cnt = 0;
}
}
But why is this necessary? I never saw this with the 8266.
I remember something about the WiFi not being disconnected properly during boot on some modules, the sollution is to disconnect wifi before connecting. I have just tested the following code on an ESP32 (DOIT ESP32 DEVKIT V1), and it connects every time (with and without the disconnect):
void setup() {
Serial.begin(115200);
WiFi.disconnect();
delay(1000); //Wait for serial
Serial.println("Connecting...");
WiFi.begin(w_ssid, w_pass);
while (WiFi.status() != WL_CONNECTED) delay(500);
Serial.println("Connected!");
ESP.restart();
}
I'm still seeing the same behavior with the disconnect(). Seems to be a 50% chance of connection success on power-up or reset. Very odd....
The good news is, I don't need STA mode immediately, and AP mode is working great. Even better, the problem that forced me to switch from 8266 to ESP32 is definitely fixed, and performance is noticeably improved!
One thing I am curious about - It is possible to run AP+STA mode, but I'm not sure what the utility of that is. Is is possible to have the STA connected to an Internet-connect AP, so the clients of the ESP32 AP can access the internet?
I have not yet figured out the right sequence of magical incantations to make AP+STA mode do anything particularly useful.
The good news is, I don't need STA mode immediately, and AP mode is working great. Even better, the problem that forced me to switch from 8266 to ESP32 is definitely fixed, and performance is noticeably improved!
One thing I am curious about - It is possible to run AP+STA mode, but I'm not sure what the utility of that is. Is is possible to have the STA connected to an Internet-connect AP, so the clients of the ESP32 AP can access the internet?
I have not yet figured out the right sequence of magical incantations to make AP+STA mode do anything particularly useful.
Also, some of the ESP32 core libraries seem a bit buggy. Adding one line, following WiFi.softAP(SSID):
WiFi.softAPConfig(APIP, gatewayIP, subnetMask);
Cause a crash when the external device connects. Sometimes, but not always, it just crashes as long as ANYTHING is attempting to connect. Other times it crashes once, then is fine. This is apparently a bug in the ESP32 core library somewhere, but I did not see this problem in the 8266. But as long as I use the default IP, etc., all seems fine.
Change
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
to:
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(WiFi.status());
}
This will show you what's happening during the connect.
The WiFi.status function returns a value
Code Value Meaning
WL_IDLE_STATUS 0 WiFi is in process of changing between statuses
WL_NO_SSID_AVAIL 1 SSID cannot be reached
WL_SCAN_COMPLETED 2
WL_CONNECTED 3 Successful connection is established
WL_CONNECT_FAILED 4 Password is incorrect
WL_CONNECTION_LOST 5
WL_DISCONNECTED 6 Module is not configured in station mode
You should normally get a couple of seconds of "6" followed by a single "3".