Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esp32 wifi not connecting to network #1829

Closed
oly99 opened this issue Sep 2, 2018 · 25 comments
Closed

esp32 wifi not connecting to network #1829

oly99 opened this issue Sep 2, 2018 · 25 comments

Comments

@oly99
Copy link

oly99 commented Sep 2, 2018

Please fill the info fields, it helps to get you faster support ;)

If you have a Guru Meditation Error, please decode it:
https://github.com/me-no-dev/EspExceptionDecoder

----------------------------- Remove above -----------------------------

Hardware:

Board: ESP32 Dev Module
Core Installation/update date: esp has not been updated firmware is about 2 years old
IDE name: Arduino IDE
Flash Frequency: 40Mhz?
Upload Speed: 115200?

Description:

the esp32 wont connect to my network it just says connecting to wifi over and over and never connects
it was working then it stopped could updating the esp32 firmware fix this or is it another issue , also is there a different way to connect to wifi that's different to the :

WiFi.begin(ssid,password);

while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi..");
delay(300);

}

that might fix this issue

Sketch:

//Change the code below by your sketch
#include <Adafruit_Sensor.h>
//#include "DHT.h"
#include <WiFi.h>
#include <PubSubClient.h>
#include "SparkFunHTU21D.h"
//Create an instance of the object
HTU21D myHumidity;

const char* ssid = "skynet";
const char* password = "Sexyfrog9919";
//const char* mqttServer = "192,168,0,196";
IPAddress mqttServer(192,168,0,196);
const int mqttPort = 1883;
const char* mqttUser = "ojosbourne";
const char* mqttPassword = "Skingsking9919";
//#define mqttPort "1883"
#define temperature_topic "topic/temp"       //Topic temperature
#define humidity_topic "topic/humid"         //Topic humidity
//#define debug_topic "debug"                   //Topic for debugging


//here we use pin IO23 of ESP32 to read data
//#define DHTPIN 14
//our sensor is DHT22 type
//#define DHTTYPE DHT22
//create an instance of DHT sensor
//DHT dht(DHTPIN, DHTTYPE);

WiFiClient espClient;
PubSubClient client(espClient);


void setup() {
//void setup() (

//   connect to wifi 



Serial.begin(115200);
WiFi.disconnect(true);
delay(3000);

//DO NOT TOUCH
    //  This is here to force the ESP32 to reset the WiFi and initialise correctly.
 //   Serial.print("WIFI status = ");
  //  Serial.println(WiFi.getMode());
  // WiFi.disconnect(true);
   // delay(3000);
   // WiFi.mode(WIFI_STA);
   // delay(1000);
    //Serial.print("WIFI status = ");
    //Serial.println(WiFi.getMode());
    // End silly stuff !!!

WiFi.begin(ssid,password);
 
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi..");
delay(300);

}
 
Serial.println("Connected to the WiFi network");
 // connect to mqtt

client.setServer(mqttServer, mqttPort);
//mqttClient.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
 delay(300);

if (client.connect( "ESP32Client", mqttUser, mqttPassword )) {
 
Serial.println("connected");
 delay(300);

} else {
 
Serial.print("failed with state ");
Serial.print(client.state());
delay(200);
 
}
//}

  
  //Serial.begin(115200);
  //Serial.println("DHT22 sensor!");
  //call begin to start sensor
  //dht.begin();

  Serial.println("HTU21D Example!");

  myHumidity.begin();
}

}
 



void loop() {

  float h = myHumidity.readHumidity();
  float t = myHumidity.readTemperature();

  
  //use the functions which are supplied by library.
  //float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  //float t = dht.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from htu21d sensor!");
    return;
  }
  // print the result to Terminal
  //client.publish(humidity_topic, (h));
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
 // client.publish(temperature_topic, (t));
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");

   // Publish values to MQTT topics
    client.publish(temperature_topic, String(t).c_str(), true);   // Publish temperature on mosohomes/temp1
    //if ( debug ) {    
      Serial.println("Temperature sent to MQTT.");
    //}
    delay(200); //some delay is needed for the mqtt server to accept the message
    
    client.publish(humidity_topic, String(h).c_str(), true);      // Publish humidity on mosohomes/humid1
    
    //if ( debug ) {
                  Serial.println("Humidity sent to MQTT.");
                 //}
  //we delay a little bit for next read
  delay(10000);
}



### Debug Messages:

[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 0 - WIFI_READY
Connecting to WiFi..
Connecting to WiFi..
Connecting to WiFi..
Connecting to WiFi..
Connecting to WiFi..
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:360] _eventCallback(): Reason: 202 - AUTH_FAIL
Connecting to WiFi..
Connecting to WiFi..
Connecting to WiFi..
Connecting to WiFi..
Connecting to WiFi..
Connecting to WiFi..
Connecting to WiFi..
Connecting to WiFi..
@lbernstone
Copy link
Contributor

Looks like an incompatible WiFi security. What is your AP and how is it set up?

@oly99
Copy link
Author

oly99 commented Sep 2, 2018

@lbernstone its a tenda ac18 with standard setup do need to give the esp32 a fixed ip then ?
the rouer has ICMP Flood Attack Defense:
TCP Flood Attack Defense:
UDP Flood Attack Defense:
Forbid WAN Ping:
all enabled

@lbernstone
Copy link
Contributor

The device is not authenticating, so a static IP is not going to work either. Your AP is not allowing the esp to connect, so you will need to wireshark the process to see what is failing.

@panther1
Copy link

panther1 commented Sep 3, 2018

I am having the same problem. Stable esp code now very seldomly connects to Arris XB6 modem since it was installed.

[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:360] _eventCallback(): Reason: 2 - AUTH_EXPIRE
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:360] _eventCallback(): Reason: 2 - AUTH_EXPIRE
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:360] _eventCallback(): Reason: 2 - AUTH_EXPIRE
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 2 - STA_START

@jjassar
Copy link

jjassar commented Sep 3, 2018

In my case ESP32 dev kit does not reconnect automatically, i need to either call WiFi.reconnect() or Wifi.Begin() again in loop.

@lbernstone
Copy link
Contributor

OP has a very specific connection error. If you do not have this problem, or a solution for him, then you are not solving the issue.

@oly99
Copy link
Author

oly99 commented Sep 3, 2018

@lbernstone thanks i will install wireshark and have a look tonight
is there anything specific i need to be looking for in the wifi packets

@oly99
Copy link
Author

oly99 commented Sep 3, 2018

@lbernstone ok i turned on my guest network and it connects to 192.168.10.139
but gateway etc is 192.168.0.1 so im guessing that 192.168.10.xxx is to do with it being the guest network separate from my other one

@oly99
Copy link
Author

oly99 commented Sep 3, 2018

i did notice something weard with my other esp sonnoff that it kept on being given 192.168.0.1 as its ip and then would not connect after as thats reserved for the main router address i had to mess with tasmota to get it a fixed ip now its ok i think it was because IP Address was 192.168.0.1
Subnet Mask:
255.255.255.0
DHCP Server had
IP Address Range:
192.168.0. 1 -200
so this clashed i have now changed the dhcp ip range to 10-200 not 1-200 .
i dont think that was helping either
hopped back on regular AP not working again

@oly99
Copy link
Author

oly99 commented Sep 4, 2018

ok looks like ive found the issue i had to add the esp32 mac address to my routers mac filtering white list
it now connects at least at the moment the mqtt is not connecting how ever but i will check the code to see if its set correctly

@oly99
Copy link
Author

oly99 commented Sep 4, 2018

ok got mqtt to connect and now can see on node red so all seems ok and fixed

many thanks for your help

@Pythonaire
Copy link

It seem for me, you use Pin14 and 22 for measuring temperature. These Pins are ADC2 pins. As described by the ESP32 manufacturer, the ADC2 pins are used by the Wifi driver. you can use these Pins for ADC measuring if the Wifi is off (driver unloaded).Me too, stumble about this and wondering about unstable Wifi.

@lmayainsite
Copy link

I'm using pin14 for digital purposes (digital output to drive a LED). I've had some wifi connection problems in my projects, and I don't know if those problems are by use this pin.

Are there some problems for wifi connections if pin14 is used as a digital output?

@Pythonaire
Copy link

As mentioned... GPIO14 (the interrupt) is used by Wifi. You Need to disable Wi-Fi first, in you like to use the Pin for other things.

@lmayainsite
Copy link

lmayainsite commented May 6, 2019

Pytho

As mentioned... GPIO14 (the interrupt) is used by Wifi. You Need to disable Wi-Fi first, in you like to use the Pin for other things.

Could you show me where do you find that in the documentation?

I understoot from documentation this (tell me if I wrong): If pin14 will be use for ADC purposes, Wi-Fi must be disable, because is a requirement for ADC, not for wifi-driver.

Thanks.

@Pythonaire
Copy link

Pythonaire commented May 7, 2019

I think, it is not related to the special function of analog-digital conversion. It is related to the system itself, the control of interrupts, piping of signal flow. Pin14 can be used for IO, but shares the interrupt with Wifi. That can leads into misfunction while switching between Wifi and IO on system driver level. My suspicion: the same behavior have the I2C Pins.

https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/adc.html

@baodo3133s
Copy link

baodo3133s commented Jun 27, 2019

@oly99 , i am having the same issue, after you added the mac address onto your filter list did you do anything else to get the esp32 to connect t wifi?

@remata
Copy link

remata commented Sep 24, 2019

I have same observations as Pythonaire - if I use ADC2 pins for something that requires interrupts this impacts WiFi. In my case I used ports 26 and 27 for UART2 and WiFi couldn't connect to AP after soft reset. After moving UART2 pins to 16, 17, there were no more issues with WiFi connectivity.

@DhrubojyotiDey
Copy link

In my case ESP32 dev kit does not reconnect automatically, i need to either call WiFi.reconnect() or Wifi.Begin() again in loop.

Can you please share the code? And is there any way to compile micropython in arduino?

@arvabhatrajesh
Copy link

esp32 WIFI not connecting for MOTO mobile
I tried simple wifi code

#include "WiFi.h"

const char* ssid = "motog4";
const char* password = "mymobilemoto";

void setup() {

Serial.begin(115200);
//connect to WiFi
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}

void loop() {}

other mobile hotspot is connecting and only moto hotspot is not connecting
solve this anyone Wise.....
thank you

@Salman1309
Copy link

Check for ESP status, if you get AUTH Fail at that time only disconnect and then reconnect. it works!!

@rfox12
Copy link

rfox12 commented Jun 9, 2020

I downgraded from the 5ghz wifi ssid to 2.4ghz version and it worked for me. Hope it helps someone else.

@RafaTaborga
Copy link

I am having this problem:
wifi station: retry to connect to the AP
It says that the ESP32 is already in station mode, but will not get an IP or "connect" completely to the network.
Did anyone have this problem?
Please, I need to have my network by Monday

@IanHarris6
Copy link

Was anyone able to get it to work?

@rob-bits
Copy link

I do not know if my issue is related but I had connection problems. First it was connecting to router pretty fast then after restart of the board it did not. I got a plenty of disconnect event, something like this:
...[ 2703][V][WiFiGeneric.cpp:362] _arduino_event_cb(): STA Disconnected: SSID: ****, BSSID: xx:d9:4d:xx:da:46, Reason: 2

My problem was hardware related. I had 10uF + 100nF capacitors on the 3.3V lines. I am using ESP32-Wrover and the datasheet recommends 22uF capacitors and in the wrover-kit reference design a 100uF capacitor was used. I hacked 100uF capacitor to my board and the WiFi issue dismissed. It seems like working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests