I am currently working on Project 4-3 in the web server course and am having some difficulties in making it work as expected.
When I run the code for the first time, it creates an access point. I then use my iPhone to connect to the access point and once it is connected, I go to the phone web browser (Safari) and enter the ip address 192.168.4.1. I obtain the expected screen where I enter the SSID, Password, and router assigned ip address of my ESP32 board. At the same time, I am monitoring the serial monitor in PlatformIO and I observe that it is trying to connect to WiFi, but it does not ever connect. The phone, of course, does not display the web page that allows control of the on board LED. I am certain that I entered the correct SSID and password and that was confirmed in the serial monitor display.
I would appreciate any suggestions for troubleshooting this project. The code entered was from the course files, so there would not be any typos.
Thanks,
Ed Wilson
Hi.
Are you sure the SSID and password are correct? Without any extra spaces?
What messages do you get on the Serial Monitor?
regards,
Sara
Sara,
Thanks for your response!
Yes, I am sure that the SSID and password are correct without any extra spaces.
I receive the following messages on the serial monitor and at the end even though the message in the phone says that the program will reset and go to the onboard LED web page:
IP Address set to: 192.168.1.29
Writing file: /ip.txt
– file written
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
SPIFFS mounted successfully
Reading file: /ssid.txt
Reading file: /pass.txt
Reading file: /ip.txt
WindingWoods2GHz
heidi100
192.168.1.29
Connecting to WiFi…
192.168.1.29
The ip address above is the one reserved for this ESP32 board by my router.
Thanks in advance for any troubleshooting hints,
Ed Wilson
Hi again.
When you say it doesn’t connect what happens exactly?
When you go to that IP address what happens? Try on your computer and smartphone.
Regards,
Sara
Sara,
Ok, maybe I do not understand how this code should work.
When I entered ip address 192.168.4.1 on the phone (to connect to the access point), it displayed a message about resetting and then going to the 192.168.1.29 ip address. I expected it to display the web page where you can turn the on board LED on and off, but it just sits there without doing anything. Today, I did go to that 192.168.1.29 ip address with another computer on the local network and the correct web page did display. I also manually changed to the 192.168.1.29 ip address on the phone and the correct web page did display. I expected the phone to “automatically” switch to the correct web page and apparently that is not the case. I also expected the serial monitor to say something like “Successfully connected to WiFi” and it just displayed the ip address and nothing else (see previous answer). I thought it was just sitting there trying to connect unsuccessfully.
So, if the project is working as intended, please excuse me for asking the question and mark it as resolved.
Thanks,
Ed Wilson
Hi.
Yes, that’s the usual behavior.
Unfortunately, it doesn’t switch to the other page automatically.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara
I am having similar issues.
There are stored WiFI credentials, but if they do not result in a LAN connection, wifi continues to attempt to reconnect (with the bad credentials), apparently forever. This prevents AP mode from working properly. AP starts then goes down, crazy infinite loop.
I can’t get the provided LAN timeout to work properly, so I have “delay(3000);” then check for a LAN connection, if FALSE, then I added:
WiFi.disconnect(true);
Then I explicitly change to AP mode:
WiFi.mode(WIFI_AP);
This stops the perpetual reconnect attempts.
Hi.
Are you using and ESP32 or ESP8266?
Are you using the right code for the board you’re using?
We have the following snippet in the code that should prevent further attempts in case of wrong SSID/password combination.
WiFi.begin(ssid.c_str(), pass.c_str());
Serial.println(“Connecting to WiFi…”);
delay(20000);
if(WiFi.status() != WL_CONNECTED) {
Serial.println(“Failed to connect.”);
return false;
}
Regards,
Sara
My code does:
Check for SPIFFS files for SSID and PW.
If file exist, read them and attempt to connect.
check for connected (as you show above)
if NOT connected to LAN, return FALSE which causes AP Mode.
However, the AP mode would start, then go down, and cycle not being active long enough to allow an AP connection. The serial monitor shows continuous attempts to reconnect to the LAN interrupting AP mode.
When I added:
WiFi.disconnect(true);
prior to the return FALSE, then it worked as desired.
// —————— Initialize WiFi
bool initWiFi() {
if(SPIFFS.exists(ssidPath) ) { // new in v034a_ota-02
Serial.println(“InitWiFi: ssid.txt EXISTS. READING ALL WIFI CONFIG FILES “);
wifimode = 1;
Serial.println(“WiFi init: reading wifi Files”);
ssid = readFile(SPIFFS, ssidPath);
//Serial.print(“init wifi: ssid: “); Serial.println(“”);
pass = readFile(SPIFFS, passPath);
//Serial.print(“init wifi: password: “); Serial.println(“”);
ip = readFile(SPIFFS, ipPath);
//Serial.print(“init wifi: IP addr: “); Serial.println(“”);
gateway = readFile(SPIFFS, gatewayPath); // v035
//Serial.print(“init wifi: gateway: “); Serial.println(“”);
subnet = readFile(SPIFFS, subnetPath);
//Serial.print(“init wifi: subnet: “); Serial.println(“”);
dhcpcheck = readFile(SPIFFS, dhcpPath);
//Serial.print(“init wifi: ssid: “); Serial.println(“”);
// attempt to conn to LAN
Serial.println(“WiFi init: Attempting LAN connection”);
WiFi.mode(WIFI_STA);
if(dhcpcheck == “off”) { // DHCP==false
//Serial.println(“init_wifi: dhcp==off”);
localIP.fromString(ip.c_str()); // v035 copied from RNT Elegant OTA
gatewayIP.fromString(gateway.c_str());
subnetMask.fromString(subnet.c_str());
if (!WiFi.config(localIP, gatewayIP, subnetMask ) ){
showln(“WiFi init: STA Failed to configure <—– error”);
return false;
}
}
else { // dhcp == On
//Serial.println(“InitWiFi: dhcp==ON”);
localIP.fromString(“”); // v035 copied from RNT Elegant OTA
gatewayIP.fromString(“”);
subnetMask.fromString(“”);
WiFi.config(localIP, gatewayIP, subnetMask);
showln(“WiFi init: DHCP selected”);
}
WiFi.begin(ssid.c_str(), pass.c_str());
wifimode = 1;
Serial.println(“InitWiFi: Checking LAN conn timeout”);
// wait for LAN connection
delay(5000); // V4.004a
if(WiFi.status() != WL_CONNECTED) { // NOT CONNECTED TO LAN
wifimode =0;
WiFi.disconnect(true); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// added line above to stop LAN connect retries
//
//
Serial.println(“InitWiFi: wifi_disconnect”);
WiFi.mode(WIFI_AP); // V4.004a
Serial.println(“Setup: WiFi starting AP Mode”);
WiFi.mode(WIFI_AP);
WiFi.softAP(“RF_Switch”, NULL);
IPAddress IP = WiFi.softAPIP();
return false; // LAN Mode Failed, return False
}
else { // LAN is connected
wifimode = 1;
Serial.print(“WiFi init: LAN Mode IP: “);
Serial.println(WiFi.localIP());
appendFile(SPIFFS, logPath, “WiFi Init: in LAN mode”); //v 4.003
return true; // YES should be LAN connected
}
} // if SPIFFD.exists
else { // no ssid.txt default to AP mode
Serial.println(“InitWiFi: No SSID cred found. Defaulting to AP Mode”);
WiFi.mode(WIFI_AP); // V4.004a
wifimode = 0;
appendFile(SPIFFS, logPath, “InitWiFi: No SSID cred fount. Defaulting to AP Mode”); //v 4.003
Serial.println(“Setup: WiFi starting AP Mode”); // 4.004b moved here from setup
WiFi.mode(WIFI_AP);
WiFi.softAP(“RF_Switch”, NULL);
IPAddress IP = WiFi.softAPIP();
Serial.print(“Setup: AP Mode, IP address: “);
Serial.println(IP);
return false;
}
} // end initWiFi