I tried to connect my ESP32 Wroom 32 to my home network but it doesn't work.
So I tried the ESP32 to an AP (my smartphone), and... it work !
But i want to create home automation so it's not possible to let my AP always active.
I tried to change the security protection (WPA (TKIP + AES) then WPA (TKIP) then WPA (AES/CCMP) but nothing change.
I tried to identify the WiFi status : Serial.println(WiFi.status());
And it was 1 (WL_NO_SSID_AVAIL). So I thought the ESP32 can't see the network.
So i test with a scan network (ESP32 Useful Wi-Fi Library Functions (Arduino IDE) | Random Nerd Tutorials) and the ssid appeared and he was with * (open network).
So I thought my box was without password (even if there is one) so I tried to connect without password : WiFi.begin(ssid) and this time the wifi status was 6 (WL_DISCONNECTED) but I the esp32 is in Station Mode !!! WiFi.mode(WIFI_STA);
I understand nothing please help me
PS : Sorry for the english mistakes, I'm french
Full code WiFi :
#include <WiFi.h>
#include<WiFiClientSecure.h>
#include <WebServer.h>
#include <ESPmDNS.h>
const char* ssid="freebox_PEKMIV";
const char* password="vichnou75";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(WiFi.status());
delay(1000);
}
Full code Scan Wifi :
#include "WiFi.h"
void setup() {
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
Serial.println("Setup done");
}
void loop() {
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(5000);
}
Your connect to WiFi code does not wait for the connection to be really established.
Try the code below
// Demo-Code connect an ESP32 to a WiFi-network using stationmode STA
// stationmode is simply join the WiFi-network as your computer or smartphone does it
// the code has three useful functions one for easy identifiying the code in the flash
// one for non-blocking timing
// a heartbeat-blinker function for indicating the state the code is in
// the code is written with two programming rules:
// 1. put EACH functionality into its OWN function
// 2. give EACH function a SELF-explaining name what the function does
// you should follow these programming rules
#include <WiFi.h>
const char *ssid = "SSID";
const char *password = "Password";
void PrintFileNameDateTime() {
Serial.println( F("Code running comes from file ") );
Serial.println( F(__FILE__));
Serial.print( F(" compiled ") );
Serial.print(F(__DATE__));
Serial.print( F(" ") );
Serial.println(F(__TIME__));
}
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
const byte OnBoard_LED = 2;
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
digitalWrite(IO_Pin,!digitalRead(IO_Pin) );
}
}
void ConnectToWiFi() {
WiFi.mode(WIFI_STA);
Serial.println("WiFi.mode(WIFI_STA)");
int myCount = 0;
Serial.print("trying to connect to #");
Serial.print(ssid);
Serial.println("#");
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED && myCount < 31) {
BlinkHeartBeatLED(OnBoard_LED, 50); // blink LED fast during attempt to connect
yield();
if ( TimePeriodIsOver(MyTestTimer, 500) ) { // once every 500 miliseconds
Serial.print("."); // print a dot
myCount++;
if (myCount > 30) { // after 30 dots = 15 seconds restart
Serial.println();
Serial.print("not yet connected executing ESP.restart();");
ESP.restart();
}
}
}
if (WiFi.status() == WL_CONNECTED ) {
Serial.println("");
Serial.print("Connected to #");
Serial.print(ssid);
Serial.print("# IP address: ");
Serial.println(WiFi.localIP());
}
}
void setup() {
Serial.begin(115200);
Serial.println( F("Setup-Start") );
PrintFileNameDateTime();
ConnectToWiFi();
}
void PrintHelloMsg() {
Serial.print( F("Hi there I'm the demo-code my IP address is: ") );
Serial.println(WiFi.localIP());
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED,500); // change blinking to a lower frequency indicating beeing connected
if ( TimePeriodIsOver(MyTestTimer,1000) ) {
PrintHelloMsg();
}
}
/*
most ESP32 boards have a blue LED connected to GPIO-pin 2
This blue LED is used to indicate state connecting to WiFi by blinking fast
state beeing connected to Wifi by blinking with 1 Hz
If the WiFi-connection is successfully established the serial monitor shows
08:44:02.915 -> Setup-Start
08:44:02.915 -> Code running comes from file
08:44:02.915 -> your-path\ESP32-connect-to-Wifi-Demo-001\ESP32-connect-to-Wifi-Demo-001.ino
08:44:02.915 -> compiled date/time of compiling
08:44:02.971 -> WiFi.mode(WIFI_STA)
08:44:02.971 -> trying to connect to #Your SSID#
08:44:03.362 -> ....
08:44:04.215 -> Connected to #Your SSID# IP address: given IP-adress NNN.NNN.NNN.NNN
08:44:04.865 -> Hi there I'm the demo-code my IP address is: NNN.NNN.NNN.NNN
if there is something wrong with your WiFi, SSID or password
you will see this in the serial monitor
08:32:36.598 -> Setup-Start
08:32:36.598 -> Code running comes from file
08:32:36.598 -> F:\MyPortable-PRgs\arduino-1.8.16-newb\portable\sketchbook\ESP32-connect-to-Wifi-Demo-001\ESP32-connect-to-Wifi-Demo-001.ino
08:32:36.598 -> compiled Dec 1 2021 08:32:18
08:32:36.684 -> WiFi.mode(WIFI_STA)
08:32:36.684 -> trying to connect to #your SSID#
08:32:37.036 -> ...............................
08:32:52.035 -> not yet connected executing ESP.restart();ets Jun 8 2016 00:22:57
08:32:52.035 ->
08:32:52.035 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
08:32:52.035 -> configsip: 0, SPIWP:0xee
08:32:52.035 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
08:32:52.073 -> mode:DIO, clock div:1
08:32:52.073 -> load:0x3fff0018,len:4
08:32:52.073 -> load:0x3fff001c,len:1216
08:32:52.073 -> ho 0 tail 12 room 4
08:32:52.073 -> load:0x40078000,len:9720
08:32:52.073 -> ho 0 tail 12 room 4
08:32:52.073 -> load:0x40080400,len:6352
08:32:52.073 -> entry 0x400806b8
08:32:52.309 -> Setup-Start
08:32:52.309 -> Code running comes from file
08:32:52.309 -> F:\MyPortable-PRgs\arduino-1.8.16-newb\portable\sketchbook\ESP32-connect-to-Wifi-Demo-001\ESP32-connect-to-Wifi-Demo-001.ino
08:32:52.309 -> compiled Dec 1 2021 08:32:18
08:32:52.405 -> WiFi.mode(WIFI_STA)
08:32:52.405 -> trying to connect to #Your SSID#
08:32:52.758 -> ...............................
08:33:07.788 -> not yet connected executing ESP.restart();ets Jun 8 2016 00:22:57
*/
How many characters does your password have?
ESP8266 and ESP32 can only connect to a WiFi if the password is minimum 8 characters long.
Your router could be configured to accept only certain MAC-adresses and maybe your ESP32 is not on the whitelist for that.
While establishing a WiFi connection the status can go through multiple different states
here is a code-version that prints the status each time the status changes
// Demo-Code connect an ESP32 to a WiFi-network using stationmode STA
// stationmode is simply join the WiFi-network as your computer or smartphone does it
// the code has three useful functions one for easy identifiying the code in the flash
// one for non-blocking timing
// a heartbeat-blinker function for indicating the state the code is in
// the code is written with two programming rules:
// 1. put EACH functionality into its OWN function
// 2. give EACH function a SELF-explaining name what the function does
// you should follow these programming rules
#include <WiFi.h>
const char *ssid = "SSID";
const char *password = "Password";
void PrintFileNameDateTime() {
Serial.println( F("Code running comes from file ") );
Serial.println( F(__FILE__));
Serial.print( F(" compiled ") );
Serial.print(F(__DATE__));
Serial.print( F(" ") );
Serial.println(F(__TIME__));
}
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
const byte OnBoard_LED = 2;
uint8_t actualWiFiStatus;
uint8_t lastWiFiStatus;
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
}
}
void printWiFiStatus(uint8_t p_WiFiStatus) {
switch (p_WiFiStatus) {
case WL_IDLE_STATUS:
Serial.println("WL_IDLE_STATUS temporary status assigned when WiFi.begin() is called");
break;
case WL_NO_SSID_AVAIL:
Serial.println("WL_NO_SSID_AVAIL when no SSID are available");
break;
case WL_SCAN_COMPLETED:
Serial.println("WL_SCAN_COMPLETED scan networks is completed");
break;
case WL_CONNECTED:
Serial.println("WL_CONNECTED when connected to a WiFi network");
break;
case WL_CONNECT_FAILED:
Serial.println("WL_CONNECT_FAILED when the connection fails for all the attempts");
break;
case WL_CONNECTION_LOST:
Serial.println("WL_CONNECTION_LOST when the connection is lost");
break;
case WL_DISCONNECTED:
Serial.println("WL_DISCONNECTED when disconnected from a network");
break;
}
Serial.println();
Serial.println();
}
void ConnectToWiFi() {
WiFi.mode(WIFI_STA);
Serial.println("WiFi.mode(WIFI_STA)");
int myCount = 0;
Serial.print("trying to connect to #");
Serial.print(ssid);
Serial.println("#");
WiFi.begin(ssid, password);
Serial.println("WiFi.begin() done");
actualWiFiStatus = WiFi.status();
#define maxCount 120
// Wait for connection
while (actualWiFiStatus != WL_CONNECTED && myCount < maxCount) {
actualWiFiStatus = WiFi.status();
if (lastWiFiStatus != actualWiFiStatus) {
Serial.println("WiFiStatus changed from");
printWiFiStatus(lastWiFiStatus);
Serial.println("to new status");
printWiFiStatus(actualWiFiStatus);
lastWiFiStatus = actualWiFiStatus;
}
BlinkHeartBeatLED(OnBoard_LED, 50); // blink LED fast during attempt to connect
yield();
if ( TimePeriodIsOver(MyTestTimer, 500) ) { // once every 500 miliseconds
Serial.print("."); // print a dot
myCount++;
if (myCount > maxCount) { // after maxCount dots restart
Serial.println();
Serial.print("not yet connected executing ESP.restart();");
ESP.restart();
}
}
}
if (WiFi.status() == WL_CONNECTED ) {
Serial.println("");
Serial.print("Connected to #");
Serial.print(ssid);
Serial.print("# IP address: ");
Serial.println(WiFi.localIP());
}
}
void setup() {
Serial.begin(115200);
Serial.println( F("Setup-Start") );
PrintFileNameDateTime();
ConnectToWiFi();
}
void PrintHelloMsg() {
Serial.print( F("Hi there I'm the demo-code my IP address is: ") );
Serial.println(WiFi.localIP());
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED, 500); // change blinking to a lower frequency indicating beeing connected
if ( TimePeriodIsOver(MyTestTimer, 1000) ) {
PrintHelloMsg();
}
}
/*
most ESP32 boards have a blue LED connected to GPIO-pin 2
This blue LED is used to indicate state connecting to WiFi by blinking fast
state beeing connected to Wifi by blinking with 1 Hz
If the WiFi-connection is successfully established the serial monitor shows
08:44:02.915 -> Setup-Start
08:44:02.915 -> Code running comes from file
08:44:02.915 -> your-path\ESP32-connect-to-Wifi-Demo-001\ESP32-connect-to-Wifi-Demo-001.ino
08:44:02.915 -> compiled date/time of compiling
08:44:02.971 -> WiFi.mode(WIFI_STA)
08:44:02.971 -> trying to connect to #Your SSID#
08:44:03.362 -> ....
08:44:04.215 -> Connected to #Your SSID# IP address: given IP-adress NNN.NNN.NNN.NNN
08:44:04.865 -> Hi there I'm the demo-code my IP address is: NNN.NNN.NNN.NNN
if there is something wrong with your WiFi, SSID or password
you will see this in the serial monitor
08:32:36.598 -> Setup-Start
08:32:36.598 -> Code running comes from file
08:32:36.598 -> F:\MyPortable-PRgs\arduino-1.8.16-newb\portable\sketchbook\ESP32-connect-to-Wifi-Demo-001\ESP32-connect-to-Wifi-Demo-001.ino
08:32:36.598 -> compiled Dec 1 2021 08:32:18
08:32:36.684 -> WiFi.mode(WIFI_STA)
08:32:36.684 -> trying to connect to #your SSID#
08:32:37.036 -> ...............................
08:32:52.035 -> not yet connected executing ESP.restart();ets Jun 8 2016 00:22:57
08:32:52.035 ->
08:32:52.035 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
08:32:52.035 -> configsip: 0, SPIWP:0xee
08:32:52.035 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
08:32:52.073 -> mode:DIO, clock div:1
08:32:52.073 -> load:0x3fff0018,len:4
08:32:52.073 -> load:0x3fff001c,len:1216
08:32:52.073 -> ho 0 tail 12 room 4
08:32:52.073 -> load:0x40078000,len:9720
08:32:52.073 -> ho 0 tail 12 room 4
08:32:52.073 -> load:0x40080400,len:6352
08:32:52.073 -> entry 0x400806b8
08:32:52.309 -> Setup-Start
08:32:52.309 -> Code running comes from file
08:32:52.309 -> F:\MyPortable-PRgs\arduino-1.8.16-newb\portable\sketchbook\ESP32-connect-to-Wifi-Demo-001\ESP32-connect-to-Wifi-Demo-001.ino
08:32:52.309 -> compiled Dec 1 2021 08:32:18
08:32:52.405 -> WiFi.mode(WIFI_STA)
08:32:52.405 -> trying to connect to #Your SSID#
08:32:52.758 -> ...............................
08:33:07.788 -> not yet connected executing ESP.restart();ets Jun 8 2016 00:22:57
*/
Hey,
Thanks to you @StefanL38@gcjr. My password is 9 characters, and I think there isn't blocked adress on my box because I don't see mac adress in the box manager.
I tried your code @StefanL38 and this is what happened :
/* 18:24:18.307 -> WiFi.mode(WIFI_STA)
18:24:18.307 -> trying to connect to #freebox_PEKMIV#
18:24:18.307 -> WiFi.begin() done
18:24:18.307 -> WiFiStatus changed from
18:24:18.307 -> WL_IDLE_STATUS temporary status assigned when WiFi.begin() is called
18:24:18.307 ->
18:24:18.307 ->
18:24:18.307 -> to new status
18:24:18.307 -> WL_DISCONNECTED when disconnected from a network
18:24:18.307 ->
18:24:18.307 ->
18:24:18.646 -> .....WiFiStatus changed from
18:24:20.726 -> WL_DISCONNECTED when disconnected from a network
18:24:20.726 ->
18:24:20.726 ->
18:24:20.726 -> to new status
18:24:20.726 -> WL_NO_SSID_AVAIL when no SSID are available
18:24:20.726 ->
18:24:20.726 ->
18:24:21.132 -> ...................................................................................................................Hi there I'm the demo-code my IP address is: 0.0.0.0
18:25:20.150 -> Hi there I'm the demo-code my IP address is: 0.0.0.0 */
Is your router configured to hide the SSID?
Is in your router only activated 5 GHz and 2,4 GHz is disabled?
If you switch on "hotspot" in your smartphone. Configuring your hotspot with a 8 char password and you try to connect your ESP32 to this hotspot does this work?