ESP8266 NodeMCU WiFi issues

Hello all,

I'm running into an issue currently when I try to connect using the WiFi capabilities of my NodeMCU.

When I try to scan for networks, I find that no networks were found, despite the presence of several networks. Also, when I attempt a direct connection to my network by using an SSID and password, it is unable to connect.

Here is the sketch I am using to scan (it's the example sketch for WifiScan):

#include "ESP8266WiFi.h"
void setup() {
  Serial.begin(74880);

  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  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) == ENC_TYPE_NONE)?" ":"*");
      delay(10);
    }
  }
  Serial.println("");

  // Wait a bit before scanning again
  delay(5000);
}

This sketch outputs:

scan start
scan done
no networks found

over and over.

Here is the sketch I am using to attempt to directly connect:

#include "ESP8266WiFi.h"

// WiFi parameters to be configured
const char* ssid = "SelenaNetwork";
const char* password = "grace888";

void setup(void)
{ 
  Serial.begin(74880);
  // Connect to WiFi
  WiFi.begin(ssid, password);

  // while wifi not connected yet, print '.'
  // then after it connected, get out of the loop
  while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
  }
  //print a new line, then print WiFi connected and the IP address
  Serial.println("");
  Serial.println("WiFi connected");
  // Print the IP address
  Serial.println(WiFi.localIP());

}
void loop() {
  // Nothing
}

This sketch simply outputs dots to serial output until I stop the board from running. I have verified that my router is broadcasting the SSID at 2.4 GHz and is requiring the correct password. Other devices can connect to it without issue.

Please note that I have 2 NodeMCU boards and both exhibit the same exact behavior.

Any help would be appreciated.

Thanks!

Anything plugged in the pins?

How many amps is your USB power supply rated at? From a PC it’s usually 500mA but If it's very low (<250ma) it could be an issue

Are you running a 5GHz wireless network? The ESP8266 only has a 2.4GHz radio.

Check which wifi channel is used - possibly that isn't supported in some regions?

Possibly reflash your units

Hi,

As far as the pins go, nothing is plugged in.

My power supply is currently just having it plugged into my PC while I upload sketches.

I ensured that my router was broadcasting at 2.4GHz for my network.

I believe my WiFi channel setting is set to 'Auto'.

I have reflashed both my boards multiple times, however, this behavior happened 'right out of the box', i.e. before and after firmware installations/upgrades.

Ok all seems in order... that’s weird

I agree. I figured it must have been some kind of hardware issue, but once I got the second one which behaved exactly the same, I figured it must have been something other than a hardware failure.

My best guess at this point is firmware, due to the fact that I can run pretty much any other sketch and it works fine. The thing is, firmware is not even needed to use every other sketch I've written so far. I tested by plugging it straight out of the box, and the sketches worked great.

If you put the ESP in access point mode - do you see it coming live ?

I just tried the following sketch to put the ESP into AP Mode:

#include <ESP8266WiFi.h>

const char *ssid = "Network1";
const char *password = "testPassword"; //the password should be 8 char or more

//setting the addresses
IPAddress ip(192,168,8,1);
IPAddress gateway(192, 168, 8, 1);
IPAddress subnet(255, 255, 255, 0);

void setup() {
  WiFi.mode(WIFI_AP); //switching to AP mode
  WiFi.softAP(ssid, password); //initializing the AP with ssid and password. This will create a WPA2-PSK secured AP

  WiFi.config(ip,gateway,subnet);
  WiFi.begin();
}

void loop() {

}

I was not able to see it under the available network for my phone, nor was I able to connect to it directly by typing in the SSID and password.

if you have Windows, try flashing AT firmware with Espressif Flash Download Tool. part of the procedure is a bin with settings including RF settings

Hi,

I got that tool you suggested and tried flashing the .bin file I got from NodeMCU's latest firmware release, but this did not have any effect on the behavior I was experiencing. I agree that the firmware is the most likely cause.

I got the impression from your post that I was supposed to do something differently involving the settings I was using to flash the firmware. Were there some specific settings you had in mind that I could try? I set them to the settings I was familiar with setting as far as my specific board goes, like flash size, cpu, etc.

Thanks for your help.

firmwares build with esp8266 SDK use a file esp_init_data_default.bin. this file contains settings for the esp8266 including WiFi RF settings.

Hi,

I created a custom esp_init_data_default.bin file that correctly set the frequency of my cpu to 40 MHz (the default is 26), and uploaded it to the correct address in memory as per the documentation for my board along with the normal AT firmware.

I was able to use Esplorer to communicate with the device, and didn't see any garbled characters anymore @ 115200 baud. I couldn't get a good test of the WiFi because I was getting strange errors when trying to use the lua code to connect to my network.

Regardless, it seems to me that the Arduino IDE completely flashes the firmware anyway when it compiles and uploads, which would overwrite any custom setting changes that might have helped.

After doing more research, I came across this thread that seems to be related:

http://www.esp8266.com/viewtopic.php?f=13&t=12175

The thread mentions how garbled characters and WiFi issues can be the result of an incorrect clock frequency setting in the firmware.

Do you think this is my issue? If so, how do I configure Arduino's IDE to correctly flash my firmware?

If you think it is another issue, perhaps as you mentioned some setting in esp_init_data_default.bin, what settings should I have changed in the AT firmware's esp_init_data_default.bin file? Also, even if it is a esp_init_data_default.bin setting that is causing this issue, how do I correct the problem with Arduino's IDE where it does not flash the correct firmware onto my device?

Thanks for your help.

in Arduino IDE use the 2.4 core. it has the crystal frequency setting. if not look into boards.txt

When you say 2.4 core I assume you mean selecting a different board than the NodeMCU 1.0 board from the board option in the IDE. The only one with core in it is the 'Core Development Module' option, which I chose, but unfortunately it does not upload sketches and throws 'error: espcomm_upload_mem failed' message. Also, I did not see any crystal frequency setting that became available in the tools dropdown.

Looking into boards.txt, it seems to me that there's some settings that might be related.

Since I'm using a custom downloaded board, I went into the boards.txt located in ...\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2.

I found the section in the .txt file for the NodeMCUv2, and located the following settings:

nodemcuv2.build.f_cpu=80000000L
nodemcuv2.menu.CpuFrequency.80.build.f_cpu=80000000L
nodemcuv2.menu.CpuFrequency.160.build.f_cpu=160000000L

It seems to me that these settings might conflict in some way.

Since the tool menu lets me choose between 80 and 160 MHz CPU Frequency, I have to choose between one of them. The seller of the board instructed to use 80 MHz from the drop-down, so I will assume that the line 'nodemcuv2.menu.CpuFrequency.80.build.f_cpu=80000000L' is what is used. I am confused as to whether or not the line 'nodemcuv2.build.f_cpu=80000000L' or the line 'nodemcuv2.menu.CpuFrequency.80.build.f_cpu=80000000L' takes precedence.

I decided to change both of them to 40 MHz:

nodemcuv2.build.f_cpu=40000000L
nodemcuv2.menu.CpuFrequency.80.build.f_cpu=40000000L

I then saved the .txt file, restarted Arduino IDE, then loaded the .txt file again to make sure the changes were saved. It appeared that the .txt file retained the changes.

I decided to write a simple sketch to test the clock speed by sending a string to serial output every two seconds:

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("test");
  delay(2000);
}

The output produced garbled characters.

I tried a blink example sketch:

/*
 ESP8266 Blink by Simon Peter
 Blink the blue LED on the ESP-01 module
 This example code is in the public domain
 
 The blue LED on the ESP-01 module is connected to GPIO1 
 (which is also the TXD pin; so we cannot use Serial.print() at the same time)
 
 Note that this sketch uses LED_BUILTIN to find the pin with the internal LED
*/

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                    // but actually the LED is on; this is because 
                                    // it is active low on the ESP-01)
  delay(1000);                      // Wait for a second
  digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
}

Despite the delay before the loop executed being set at 2000 ms, the blink occurred significantly faster, indicating that the clock wasn't correctly synced. WiFi scan does not work either, nor does direct connection to my network.

Just to be 100% sure that the value I set in boards.txt worked correctly, I changed the test sketch above to output the value of F_CPU, and it printed that it was 40000000L.

It seems as if the crystal frequency is still not synced despite changing the build parameters that would affect it.

I apologize for the double post, but it seems that I have found a resolution of sorts.

The following settings, as described in my last post, did not have an effect:

nodemcuv2.build.f_cpu=40000000L
nodemcuv2.menu.CpuFrequency.80.build.f_cpu=40000000L

I started looking around in boards.txt for an actual crystal frequency setting, and I found the following in another esp8266 module:

arduino-esp8266.menu.BoardModel.starottodeved.build.extra_flags=-DF_CRYSTAL=40000000

It appears that this line adds an extra flag to the firmware compiler that sets the crystal frequency.

I added a new line in boards.txt under the NodeMCU header where the settings for my board were, and changed the syntax to correctly match what I thought must be right:

nodemcuv2.build.extra_flags=-DF_CRYSTAL=40000000

I ran a test sketch that output text to the serial output, set the baud rate for Serial.begin to 115200, and it worked. I did not have to set it to 74880 any longer.

I started testing WiFi sketches and had better success. I still could not connect to my WiFi network, but when I scanned, every once in a while I'd see another nearby network come up in the list.

I was also able to setup an access point- I could see it as an available connection on my phone. I can only conclude that WiFi access has some functionality now.

I ran a blink sketch, and the timing seems to be much closer to what it should be per the sketch (2000 ms).

It seems that my crystal frequency issue has been resolved.

The only thing that remains is determining why I can't connect my board to my WiFi network.

I am using the following test sketch:

#include <ESP8266WiFi.h>
 
const char* ssid = "TestNetwork";
const char* password = "testPassword";
  
void setup() {
  Serial.begin(115200);
  delay(10);
 
  // Connect to WiFi network
  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");
 }
 
void loop() {
 
}

It just endlessly prints out periods.

I ensured that my network is broadcasting @ 2.4 GHz. It is definitely not an issue of distance or interference, as the router is only a foot or so away from the board.

What else could be causing this issue?

Thanks for your help.

esp8266 Arduino core 2.4.release. now you have 2.4 rc2

I suppose you're asking me to switch back to 2.4 board release since you noticed that I have 2.4 rc2?

I did that, and readded the crystal build flag to the boards.txt, and the issue still occurs using the same sketch as I included in my last post.

did you try the WiFiScan example?

Setting up an AP seems to work, as I can see the AP as an available network on my phone.

WiFiScan detected one network in my area twice out of about 40 tries, though it was not my network.

your posts about settings were hard to follow.
Common is 80 MHz CPU, 40 MHz flash and 26 or 40 MHz crystal.

I have 80 MHz CPU currently set, 40 MHz crystal as build flag, and flash frequency at 40 as well.