ESP8266-01 failed connecting to AP using Arduino Sketch

Hi All,

I'm using ESP8266-01 through the Arduino Mega 2560 and the library that i used is WiFiEsp.h

I had successfully connect the ESP8266-01 to an AP through AT command, and when connected through Arduino Mega, it can also list network available by running ScanNetworks example in WiFiEsp library, so i believe the ESP8266 should be functioning.

However, when I want to connect to an AP using Arduino sketch(ConnectWPA, example in WiFiEsp library), it simply won't connect and on the serial monitor always show "failed connecting to WPA SSID ...".

Hardware connection:

Arduino Mega 5V ----- Voltage divider Vin
Voltage Divider Vout(3.3V) ----- Vcc & CH_EN
Arduino TX1 ----- ESP8266 Rx
Arduino RX1 ----- ESP8266 Tx

RST, GPIO-0, GPIO-2 ----- unconnected

Tools>Board"Arduino Mega 2560" if that make any difference

Firmware of ESP8266-01 is v0.9.2.2 AT Firmware

Below are the codes & serial monitor output

ConnectWPA

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid[] = "onemillion";            // your network SSID (name)
char pass[] = "12345678";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

void setup()
{
 // initialize serial for debugging
 Serial.begin(115200);
 // initialize serial for ESP module
 Serial1.begin(9600);
 // initialize ESP module
 WiFi.init(&Serial1);

 // check for the presence of the shield
 if (WiFi.status() == WL_NO_SHIELD) {
   Serial.println("WiFi shield not present");
   // don't continue
   while (true);
 }

 // attempt to connect to WiFi network
 while ( status != WL_CONNECTED) {
   Serial.print("Attempting to connect to WPA SSID: ");
   Serial.println(ssid);
   // Connect to WPA/WPA2 network
   WiFi.begin(ssid, pass);
 }

 Serial.println("You're connected to the network");
}

void loop()
{
 // print the network connection information every 10 seconds
 Serial.println();
 printCurrentNet();
 printWifiData();
 
 delay(10000);
}

void printWifiData()
{
 // print your WiFi shield's IP address
 IPAddress ip = WiFi.localIP();
 Serial.print("IP Address: ");
 Serial.println(ip);

 // print your MAC address
 byte mac[6];
 WiFi.macAddress(mac);
 char buf[20];
 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
 Serial.print("MAC address: ");
 Serial.println(buf);
}

void printCurrentNet()
{
 // print the SSID of the network you're attached to
 Serial.print("SSID: ");
 Serial.println(WiFi.SSID());

 // print the MAC address of the router you're attached to
 byte bssid[6];
 WiFi.BSSID(bssid);
 char buf[20];
 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
 Serial.print("BSSID: ");
 Serial.println(buf);

 // print the received signal strength
 long rssi = WiFi.RSSI();
 Serial.print("Signal strength (RSSI): ");
 Serial.println(rssi);
}

ScanNetworks

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

void setup() {
 // initialize serial for debugging
 Serial.begin(115200);
 // initialize serial for ESP module
 Serial1.begin(9600);
 // initialize ESP module
 WiFi.init(&Serial1);

 // check for the presence of the shield
 if (WiFi.status() == WL_NO_SHIELD) {
   Serial.println("WiFi shield not present");
   // don't continue
   while (true);
 }

 // Print WiFi MAC address
 printMacAddress();
}

void loop()
{
 // scan for existing networks
 Serial.println();
 Serial.println("Scanning available networks...");
 listNetworks();
 delay(10000);
}


void printMacAddress()
{
 // get your MAC address
 byte mac[6];
 WiFi.macAddress(mac);
 
 // print MAC address
 char buf[20];
 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
 Serial.print("MAC address: ");
 Serial.println(buf);
}

void listNetworks()
{
 // scan for nearby networks
 int numSsid = WiFi.scanNetworks();
 if (numSsid == -1) {
   Serial.println("Couldn't get a wifi connection");
   while (true);
 }

 // print the list of networks seen
 Serial.print("Number of available networks:");
 Serial.println(numSsid);

 // print the network number and name for each network found
 for (int thisNet = 0; thisNet < numSsid; thisNet++) {
   Serial.print(thisNet);
   Serial.print(") ");
   Serial.print(WiFi.SSID(thisNet));
   Serial.print("\tSignal: ");
   Serial.print(WiFi.RSSI(thisNet));
   Serial.print(" dBm");
   Serial.print("\tEncryption: ");
   printEncryptionType(WiFi.encryptionType(thisNet));
 }
}

void printEncryptionType(int thisType) {
 // read the encryption type and print out the name
 switch (thisType) {
   case ENC_TYPE_WEP:
     Serial.print("WEP");
     break;
   case ENC_TYPE_WPA_PSK:
     Serial.print("WPA_PSK");
     break;
   case ENC_TYPE_WPA2_PSK:
     Serial.print("WPA2_PSK");
     break;
   case ENC_TYPE_WPA_WPA2_PSK:
     Serial.print("WPA_WPA2_PSK");
     break;
   case ENC_TYPE_NONE:
     Serial.print("None");
     break;
 }
 Serial.println();
}

Serial Monitor of ConnectWPA

[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Warning: Unsupported firmware
Attempting to connect to WPA SSID: SuiBianLa
[WiFiEsp] Failed connecting to SuiBianLa
Attempting to connect to WPA SSID: SuiBianLa
[WiFiEsp] Failed connecting to SuiBianLa

you can't power a device over a voltage divider.
voltage divider can be used to convert ttl logic level, but at higher current draw something could be damaged

Whether a voltage divider is used or not, the Arduino 's 5V pin does not provide enough power to run a ESP-01 module. The ESP-01's current draw will spike when transmitting.

.

ieee488:
Whether a voltage divider is used or not, the Arduino 's 5V pin does not provide enough power to run a ESP-01 module. The ESP-01's current draw will spike when transmitting.

.

so I run the esp8266 on the 5 V pin of Arduino. for example a download of a 200 kbyte picture from SD card over esp8266 AT firmware to browser

Per the OP's comments, the esp8266 seems to be working as wired as it apparently connects to an AP. So, voltage differences might not be the issue. Below is how to post code.

To put your code in a code box, use the </> icon in the far left of the post tool bar and paste your code between the two bracket sets that appear.

To go back and put your code in a code box, in the bottom right of your post, select "more" and click modify. When the modify post opens, high light your code and click the </> in the far left of the post tool bar. This will put you code in code brackets. Then save the changes.

zoomkat:
Per the OP's comments, the esp8266 seems to be working as wired as it apparently connects to an AP. So, voltage differences might not be the issue. Below is how to post code.

connecting to AP takes much more current then listing APs

"connecting to AP takes much more current then listing APs"

If it doesn't connect connect to the APs, where does the list of APs come from? The voltage divider is probably bad for a power source. I'd just try two or three diodes in series instead.

zoomkat:
"connecting to AP takes much more current then listing APs"

If it doesn't connect connect to the APs, where does the list of APs come from? The voltage divider is probably bad for a power source. I'd just try two or three diodes in series instead.

the list of APs available in 'air' around is not provided. it is build by 'listening'

Hi All,

I'm happy to say the problem has been solved, it turn out that i shouldn't ignore the [Timeout] and [Unsupported Firmware] warning.

I had flashed my ESP8266-01 with Ai-Thinker-v1.3-115200 firmware using ESP8266 Flasher and as the firmware name suggests, the baud rate of the module became 115200 so I had to set Serial1.begin(115200) in my skecth.

The ESP8266 can now connect to AP using Arduino sketch without any warning/error.

Once again thanks all for your help.

1 Like

kaijie_98:
Hi All,

I'm happy to say the problem has been solved, it turn out that i shouldn't ignore the [Timeout] and [Unsupported Firmware] warning.

I had flashed my ESP8266-01 with Ai-Thinker-v1.3-115200 firmware using ESP8266 Flasher and as the firmware name suggests, the baud rate of the module became 115200 so I had to set Serial1.begin(115200) in my skecth.

The ESP8266 can now connect to AP using Arduino sketch without any warning/error.

Once again thanks all for your help.

you could flash AT 1.7 and use my WiFiEspAT library (if you don't use secure connection)