Unreliable WiFi connections with ESP8266 and Uno

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