espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.69k stars 7.42k forks source link

unreliable connecting to wifi #1212

Closed JohnGEyles closed 4 years ago

JohnGEyles commented 6 years ago

Hardware:

Board: HUZZAH32 Core Installation/update date: 8Jan2018 IDE name: Arduino IDE w/ Esspressif extensions Flash Frequency: 80Mhz Upload Speed: 921600

Description:

I'm having very unreliable behavior connecting to my home wifi network. It has worked intermittently in the past, and right now it's not connecting at all.

I have tried reloading my sketch. I have tried moving very close to my wifi router (to which other devices are successfully connecting). I have repeatedly tried pushing the reset button. I do not believe power is an issue, as (in addition to USB) I have a fully-charged (4+v) LiPo battery connected. I have tried disconnecting USB cable and battery for awhile (to completely power down).

I'd be thankful for any pointers as to how to debug this problem. If I should post this elsewhere, please advise.

I have only included my setup routine, as that's where the failure occurs (as it hangs in the while loop). The code is lifted from the SimpleWiFiServer built-in example. The 'blinkx' routine just turns the built-in LED on and off the specified numbe rof times.

Sketch:

#include <WiFi.h>
#include <SPI.h>

int i, j, num;

int enabs[] = {14, 15, 25, 27, 32, 33};

const char* ssid     = "ATT1530";
const char* password = "";

char numstr[20];

WiFiServer server(80);
void setup() {

  for (i=0; i<6; i++) {
      pinMode (enabs[i], OUTPUT);
      digitalWrite (enabs[i], HIGH);
   }

    Serial.begin(115200);
    pinMode(LED_BUILTIN, OUTPUT);      // set the LED pin mode

    delay(10);

    // blink LED, before trying wifi connect
   blinkx (3, 100, 500, 1000);

   // We start by connecting to a WiFi network

    Serial.println();
    Serial.println();
    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());

    server.begin();

    SPI.begin();}
}

Debug Messages:

Nothing, just a continuous series of dots as the while loop tries to connect.

lrmoreno007 commented 6 years ago

Add this before WiFi.begin();

WiFi.enableSTA(true);

lbernstone commented 6 years ago

If you set your board to esp32 dev module and turn on debugging, it should give you a lot more info about why it cannot connect.

JohnGEyles commented 6 years ago

WiFi.enableSTA(true) did not help.

JohnGEyles commented 6 years ago

I set my board to ESP32 Dev Module, and Core Debug Level to "Verbose", in the Tools dropdown (if that's what you intended).

All I got was this:

Connecting to ATT1530
[D][WiFiGeneric.cpp:265] _eventCallback(): Event: 2 - STA_START
............................
JohnGEyles commented 6 years ago

FWIW, I plugged in an old Airport Extreme I had lying around, and it seemed to connect:

Connecting to jge's Wi-Fi Network
[D][WiFiGeneric.cpp:265] _eventCallback(): Event: 2 - STA_START
......[D][WiFiGeneric.cpp:265] _eventCallback(): Event: 4 - STA_CONNECTED
[D][WiFiGeneric.cpp:265] _eventCallback(): Event: 7 - STA_GOT_IP
.
WiFi connected.
IP address: 

10.0.1.3

But the thing isn't configured correctly (a whole 'nother issue) so I'm not sure if it's really a viable connection, but it appears to be.

The wifi I'm attempting to connect to is a Netgear 7550 DSL gateway. (Lord willing, ATT is going to seem like a bad dream within a week or two).

JohnGEyles commented 6 years ago

Yep, I created another wifi network using the AIrport Extreme, and the ESP32 connects and the link functions properly. It's a mystery. Not a major malfunction of the ATT-supplied gateway, as other devices connect without issue. And not a range issue, as I was testing with the ESP32 several feet from the gateway.

I don't know if this remains an ESP32 issue, or if it's really an ATT/Netgear issue.

stickbreaker commented 6 years ago

@JohnGEyles My ESP32, a WeMos Battery&Bluetooth was giving me fits. Failing to connect, incomplete transmissions...

What I found was that my device is very orientation specific, my router is 20ft right of me:

Try reorienting your module.

    Serial.printf("New Client. RSSi: %ld dBm\n",WiFi.RSSI()); 

Chuck.

JohnGEyles commented 6 years ago

That's interesting, Chuck. I guess the ESP32 doesn't have an antenna pin. I'll mess around with it with that WiFi call. But I'm happy enough that it's robust with the Airport Extreme.

cjdell commented 6 years ago

I am also having trouble connecting to WiFi. This applies to any of the Arduino samples that I've tried that utilise WiFi.

Symptoms are strange. In almost all circumstances, WiFi fails to connect on boot, and will never work even after many hard boots.

Very occasionally, after flashing a new program, the WiFi will connect instantly upon the first boot, and will ALWAYS connect on subsequent reboots and hard reboots. It will continue to work reliably until I flash a new program (even if it's the same one) and then never work again.

Summarised, if it works upon first boot upon flashing, it will always work upon reboots, otherwise it will never work.

Tried many different routers, and also tried erasing the flash with esptool.py.

Can anyone shed light on this?

I have two of these modules (same problem on both): https://www.banggood.com/Wemos-TTGO-WiFi-Bluetooth-Battery-ESP32-0_96-Inch-OLED-Development-Tool-p-1213497.html?rmmds=search&cur_warehouse=CN

Thanks.

rmick commented 6 years ago

Yes Cjdell.

You have just described the exact same behaviour i am seeing with a Heltec WiFiKit32 module. It is driving me insane !

drrehak commented 6 years ago

hi, I just had troubling connecting my new Adafruit ESP32 Feather board with my Comcast router. but I got it to work when I tried the 2.4ghz channel SSID. worth a try for anyone that came across this issue...

RobbertH commented 6 years ago

Hi there, I'm having issues too. The first time I connected, everything went fine, but from the first reupload onwards, it kept doing this (verbose debug output):

Connecting to [...]
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 13 - AP_START
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 13 - AP_START
.....[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:309] _eventCallback(): Reason: 201 - NO_AP_FOUND
..............................
RobbertH commented 6 years ago

Strange. Connected to my android hotspot, then tried connecting to the normal router again. Everything works fine again.

rmick commented 6 years ago

I had the exact same problem, and it drove me crazy for ages. It all started when I added a 2nd Wifi mode to the sketch on my Heltec WIFIkit32 ESP32 module.

This is the solution I found. My unit now works perfectly 99% of the time.

    //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(1000);
    WiFi.mode(WIFI_STA);
    delay(1000);
    Serial.print("WIFI status = ");
    Serial.println(WiFi.getMode());
    // End silly stuff !!!

   // Connect to provided SSID and PSWD
    WiFi.begin(otaSSID, otaPSWD);

The problem seems to be that the ESP32 remembers the last WifiMode setting when it reboots, so you must interrogate it, disconnect it, and interrogate it again, to reset it - then you can initialise it. If you do not do the above little dance, then the Init fails.

dpondaco commented 6 years ago

regarding rmick comments above. I tried the code recommended, but got a bunch of compiler errors. Do you have a list the the libraries the should be inlcuded in the ESP32 reset sketch. I am having same problem as everyone else and not sure where to go from here.

Bodmer commented 5 years ago

I am here because I have the same issue. With seemingly identical TTGO boards some connect quickly running identical code and others take minutes or never connect.

Changing one irrelevant line of code like a print statement suddenly seems to make it work. Then adding more code breaks it again. In the worst case it seems there is about a 1 in 4 chance that adding a few extra lines of code will make these boards work. Once they are persuaded to work they connect reliably in any power-up or reboot.

This suggest that something is not being initialized properly in the WiFi library but that most board happen to come up in an acceptably configured state. It is going to be difficult to track the real cause down until one of the core developers gets hold of one of these rogue boards!

rodmcm commented 5 years ago

I am also having the same problem, downloaded ESP32 board to Arduino last week and nothing but problems. Old programs that tests AP and STA WIFI connections do not work on AP mode.. Also I see lots of reboots when a PC or laptop attempts to connect... I am sure that the ESP32 core has issues...

crimeacs commented 5 years ago

I also have the same problem with my ESP32-WROOM. I am able to create AP and connect to it from my phone or PC, but not the other way around. I have tested my ESP32 with several routers, different SSIDs etc, but it always says: Authentication failed. Nor did it help to erase flash, of do WiFi.disconnect() trick, nor ESP.reboot() trick.

a-c-sreedhar-reddy commented 5 years ago

Is there anyone who solved this ? Please share the solution.

GithubMystic commented 5 years ago

Hi Guys. I have just posted an Issue (255) regarding ==> Unreliable behaviour on ESP32 but reliable on ESP8266 under MQTT https://github.com/knolleary/pubsubclient/issues/554 Reading the post you guys made, I get the feeling that these two posts might be related. It would be interesting to see if anyone gets the same symptoms as me when testing the ESP32 as I have done in my post. Would anyone be prepared to try this ? hopefully this would help to get to the bottom of these issues.

lbernstone commented 5 years ago

Please do not hijack threads. Your issue is clearly not the same.

a-c-sreedhar-reddy commented 5 years ago

hey @lberstone did you find the solution for this problem

GithubMystic commented 5 years ago

hi @lberston. I do object somewhat to being accused of hijacking a thread. In my opinion I can see similarities in the underlying issue of the unreliability of WiFi on the ESP32. Just because they manifest themselves in the way I am using it as per my post does not mean that the underlying issue causing the issues in my post are not the same. The forums are for sharing and helping each other with issues and where someone sees an issue related to theirs, we should all be trying to help each other where possible. To anyone else reading this post, please do consider that the underlying issues in this post and my post could very well be related, as they pertain to unreliablity of WiFi on the ESP32 that so far has not been identified. Many thanks

JohnGEyles commented 5 years ago

I don't really understand the electro-politics of this situation with the ESP32, so maybe this comment makes no sense, but I'm a bit confused that the manufacturers of the ESP32 are not addressing this at all. The product is supposed to support Wifi connectivity. Lots of people are having a variety of problems making it actually work. What am I missing ?

GithubMystic commented 5 years ago

Hi JohnGEyles. I totally agree with you . I have researched this quite extensively and no one is really addressing this issue. With an ESP8266 with the identical environment and sketch, everything runs rock solid. The only difference is the ESP32 and the WiFi library so the unreliability has to stem from that It is possible in my case the MQTT has issues with the ESP32 library, but very unlikely. For now I am forced to stay with the ESP8266 until this has a resolution or I can find a workaround. I would love to be able to use the ESP32 reliably to have access to all those additional functions and pins.

eziosoma commented 5 years ago

this other issue seems to fix the problem. https://github.com/espressif/arduino-esp32/issues/1147

eziosoma commented 5 years ago

Tryed #1147, the problem still presents

bertmelis commented 5 years ago

Unless you can share some debug output we can not investigate.

Pythonaire commented 5 years ago

Idea ... Me too, stumbling about unstable Wifi connections. I use the ESP32 for sensoring. Now I read in the ESP32 manufacturer description, the ADC2 Pins/GPIO/Interrupts are used by the Wifi driver. So, measuring with ADC2 Pins and Wifi at the same time produce incomprehensible errors. Same thing on other Pins blocked by the systems for internal use.

lumtvn commented 5 years ago

Hello everyone. I seem to have a similar issue. I worked for days with this library with no problems at all until today. I tried to mess with the WiFi connection to see if the esp32 would reconnect on its own, but not only it didn't reconnect, it also failed to connect ever again. I tried re-uploading the sketch and even tried SimpleWifiServer example with no success.

I set verbose debug level on console with esp_log_level_set("*", ESP_LOG_VERBOSE); and something came up; right after Wifi.begin(ssid,passwd), the esp32 writes a few lines in console (thanks to debug) describing the actions taken in attempting to connect to a network. This is the output i got:

I (218887) wifi: new:<6,0>, old:<6,0>, ap:<255,255>, sta:<6,0>, prof:1 17:14:08.108 -> I (219621) wifi: state: init -> auth (b0) 17:14:08.108 -> I (219629) wifi: state: auth -> assoc (0) 17:14:08.108 -> I (219633) wifi: state: assoc -> run (10) 17:14:08.950 -> I (220473) wifi: connected with MLM, channel 6, bssid = ##:##:##:##:##:## 17:14:08.950 -> I (220478) wifi: pm start, type: 1

"MLM" is the name of the WiFi network i'm attempting to connect. So it would seem, with this output, that the esp32 successfully connects to the network. But it's not happening. The debug lines shows it has connected, but the connection loop never breaks, it just keeps trying to connect and showing the same output over and over again. The condition (WiFi.status() == WL_CONNECTED) never seems to return true. Does this mean it's a problem with the WiFi library?.

this is the loop for connecting to the WiFi:

while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
    if (count == 15){
        WiFi.begin(ssid, password);
        count = 0;
        Serial.println();
        Serial.print("Retrying connection...");
        delay(5000);
    }
   count ++;
}
rodmcm commented 5 years ago

This is a variation on an above post, but has cured my problems. Disconnect both AP and STA mode with a long delay

    WiFi.disconnect(true);                     // disconnects STA Mode
    delay(1000);
            WiFi.softAPdisconnect(true);           // disconnects AP Mode 
    delay(1000);  
rodmcm commented 5 years ago

I also agree that there seems to be no one taking control of this issue, Seems to me that we as users need to somehow bypass this Forum and send our concerns direct to Espressif. They are the product manufacturer after all

Another option is to go with another supplier.. Anyone had success with anything else?

BaaridunNasr commented 5 years ago

@LuchoManto

//--------------------------------------------------------------------------------------------------
// Function Details
//--------------------------------------------------------------------------------------------------
// Name : wifiClear
// Purpose : clears all wifi settings before connecting. must be run before every wifiConnect
// Argument(s) : void
// Returns : void

void ESP32Wifi::wifiClear()
{
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    wifiStatus = WiFi.status() == WL_CONNECTED;
    delay(100);
}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
// Function Details
//--------------------------------------------------------------------------------------------------
// Name : wifiConnect
// Purpose : connects to given ssid using password for given number of tries. updates the wifiStatus.
// Argument(s) : char *SSID, char *PASS, unsigned short tries
// Returns : void

void ESP32Wifi::wifiConnect(char *SSID, char *PASS, unsigned short tries)
{
    _SSID = SSID;
    _PASS = PASS;
    wifiStatus = WiFi.status() == WL_CONNECTED;
    while ((tries > 0) && (!wifiStatus))
    {
        wifiClear();
        WiFi.begin(_SSID, _PASS);
        delay(5000);
        wifiStatus = WiFi.status() == WL_CONNECTED;
        tries--;
    }
}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
// Function Details
//--------------------------------------------------------------------------------------------------
// Name : wifiDisconnect
// Purpose : disconnects from the network and switches off the radio. sets wifiStatus to false.
// Argument(s) : void
// Returns : void

void ESP32Wifi::wifiDisconnect()
{
    WiFi.disconnect();
    WiFi.mode(WIFI_OFF);
    wifiStatus = false;
}
//--------------------------------------------------------------------------------------------------

Code that I use for connecting and disconnecting WiFi. Works pretty good. Solid connects and it kills the connection and reconnects when I want it to. You could try this and let me know if you have any issues.

GithubMystic commented 5 years ago

RodMcm As I mentioned in a previous post, I have given up on the ESP32 / WiFi / MQTT combination due to unreliablility. I have spent far too much time trying all the permutations from forums, etc,... I have switched to Raspberry Pi zeroW for all my Wifi/Mqtt nodes. These are now rock solid and never miss a beat. As I already use Raspberry pi for my automation hub, it also simplifies my whole environment. Let me know if you need more details

tavdog commented 5 years ago

I am seeing this issue with the Heltec ESP32 Wifi Kit. Has anyone figured out a fix for this ?

kenken64 commented 5 years ago

3:32:40.039 -> I (33) wifi: wifi driver task: 3ffb4e6c, prio:23, stack:3584, core=0 03:32:40.074 -> I (105) wifi: wifi firmware version: 6b44342 03:32:40.074 -> I (105) wifi: config NVS flash: enabled 03:32:40.074 -> I (107) wifi: config nano formating: disabled 03:32:40.074 -> I (127) wifi: Init dynamic tx buffer num: 32 03:32:40.074 -> I (127) wifi: Init data frame dynamic rx buffer num: 10 03:32:40.074 -> I (127) wifi: Init management frame dynamic rx buffer num: 10 03:32:40.108 -> I (130) wifi: Init static rx buffer size: 1600 03:32:40.108 -> I (134) wifi: Init static rx buffer num: 4 03:32:40.108 -> I (138) wifi: Init dynamic rx buffer num: 10 03:32:40.143 -> I (209) wifi: mode : sta (3c:71:bf:4d:6e:4c) 03:32:40.177 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 0 - WIFI_READY 03:32:40.177 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 2 - STA_START 03:32:40.282 -> I (347) wifi: new:<9,0>, old:<1,0>, ap:<255,255>, sta:<9,0>, prof:1 03:32:41.047 -> I (1087) wifi: state: init -> auth (b0) 03:32:41.047 -> I (1089) wifi: state: auth -> assoc (0) 03:32:41.047 -> I (1092) wifi: state: assoc -> init (6c0) 03:32:41.047 -> I (1092) wifi: new:<9,0>, old:<9,0>, ap:<255,255>, sta:<9,0>, prof:1 03:32:41.047 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 5 - STA_DISCONNECTED 03:32:41.047 -> [W][WiFiGeneric.cpp:357] _eventCallback(): Reason: 203 - ASSOC_FAIL 03:32:41.081 -> I (1105) wifi: flush txq 03:32:41.081 -> I (1107) wifi: stop sw txq 03:32:41.081 -> I (1110) wifi: lmac stop hw txq 03:32:41.081 -> I (1120) wifi: mode : sta (3c:71:bf:4d:6e:4c) 03:32:41.081 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 0 - WIFI_READY 03:32:41.081 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 2 - STA_START 03:32:41.081 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 2 - STA_START 03:32:41.185 -> I (1244) wifi: new:<9,0>, old:<1,0>, ap:<255,255>, sta:<9,0>, prof:1 03:32:41.185 -> I (1244) wifi: state: init -> auth (b0) 03:32:41.220 -> I (1246) wifi: state: auth -> assoc (0) 03:32:41.220 -> I (1249) wifi: state: assoc -> run (10) 03:32:41.220 -> I (1267) wifi: connected with kenken64, channel 9, bssid = 16:0c:c3:e4:26:00 03:32:41.255 -> I (1306) wifi: pm start, type: 1 03:32:41.255 -> 03:32:41.255 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 4 - STA_CONNECTED 03:33:07.705 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 7 - STA_GOT_IP 03:33:07.705 -> [D][WiFiGeneric.cpp:385] _eventCallback(): STA IP: 192.168.1.56, MASK: 255.255.255.0, GW: 192.168.1.254 03:33:07.705 -> [27686] 03:33:07.705 -> 03:33:07.705 -> / )/ /_ ___ / / 03:33:07.740 -> / / / // / \/ '/ 03:33:07.740 -> /____//_, /////_\ 03:33:07.740 -> // v0.6.1 on ESP32 03:33:07.740 -> 03:33:07.740 -> [27699] Connecting to blynk-cloud.com:80 03:33:07.808 -> [27803] Ready (ping: 75ms).

yoursunny commented 5 years ago

I have a unit that won't connect to WiFi. DEBUG logging shows warning 4WAY_HANDSHAKE_TIMEOUT. After esptool.exe --port COM8 erase_flash, the problem disappears. You may give this a try.

ESP32 can store some WiFi parameters on the flash, so that it can start connecting to the access point before user sketch starts executing. This improves performance and saves energy when deep sleep is used. However, if the stored WiFi parameters are somehow corrupted, it could cause strange problems. My sketch normally contains WiFi.persistent(false) so that the WiFi parameters aren't stored, but many examples out there do not have this line.

carlerickrowan commented 5 years ago

My sketch normally contains WiFi.persistent(false) so that the WiFi parameters aren't stored, but many examples out there do not have this line.

I think WiFi.perisistent() implementation is already changed and default to false that's why you don't see it on examples.

eos1d3 commented 5 years ago

Just to share similar thing and I hope that may help a little to find the problem.

I am using Asus AC-86U router (2.4GHz mode) with ESP-32 without problem. When I use Android hotspot (LG V20, latest firmware) to connect repeatedly, it will always fail after a successful connection. This means it will connect at 1, 3, 5 connections, and will always fail at 2, 4, 6....

Using standard code:

while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

There are two strange things. If I change 500 to 1000 or more, and add WiFi.begin(ssid, password); it will always connect. ( I tried 750 but it still fails.)

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(1000);
    Serial.print(".");

    WiFi.begin(ssid, password);
  }

And another strange thing is, Asus 86U works only with standard code. Adding extraWiFi.begin(ssid, password); in while loop it will never work!

@LuchoManto I just read your code with 15 retrial code. It seems the fix is similar to mine. But mine takes very short time to get connection, please try too. And I agree with you, WiFi.status() will never return true if it gets first false.

PS: From my test, normal router or mobile hotspot may give different results.

PPS: I found something different now. This is the auto-connection settings I am using:

  WiFi.setAutoConnect(true);
  WiFi.setAutoReconnect(true);

With Asus router, ESP32 can auto-reconnect if connection is lost. But when using mobile hotspot, it will never re-connect. This seems hotspot and ESP32 do not retry for FIRST connection AND also reconnection if connection is broken.

tavdog commented 5 years ago

Why is the ESP32 so bad at this wifi stuff while the ESP8266 works great all the time ?

YHatahet commented 5 years ago

Why is the ESP32 so bad at this wifi stuff while the ESP8266 works great all the time ?

I'm guessing it has to do with the fact that the 8266 is cheaper, and hence more people end up using and supporting it. Also ESP32 runs on Free RTOS, and using Arduino IDE to program it is basically running a massive library (according to my understanding, please correct me if I'm wrong).

rumoldus commented 5 years ago

There is definitely a relation with interrupts on specific pins. According to documentation, ADC2 is not to be used combined with Wifi. I found on another forum a suspicion that interrupts with the same pins also cause trouble. I also experienced Wifi problems, but when I remove all AttachInterrupts from pins 25, 26 and 32, Wifi works like a charm.

rumoldus commented 5 years ago

ADC2 uses GPIOs 0, 2, 4, 12 - 15 and 25 - 27. Praise to Pythonaire (see issue 1829).

Ajioz commented 5 years ago

@LuchoManto

//--------------------------------------------------------------------------------------------------
// Function Details
//--------------------------------------------------------------------------------------------------
// Name : wifiClear
// Purpose : clears all wifi settings before connecting. must be run before every wifiConnect
// Argument(s) : void
// Returns : void

void ESP32Wifi::wifiClear()
{
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    wifiStatus = WiFi.status() == WL_CONNECTED;
    delay(100);
}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
// Function Details
//--------------------------------------------------------------------------------------------------
// Name : wifiConnect
// Purpose : connects to given ssid using password for given number of tries. updates the wifiStatus.
// Argument(s) : char *SSID, char *PASS, unsigned short tries
// Returns : void

void ESP32Wifi::wifiConnect(char *SSID, char *PASS, unsigned short tries)
{
    _SSID = SSID;
    _PASS = PASS;
    wifiStatus = WiFi.status() == WL_CONNECTED;
    while ((tries > 0) && (!wifiStatus))
    {
        wifiClear();
        WiFi.begin(_SSID, _PASS);
        delay(5000);
        wifiStatus = WiFi.status() == WL_CONNECTED;
        tries--;
    }
}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
// Function Details
//--------------------------------------------------------------------------------------------------
// Name : wifiDisconnect
// Purpose : disconnects from the network and switches off the radio. sets wifiStatus to false.
// Argument(s) : void
// Returns : void

void ESP32Wifi::wifiDisconnect()
{
    WiFi.disconnect();
    WiFi.mode(WIFI_OFF);
    wifiStatus = false;
}
//--------------------------------------------------------------------------------------------------

Code that I use for connecting and disconnecting WiFi. Works pretty good. Solid connects and it kills the connection and reconnects when I want it to. You could try this and let me know if you have any issues.

I get this error while trying to compile it:

ESP32Wifi has not been declared

stale[bot] commented 4 years ago

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

nacpem commented 4 years ago

I am getting an errie behaviour with the ESP32. The ESP32 connects to the wifi network. I have a rabbitmq broker working on the server side.

The ESP32 wifi network is unstable and after reconnection it resend the MQTT message

DATA=data
I (1017135) wifi: new:<6,1>, old:<6,1>, ap:<6,1>, sta:<6,1>, prof:6
I (1019075) wifi: new:<6,1>, old:<6,1>, ap:<6,1>, sta:<6,0>, prof:6
I (1020105) wifi: new:<6,1>, old:<6,1>, ap:<6,1>, sta:<6,1>, prof:6
I (1023075) wifi: new:<6,1>, old:<6,1>, ap:<6,1>, sta:<6,0>, prof:6
I (1025125) wifi: new:<6,1>, old:<6,1>, ap:<6,1>, sta:<6,1>, prof:6
11. I (1031395) MQTT: Other event id:7
I (1031395) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000
E (1031415) MQTT_CLIENT: mqtt_message_receive: transport_read() error: errno=128
E (1031415) MQTT_CLIENT: mqtt_process_receive: mqtt_message_receive() returned -1
5. I (1031425) MQTT: MQTT_EVENT_DISCONNECTED
I (1031465) MQTT: MQTT_EVENT_CONNECTED
1. I (1031465) MQTT: sent publish successful, msg_id=38383
2. I (1031465) MQTT: sent subscribe successful, msg_id=37502
3. I (1031475) MQTT: sent subscribe successful, msg_id=10356
4. I (1031475) MQTT: sent unsubscribe successful, msg_id=42238
8. I (1031485) MQTT: MQTT_EVENT_PUBLISHED, msg_id=38383
6. I (1031495) MQTT: MQTT_EVENT_SUBSCRIBED, msg_id=37502
I (1031495) MQTT: sent publish successful, msg_id=0
6. I (1031505) MQTT: MQTT_EVENT_SUBSCRIBED, msg_id=10356
I (1031505) MQTT: sent publish successful, msg_id=0
7. I (1031515) MQTT: MQTT_EVENT_UNSUBSCRIBED, msg_id=42238
9. I (1031515) MQTT: MQTT_EVENT_DATA
TOPIC=/topic/qos0
DATA=data
9. I (1031525) MQTT: MQTT_EVENT_DATA
TOPIC=/topic/qos0
DATA=data
I (1039145) wifi: new:<6,1>, old:<6,1>, ap:<6,1>, sta:<6,0>, prof:6
I (1040075) wifi: new:<6,1>, old:<6,1>, ap:<6,1>, sta:<6,1>, prof:6
I (1046115) wifi: new:<6,1>, old:<6,1>, ap:<6,1>, sta:<6,0>, prof:6
11. I (1046425) MQTT: Other event id:7
I (1046425) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000
E (1046435) MQTT_CLIENT: mqtt_message_receive: transport_read() error: errno=128
E (1046435) MQTT_CLIENT: mqtt_process_receive: mqtt_message_receive() returned -1
5. I (1046445) MQTT: MQTT_EVENT_DISCONNECTED
I (1046475) MQTT: MQTT_EVENT_CONNECTED
1. I (1046485) MQTT: sent publish successful, msg_id=42926
2. I (1046485) MQTT: sent subscribe successful, msg_id=10539
3. I (1046485) MQTT: sent subscribe successful, msg_id=25154
4. I (1046495) MQTT: sent unsubscribe successful, msg_id=51792
8. I (1046505) MQTT: MQTT_EVENT_PUBLISHED, msg_id=42926
6. I (1046515) MQTT: MQTT_EVENT_SUBSCRIBED, msg_id=10539
I (1046515) MQTT: sent publish successful, msg_id=0
6. I (1046525) MQTT: MQTT_EVENT_SUBSCRIBED, msg_id=25154
I (1046525) MQTT: sent publish successful, msg_id=0
7. I (1046535) MQTT: MQTT_EVENT_UNSUBSCRIBED, msg_id=51792
9. I (1046535) MQTT: MQTT_EVENT_DATA
TOPIC=/topic/qos0
DATA=data
9. I (1046545) MQTT: MQTT_EVENT_DATA
TOPIC=/topic/qos0
DATA=data

Any idea how to work around this ?

Im using ESP-IDF framework

stale[bot] commented 4 years ago

[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future.

stale[bot] commented 4 years ago

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

kingyyp commented 4 years ago

I am using a ESP32-cam for monitoring , however, the video stream is not showing regularly , mostly after 5-6 hours from power on. i can see someone try raspberrypi as alternative, will try that as well. see if raspberrypi can be more stable.

stale[bot] commented 4 years ago

[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future.