Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

Can not connect to some routers

Open odilonafonso opened this issue 6 years ago • 38 comments

Platform

  • Hardware: [ESP-12]
  • Core Version: [.6.3]
  • Development Env: [Arduino IDE]
  • Operating System: [Ubuntu]

Settings in IDE

  • Module: [LOLIN(Wemos) D1 R2 & mini]
  • Flash Mode: [dio]
  • Flash Size: [2MB/1MB]
  • lwip Variant: [v2 Lower Memory]
  • Reset Method: [--before default_reset --after hard_reset]
  • Flash Frequency: [40Mhz]
  • CPU Frequency: [80Mhz|160MHz]
  • Upload Using: [SERIAL]
  • Upload Speed: [921600]

Problem Description

ESP is unable to connect to some routers. On some routers it works perfectly; others do not. I was unable to identify the difference between the routers that explains this differentiated operation. I tried to change the ssid, password. It does not connect and after 250 seconds it connects and obtain a strange IP. It does not appear to be a LAN IP.

Sketch

#include <ESP8266WiFi.h> // Include the Wi-Fi library

const char* ssid = "CLARO2G"; // The SSID (name) of the Wi-Fi network you want to connect to const char* password = "mypassword"; // The password of the Wi-Fi network

void setup() { Serial.begin(115200); // Start the Serial communication to send messages to the computer delay(10); Serial.println('\n');

Serial.println("\n\n"); WiFi.begin(ssid, password); // Connect to the network Serial.print("Connecting to "); Serial.print(ssid); Serial.println(" ...");

int i = 0; int ws; long tini = millis(); long tnow; #define INTERVAL 10000 while ( (ws = WiFi.status()) != WL_CONNECTED) { // Wait for the Wi-Fi to connect tnow = millis(); if ( tnow - tini > INTERVAL ) { ++i; Serial.println("Not connected after " + String(i*INTERVAL/1000) + "secs status:" + String(ws)); tini = tnow; } yield(); }

Serial.println('\n'); Serial.println("Connection established!");
Serial.print("IP address:\t"); Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer }

void loop() {}

Debug Messages

SDK:2.2.2-dev(38a443e)/Core:2.6.3=20603000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-16-ge23a07e/BearSSL:89454af

mode : sta(5c:cf:7f:36:89:dc) + softAP(5e:cf:7f:36:89:dc) add if0 Connecting to CLARO2G ... wifi evt: 8 wifi evt: 2 scandone state: 0 -> 2 (b0) state: 2 -> 3 (0) state: 3 -> 5 (10) add 0 aid 2 cnt

connected with CLARO2G, channel 6 dhcp client start... wifi evt: 0 Not connected after 10secs status:6 wifi evt: 7 wifi evt: 7 wifi evt: 7 wifi evt: 7 Not connected after 20secs status:6 wifi evt: 7 wifi evt: 7 wifi evt: 7 wifi evt: 7 wifi evt: 7 wifi evt: 7 wifi evt: 7 wifi evt: 7 wifi evt: 7 Not connected after 30secs status:6 Not connected after 40secs status:6 Not connected after 50secs status:6 wifi evt: 7 wifi evt: 7 wifi evt: 7 ...... ...... (similar debug msgs) ...... wifi evt: 7 wifi evt: 7 Not connected after 250secs status:6 ip:169.254.221.137,mask:255.255.0.0,gw:0.0.0.0 wifi evt: 3

Connection established! IP address: 169.254.221.137 wifi evt: 7 wifi evt: 7 wifi evt: 7

odilonafonso avatar Jun 08 '20 13:06 odilonafonso

This is a debug messages for another router that I connect with the same code (changing ssid).

bcn 0 del if1 usl mode : sta(5c:cf:7f:36:89:dc) add if0 Connecting to PONESA-3G ... wifi evt: 8 wifi evt: 2 scandone state: 0 -> 2 (b0) state: 2 -> 3 (0) state: 3 -> 5 (10) add 0 aid 1 cnt

connected with PONESA-3G, channel 7 dhcp client start... wifi evt: 0 ip:192.168.2.103,mask:255.255.255.0,gw:192.168.2.1 wifi evt: 3

Connection established! IP address: 192.168.2.103 pm open,type:2 0

odilonafonso avatar Jun 08 '20 15:06 odilonafonso

I accessed the configuration of the CLARO2G router, to which the ESP was not being able to connect and manually placed it to use channel 7. Got connection! But I didn't understand why.

bcn 0 del if1 usl mode : sta(5c:cf:7f:36:89:dc) add if0 Connecting to CLARO2G ... wifi evt: 8 wifi evt: 2 scandone state: 0 -> 2 (b0) state: 2 -> 3 (0) state: 3 -> 5 (10) add 0 aid 2 cnt

connected with CLARO2G, channel 7 dhcp client start... wifi evt: 0 ip:192.168.0.113,mask:255.255.255.0,gw:192.168.0.1 wifi evt: 3

Connection established! IP address: 192.168.0.113 pm open,type:2 0

odilonafonso avatar Jun 08 '20 16:06 odilonafonso

The biggest issue I've had is the modem over-driving the router. I think the default WiFi.setOutputPower(x) is set to 17dBm, and I had to drop that to 5dBm to get reliable connection to my router.

That's one thing to check, In any case.

Tech-TX avatar Jun 08 '20 16:06 Tech-TX

I did not know this method WiFi.setOutputPower (x). Please, do you know where I find Wifi class documentation with the methods implemented for ESP8266?

odilonafonso avatar Jun 08 '20 16:06 odilonafonso

@odilonafonso readthedocs, but it's probably incomplete. The recommendation is always to look at a function/class interface to see what is available for use. That means look at the .h file.

devyte avatar Jun 08 '20 20:06 devyte

The times I have seen this it's due to a bad wifi config on board. You have to wipe the flash when downloading the sketch.

devyte avatar Jun 08 '20 20:06 devyte

Grateful for the @devyte help, but I didn't understand what you mean. I have already tried to rewrite the firmware with the three options offered by the IDE:

"Only sketch" "Sketch + Wifi settings" "All flash Contents".

I've already done another test; without modifying anything on the router, just adding any channel in the call to "Wifi.begin ()" and that sometimes works, other times it doesn't. On the other hand, one of my routers accepts connection requests in any way that ESP is configured.

I really don't know what to conclude, apparently it is a hardware failure and not a software failure. I just want to be sure about that.

I'm thinking of adding a loop to the call Wifi.begin () adding one channel after another, until I get a connection.

odilonafonso avatar Jun 09 '20 15:06 odilonafonso

I did not know this method WiFi.setOutputPower (x). Please, do you know where I find Wifi class documentation with the methods implemented for ESP8266?

In my code it's usually WiFi.setOutputPower (5); (5dBm) due to my router. At higher levels I get dropped packets. I think the allowed range for output power is 0 to 20.5, default 17.

Tech-TX avatar Jun 09 '20 16:06 Tech-TX

Just an idea... would it make sense to make this output power dynamic, based on the RSSI of the AP you want to connect to?

TD-er avatar Jun 09 '20 17:06 TD-er

@odilonafonso readthedocs, but it's probably incomplete. The recommendation is always to look at a function/class interface to see what is available for use. That means look at the .h file.

I always look at the .h source and often the .cpp

odilonafonso avatar Jun 09 '20 19:06 odilonafonso

I made some changes. I added a channel to Wifi.begin (). Apparently it is not used.

New sketch

#include <ESP8266WiFi.h>        // Include the Wi-Fi library

const char* ssid     = "CLARO2G";         // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "meiadeseda";     // The password of the Wi-Fi network

void setup() {
   Serial.begin(115200);         // Start the Serial communication to send messages to the computer
   delay(10);
   Serial.println("\n\n");
   WiFi.mode(WIFI_STA);

   int i = 0;
   int ws;
   long tini = millis();
   long tnow;
   #define CHINI 2
   #define CHEND 9
   #define INTERVAL 5000
   int chan = CHINI;
   int retrys;
   #define NTRYS 3
   while (true) {
      Serial.println("Connecting to " + String(ssid) + " Channel:" + String(chan));
      WiFi.begin(ssid, password, chan);             // Connect to the network
      retrys = 0;
      while ( (ws = WiFi.status()) != WL_CONNECTED && retrys < NTRYS) { // Wait for the Wi-Fi to connect
         tnow = millis();
         if ( tnow - tini > INTERVAL ) {
            ++i;
            Serial.println("Not connected after " + String(i*INTERVAL/1000) + "secs status:" + String(ws));
            tini = tnow;
            ++retrys;
         }
         yield();
      }
      if (ws == WL_CONNECTED) break;
      chan = (chan == CHEND ? CHINI : chan+1);
   }

   Serial.println('\n');
   Serial.println("Connection established!");  
   Serial.print("IP address:\t");
   Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer
}

void loop() {}

Debug messages

SDK:2.2.2-dev(38a443e)/Core:2.6.3=20603000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-16-ge23a07e/BearSSL:89454af



Connecting to CLARO2G Channel:2
scandone
wifi evt: 2
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2
cnt 

connected with CLARO2G, channel 6
dhcp client start...
wifi evt: 0
Not connected after 5secs status:6
Not connected after 10secs status:6
pm open,type:2 0
Not connected after 15secs status:6
Connecting to CLARO2G Channel:3
sl
scandone
state: 5 -> 2 (b0)
usl
state: 5 -> 2 (7c0)
wifi evt: 1
STA disconnect: 7
reconnect
state: 2 -> 0 (0)
sl
Not connected after 20secs status:6
scandone
state: 0 -> 2 (b0)
usl
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2
cnt 

connected with CLARO2G, channel 6
dhcp client start...
wifi evt: 0
Not connected after 25secs status:6
Not connected after 30secs status:6
Connecting to CLARO2G Channel:4

......

I don't know why I get this message: connected with CLARO2G, channel 6

Why ? Why does the message say ESP is connected? I dont understand

odilonafonso avatar Jun 10 '20 01:06 odilonafonso

It would make the source a lot more readable if you place 3 back-ticks before and after the source code block.

And the AP you connect to determines the WiFi channel, not the client that tries to connect to it. When calling WiFi.begin with a MAC address and channel, you force the connect routine to try those first. Typical use case for that is when you picked the best AP from a WiFi scan or lost connection and want to make a quick reconnect to the last known AP.

TD-er avatar Jun 10 '20 07:06 TD-er

Just an idea... would it make sense to make this output power dynamic, based on the RSSI of the AP you want to connect to?

If you can't connect to the AP, how can you tell if your TX power is too hot (or weak)? BER would be a better indicator unless the RSSI gives a "too hot" "too weak" response, as a number by itself is almost meaningless without a range.

Tech-TX avatar Jun 10 '20 16:06 Tech-TX

RSSI can output +31 as value indicating it is in some kind of error state. Or maybe it wants to tell you to "call the Dutch" :)

I agree about the limited meaning of the RSSI value. But on the other hand, it is an indication of the signal strength and not the reliability. So to me that would suggest it is somewhat related to the TX power. On the other hand, you don't get the value of the RSSI of your own signal. Only that of others.

TD-er avatar Jun 10 '20 16:06 TD-er

It would make the source a lot more readable if you place 3 back-ticks before and after the source code block.

And the AP you connect to determines the WiFi channel, not the client that tries to connect to it. When calling WiFi.begin with a MAC address and channel, you force the connect routine to try those first. Typical use case for that is when you picked the best AP from a WiFi scan or lost connection and want to make a quick reconnect to the last known AP.

Back-ticks ? what are back-ticks?

I do not call 'Wifi.begin()' with MAC address.

If it is not to provide the channel that I want to use, then what is the channel argument in the call for?

And I still don't understand what the debug message is:

connected with CLARO2G, channel 6

That must have probably been sent by the ESP8266WiFi library. How can the library send this message and at the same time return status 6?

odilonafonso avatar Jun 10 '20 21:06 odilonafonso

It would make the source a lot more readable if you place 3 back-ticks before and after the source code block.

Back-ticks ? what are back-ticks?

The same as you use to mark a few words in code markup.

As illustration, I will now use the wrong (the other) single quotes:

To mark a word in 'code markup'

''' To mark several lines in code markup '''

Now with the correct 'back ticks' (the single comma left from the 1 key on the US keyboard layout):

To mark a word in code markup

To mark 
several lines 
in code markup

TD-er avatar Jun 10 '20 21:06 TD-er

Now with the correct 'back ticks' (the single comma left from the 1 key on the US keyboard layout):

To mark a word in code markup

To mark 
several lines 
in code markup

Thanks @TD-er !!

odilonafonso avatar Jun 10 '20 22:06 odilonafonso

@odilonafonso in your case, Wifi.begin() means connect the ESP's wifi station + establish a dhcp IP address. Your last log says connected with CLARO2G, channel 6, which means the wifi connection was established, but then I see that the ESP's dhcp client times out waiting for your dhcp server to offer. Such things are support questions, please request peer help at a community forum like esp8266.com or stackoverflow. Closing.

devyte avatar Jun 15 '20 19:06 devyte

@devyte Why did you close? The problem has not been solved.

@odilonafonso in your case, Wifi.begin() means connect the ESP's wifi station + establish a dhcp IP address. Your last log says connected with CLARO2G, channel 6, which means the wifi connection was established, but then I see that the ESP's dhcp client times out waiting for your dhcp server to offer. Such things are support questions, please request peer help at a community forum like esp8266.com or stackoverflow. Closing.

The message connected with CLARO2G, channel 6 is not mine. Is not from my sketch. I receive "not connected".

I am just questioning why the message "connected to CLARO2G" arrives from "somewhere" and meanwhile I get "disconnected" status. How to solve this?

odilonafonso avatar Jun 16 '20 12:06 odilonafonso

@devyte this

And I still don't understand what the debug message is:

connected with CLARO2G, channel 6

That must have probably been sent by the ESP8266WiFi library. How can the library send this message and at the same time return status 6?

This seems to me to be a software problem. There is no point in the library sending a "CONNECTED" log message and at the same time sending "DISCONNECTED" status.

odilonafonso avatar Jun 16 '20 12:06 odilonafonso