Unreliable WiFi connections with ESP8266 and Uno

Hi,
I’m attempting to get an ESP8266-01 working as a WiFi shield with an Arduino Uno and Blynk but having very little success, could use the guidance from folks here. Setup is similar to here. I’m using the latest version of the AT firmware 1.6.2.0 on the ESP8266-01.

Good news is the same setup already works reliably to upload data to Thingspeak via the WiFiEsp.h library. However I can’t seem to get it to work reliably at all for Blynk. Frankly I’d rather use the WiFiEsp library as it seems more reliable but Blynk insists in managing it’s own connection from most examples I’ve seen. This is based on the standard Blynk example for this setup here and should work, but it doesn’t. :expressionless:

Core of the problem seems to be my AP takes a few tries to connect (sometimes 30-45s) and Blynk seems to time out quite quickly and not retry and fails in the getLocalIP routine in ESP8266.cpp. I’ve tried to pre-connect with the WiFiEsp routines which succeeds as seen below, but it fails thereafter anyhow.

I’ve tried using Blynk.config() routine instead but it does not seem to compile with BlynkSimpleShieldEsp8266.h and ESP8266_Lib.h as seen by the comment below. Is an additional library needed here?

Here is the typical serial debug output:

Connecting to:things
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.2.1
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Connected to things
Connected!
[132557] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

[132653] Connecting to things
[135901] AT version:1.6.2.0(Apr 13 2018 11:10:59)
SDK version:2.2.1(6ab97e9)
compile time:Jun  7 2018 19:34:26
Bin version(Wroom 02):1.6.2
OK
[146608] Failed to connect WiFi

Here is the short test code I’ve been using to debug the issue resulting in the above output. SSID, password and auth codes are in Settings.h (not shown).

  /* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include "Settings.h"
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
//#include <BlynkSimpleStream.h>  // For Serial communications with Blynk
#include <WiFiEsp.h>          // Include support for ESP8266-01 WiFi module
#include <avr/pgmspace.h>

int status = WL_IDLE_STATUS;     // the Wifi radio's status
WiFiEspClient client;       // Initialize the Wifi client library

// Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial Serial1(5, 4); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

ESP8266 wifi(&EspSerial);  //Blynk use

//BlynkTimer timer; // Uses lots of SRAM on Uno!!!

void setup()
{

  char ssidbuff[strlen_P(ssid) + 1];        // buffer for ssid
  char pwdbuff[strlen_P(pwd) + 1];          // buffer for pwd
  char authbuff[strlen_P(auth) + 1];
  strcpy_P(ssidbuff, ssid);
  strcpy_P(pwdbuff, pwd);
  strcpy_P(authbuff, auth);
  
  // Debug console
  Serial.begin(9600);
 
  delay(100);
  Serial.print(F("Connecting to:"));
  Serial.println(ssidbuff);
  

  Serial1.begin(9600); // Init ESP Serial link
  
  WiFi.init(&Serial1);
  
  delay(100);
  while (status != WL_CONNECTED) {          // attempt to connect to WiFi network
    status = WiFi.begin(ssidbuff,pwdbuff);          // connect to WPA/WPA2 network
    delay(3000);  
  }
  Serial.println(F("Connected!"));

  
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(100);

  //Blynk.begin(Serial1, auth);  // Does not work
  Blynk.begin(authbuff, wifi, ssidbuff, pwdbuff);
  //Blynk.config(authbuff, "blynk-cloud.com", 8442); // Does not compile!
  Blynk.connect();
  delay(100);
  Serial.println(F("Blynk connected!"));
}

void loop()
{
  Blynk.run();
  Blynk.virtualWrite(V0, millis() / 1000);
  Serial.print(F("."));
  delay(5000);
}

I should have mentioned that I’ve tried numerous times without the WiFiEsp.h calls, but it never connect there either. In any case I need data uploaded to Thingspeak, so they will need to stay, with Blynk as an addition.

Any help much appreciated. Thanks

I can’t see how this is going to work when you’re trying to use WiFiEsp.h and Blynk.begin at the same time. I know that you said you’ve tried it without WiFiEsp.h but I think you should focus on just using Blynk.begin.

I’m a bit beamused by all this stuff:

and I’d probably start by hard-coding your credentials directly into the Blynk.begin statement to ensure that your Auth code isn’t being mangled by this process.

Once you do get connected then the delay(5000) in your void loop will probably lead to a disconnection quite quickly, so you should be calling the Blynk.virtualWrite using a timer.

Turning-on Blynk debugging may also help.

Also, I think I’d probably avoid using Serial1 as the name of my SoftwareSerial UART, as it’s probably a reserved word and may cause issues.

Pete.

Hi Pete,

Thanks much for the response. I guess what I’d prefer is for Blynk to use the WiFi connection that’s already established by WiFiEsp.h rather then try re-establish it again, is there a recommended way to achieve this?

If I uncomment the Blynk.begin() and try Blynk.config() as such, I get the following compilation error;

//Blynk.begin(auth, wifi, ssidbuff, pwdbuff);
  Blynk.config(auth);

IDE Output:

In file included from /Volumes/Users/tsarath/Documents/My Gear/Arduino/Sketch_Files/Atmel1284p/Blynk_Test_v6_apr12b3/Blynk_Test_v6_apr12b3.ino:39:0:
/Volumes/Users/tsarath/Documents/My Gear/Arduino/Sketch_Files/libraries/Blynk/src/BlynkSimpleShieldEsp8266.h:169:10: note: candidate: void BlynkWifi::config(ESP8266&, const char*, const char*, uint16_t)
     void config(ESP8266&    esp8266,
          ^~~~~~
/Volumes/Users/tsarath/Documents/My Gear/Arduino/Sketch_Files/libraries/Blynk/src/BlynkSimpleShieldEsp8266.h:169:10: note:   candidate expects 4 arguments, 1 provided
exit status 1
no matching function for call to 'BlynkWifi::config(char [33])'

It seems in BlynkSimpleShieldEsp8266.h:169 expects the wifi to be available and hence fails. I’m guessing I’m doing something obviously wrong here as something else needs to pass the wifi info prior to Blynk.config() being called, I assume that’s Blynk.begin().

What I need Blynk to do is simply run at the application layer and not try manage the underlying WiFi connection. If it needs the source IP address etc, I can easily pass it that information from WiFiEsp.h. Is there a recommended way to achieve this?

The final code will run also on a Atmel 1284P, hence why I was using Serial1 which is the second hardware serial on that chip, but I’ll try switching this out to see if it make any difference.

The problem I think is related to the time taken to establish the WiFi, as 1 in every 20 goes or so Blynk does sometimes in fact connect to the WiFi and is able to ping. WifiEsp.h seems to handle this scenario correctly.

I’ll also try turn on Blynk debugging as you’ve suggested.

Thanks

I know that you said you’ve tried it without WiFiEsp.h but I think you should focus on just using Blynk.begin.

Hi Pete,

As a reference going back to basics, I tried the standard code example below just to make sure I was sane. :grinning:

Code and resulting output both below. Turning on debug didn’t seem to have any additional output unfortunately.

___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
    /___/ v0.6.1 on Arduino Uno

[598] Connecting to things
[3815] AT version:1.6.2.0(Apr 13 2018 11:10:59)
SDK version:2.2.1(6ab97e9)
compile time:Jun  7 2018 19:34:26
Bin version(Wroom 02):1.6.2
OK
[14022] Failed to connect WiFi

Standard example with minor modifications:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define BLYNK_DEBUG         // Optional, this enables lots of prints

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MyAPIToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "things";
char pass[] = "MyPassword";

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(5, 4); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V0, millis() / 1000);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

Hence I’re really appreciate if someone can give a pointer on how to use a network connection that’s already established outside of Blynk as per my prior note. Blynk just needs to act like any other standard HTTP application.

Thanks

Quite easy if you’re using something like a NodeMCU or ESP32, but I don’t think you can use Blynk.config when you’re using an ESP-01 as an AT WiFi modem, which is what you’d need to do.

The compiler also doesn’t like the use of Blynk.config with the BlynkSimpleShieldEsp8266.h library.

If you take a look into the library then you’l see that it enables a very limited number of methods to be used to communicate with the included ESP8266_Lib.h library.

No, Blynk.config is never used in conjunction with Blynk.begin.
When using Blynk.config it’s your responsibility to set-up the WiFi connection (or use Blynk.connectWiFi) and Blynk.config defines the configuration and Blynk.connect or the first occurrence of Blynk.run initialises the connection.

However, as I said, I don’t think you can use Blynk.config with the BlynkSimpleShieldEsp8266.h library.

Then I think you either need to change your hardware, or maybe experiment using some of the other Blynk libraries - maybe the BlynkSimpleEsp8266.h library in conjunction with Blynk.config (but don’t blame me if this suggestion leads down a dead-end)

Pete.

Then I think you either need to change your hardware, or maybe experiment using some of the other Blynk libraries - maybe the BlynkSimpleEsp8266.h library in conjunction with Blynk.config (but don’t blame me if this suggestion leads down a dead-end)

Thanks again Pete.

Just to make sure I was not doing something silly, I tested the standard Blynk example as shown here with an Arduino W5200 Ethernet shield I had on the same Uno, and everything of course works there as expected. :smiley:

I tried using BlynkSimpleEsp8266.h with the ESP8266-01 shield but compiler spits out a fatal error as that code is meant to run natively on the ESP8266. Also tried with Ethernet.h and BlynkSimpleEthernet.h which compiles, tries to connect but does not actually do so, output as below;

Connecting to:things
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.2.1
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Connected to things
Connected!
IP Address: 192.168.3.83
MAC address: 2C:3A:E8:0A:67:3C

[50417] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

Blynk connected!
[50646] Connecting to blynk-cloud.com:80
..........[55648] Connecting to blynk-cloud.com:80
..........[60650] Connecting to blynk-cloud.com:80
..........[65652] Connecting to blynk-cloud.com:80
..........[70654] Connecting to blynk-cloud.com:80
..........[75657] Connecting to blynk-cloud.com:80
..........[80660] Connecting to blynk-cloud.com:80

I guess at this point I seem to have 3 possible options:
[1] Persevere and see if I can get Blynk.config() etc working with another adapter library and WiFiEsp.h
[2] Try fix BlynkSimpleShieldEsp8266.h connections issues to my AP
[3] Try a different AP perhaps just for Blynk and this device. :expressionless:

Let me try [1] first and see how far I get perhaps…

Thanks

Hey Pete,

I got it all working, woohoo! :crazy_face: Thanks for all your help, definitely pointed me in the right direction!

I used the example code provided here with the Blynk library itself. But importantly this is using the ESP8266-01 as a Wifi shield, with all the code running on the Uno which is my preferred setup. The Arduino Ethernet example here was also helpful.

Here’s my full working code using the WiFiEsp.h library for reference;

// Works with ESP8266-01 Shield on Arduino Uno [04/15/20]!!

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  This example shows how to use ESP8266 Shield (with AT commands)
  to connect your project to Blynk using the WifiEsp.h library

  WARNING!
    It's very tricky to get it working. Please read this article:
    http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG         // Optional, this enables lots of prints

#include "Settings.h"
#include <ESP8266_Lib.h>
#include <BlynkSimpleStream.h>  // For Serial communications with Blynk
#include <WiFiEsp.h>            // Include support for ESP8266-01 WiFi module
#include <avr/pgmspace.h>

int status = WL_IDLE_STATUS;     // the Wifi radio's status
WiFiEspClient client;            // Initialize the Wifi client library

// Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial Serial1(5, 4); // RX, TX


void setup() {

  char ssidbuff[strlen_P(ssid) + 1];        // buffer for ssid stored in progmem
  char authbuff[strlen_P(auth) + 1];        // buffer for auth code stored in progmem
  strcpy_P(ssidbuff, ssid);
  strcpy_P(authbuff, auth);
  
  // Debug console
  Serial.begin(9600);
 
  delay(100);
  Serial.print(F("Connecting to:"));
  Serial.println(ssidbuff);
  Serial1.begin(9600);  // Init ESP Serial link
  WiFi.init(&Serial1);  // Init Wifi link

  connectWifi();        // Connect to Wifi using WiFiEsp.h
  printWifiData();      // Print Wifi IP/MAC info
  printCurrentNet();    // Print Wifi signal info
  delay(10);
  connectBlynk();
  Blynk.begin(client, authbuff);
  delay(10);
  Serial.println(F("Blynk connected!"));

}

void loop() {
  // Reconnect WiFi
  if (status != WL_CONNECTED) {
    connectWifi();
    return;
    }

  // Reconnect to Blynk Cloud
  if (!client.connected()) {
    connectBlynk();
    return;
    }
  
  Blynk.run();
  Blynk.virtualWrite(V0, millis() / 1000);
  Serial.println(F("."));
  delay(1000);
}

bool connectBlynk()                         // This function tries to connect to the cloud using TCP
{
  client.stop();
  return client.connect(BLYNK_DEFAULT_DOMAIN, BLYNK_DEFAULT_PORT);
}

void connectWifi(){                         // This function connects with Wifi via WiFiEsp.h library
  
  char ssidbuff[strlen_P(ssid) + 1];        // buffer for ssid
  char pwdbuff[strlen_P(pwd) + 1];          // buffer for pwd
  strcpy_P(ssidbuff, ssid);
  strcpy_P(pwdbuff, pwd);
  delay(100);
  while (status != WL_CONNECTED) {          // attempt to connect to WiFi network
    status = WiFi.begin(ssidbuff,pwdbuff);  // connect to WPA/WPA2 network
    delay(2000);  
  }
  Serial.println(F("WiFi Connected!"));
}

void printWifiData() {
  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print(F("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(F("MAC address: "));
  Serial.println(buf);
}

void printCurrentNet()
{
  // print the SSID of the network you're attached to
  Serial.print(F("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(F("BSSID: "));
  Serial.println(buf);

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

Settings.h is simply as below, this is done to save SRAM on the Uno.

// User defined settings

// Blynk auth token
const static char auth[] PROGMEM = "MyAuthToken"; 
// Your WiFi credentials.
// Set password to "" for open networks.
const static char ssid[] PROGMEM =  "MySSID";
const static char pwd[] PROGMEM = "MyPassword";

Working code is a wonderful thing :grinning:
Now I can move all of this to my 1284P setup and have it work there as well…

1 Like

Well done!
I didn’t think that was going to be possible.

Pete.

Thanks, yes, it was quicker than I thought once I was heading in the right direction and didn’t need Blynk.config() after all, Blynk.begin(client, authbuff) did the trick. On the Blynk app I have Arduino Uno with WiFi selected with mills count getting updated every second.

Below is the console output for reference

Connecting to:things
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.2.1
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Failed connecting to things
[WiFiEsp] Connected to things
WiFi Connected!
IP Address: 192.168.3.79
MAC address: 2C:3A:E8:0A:67:3C
SSID: things
BSSID: 00:1E:13:0D:D3:40
Signal strength (RSSI): -38
[WiFiEsp] Connecting to blynk-cloud.com
[166165] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

[166259] Connecting...
[166598] Ready (ping: 37ms).
Blynk connected!
.
.
.

Thanks again :smiley:

Hmmmm, that surprises me.
Be aware that Blynk.begin is a blocking function - if the device can’t connect to Blynk for whatever reason then the code execution will stop at that point. This means that there is no chance of using your device in ‘offline mode’.

Pete.

Hmmmm, that surprises me.
Be aware that Blynk.begin is a blocking function - if the device can’t connect to Blynk for whatever reason then the code execution will stop at that point. This means that there is no chance of using your device in ‘offline mode’.

I think if you look at the Arduino Ethernet example, Blynk.begin(ethernetClient, auth) is passed a reference to the client handler function, so Blynk.begin() is no longer blocked if I understand correctly. In my case WiFiEsp.h handles the translation to all the silly AT commands of the ESP8266-01 and presents a higher level function that’s comparable to the standard Arduino Ethernet or Wifi client libraries. I think that’s why it works.

Understand it’s blocking, rest of my code already connects to Thingspeak to upload weather data, so this will just piggy back on that. If Wifi is not available, yes, it will not start.

The best part about this is that is also more memory efficient. The standard Blynk example uses 55% of the available 2K memory on an Uno. The code above uses 20% less to do effectively the same thing. :smiley:

1 Like