Arduino Nano and ESP-01 using SoftwareSerial repeatedly runs setup() never gets to loop()

I’m using an Arduino Nano with an ESP-01. I created a QuickStart script using Blynk’s online generator. Here’s the sequence of relevant events that I’ve been able to verify:

  1. Code loads to Nano
  2. setup() is executed
  3. Blynk.begin() is executed
  4. ESP-01 connects to WiFi
  5. Blynk console (browser) shows Online
  6. 11 seconds pass
  7. Blynk console shows Offline
  8. setup() is executed again
  9. repeat steps 3-7

I’ve put in a few println("argh *[X]* ") to help track down where the problem is occurring.

  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

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           " *[the template ID]* "
#define BLYNK_DEVICE_NAME           "Quickstart Device"
#define BLYNK_AUTH_TOKEN            " *[the auth token]* "


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


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

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = " *[the wifi router]* ";
char pass[] = " *[the wifi password]* ";

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

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

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  Serial.println("argh");

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Serial.println("argh2");

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

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

void loop()
{
  Serial.println("ugh");
  Blynk.run();
  Serial.println("blynk");
  timer.run();
  Serial.println("timer");
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

Here are the messages that are output to Serial Monitor.

08:37:30.607 -> argh
08:37:30.607 -> argh2
08:37:30.607 -> [10] 
08:37:30.607 ->     ___  __          __
08:37:30.607 ->    / _ )/ /_ _____  / /__
08:37:30.607 ->   / _  / / // / _ \/  '_/
08:37:30.607 ->  /____/_/\_, /_//_/_/\_\
08:37:30.607 ->         /___/ v1.0.1 on Arduino Nano
08:37:30.607 -> 
08:37:31.106 -> [518] Connecting to  *[the wifi router]* 
08:37:34.297 -> [3712] AT version:1.2.0.0(Jul  1 2016 20:04:45)
08:37:34.297 -> SDK version:1.5.4.1(39cb9a32)
08:37:34.297 -> v1.0.0
08:37:34.297 -> Mar 11 2018 18:27:31
08:37:34.332 -> OK
08:37:44.463 -> [13846] Failed to connect WiFi
08:37:54.782 -> [24192] Ready (ping: 24ms).
08:38:04.017 -> argh
08:38:04.053 -> argh2
08:38:04.053 -> [10] 
08:38:04.053 ->     ___  __          __
08:38:04.053 ->    / _ )/ /_ _____  / /__
08:38:04.053 ->   / _  / / // / _ \/  '_/
08:38:04.053 ->  /____/_/\_, /_//_/_/\_\
08:38:04.053 ->         /___/ v1.0.1 on Arduino Nano
08:38:04.053 -> 
08:38:04.538 -> [518] Connecting to  *[the wifi router]* 
08:38:05.575 -> [1534] ESP is not responding
08:38:15.909 -> [11878] Ready (ping: 25ms).
08:38:26.307 -> argh
08:38:26.341 -> argh2
08:38:26.341 -> [11] 
08:38:26.341 ->     ___  __          __
08:38:26.341 ->    / _ )/ /_ _____  / /__
08:38:26.341 ->   / _  / / // / _ \/  '_/
08:38:26.341 ->  /____/_/\_, /_//_/_/\_\
08:38:26.341 ->         /___/ v1.0.1 on Arduino Nano
08:38:26.341 -> 
08:38:26.834 -> [519] Connecting to  *[the wifi router]* 
08:38:27.848 -> [1533] ESP is not responding
08:38:38.200 -> [11883] Ready (ping: 25ms).
[repeat]

Edit to add this link to the diagram of my hardware configuration from this post.

Seems like a strange wiring setup to me.
I cant see any logical reason to have an impendence in the Rx signal line, or why you’d be pulling the Rx line down to GND with a 2K resistor (which is an odd value anyway).

I also don’t know why you’d use a 10K resistor between CH_PD and VCC, when that would normally be link wire.

Pete.

It’s the same setup as this one, this post, and this one.

It’s worked for several sample scripts, like this one and this one so I don’t think it’s a problem with the ESP-01 (or its wiring). It seems it is something Blynk related?

What makes you think that these two messages are Blynk related?

Pete.

The first of those example uses an Uno, which has 5v logic levels, and reccomends a 20K resistor between Rx and GND, although the author says that he didn’t bother with any resistors.
The second and third examples don’t use a resistor in the Rx line, or between Rx and GND.
The the first and second examples has no resistor between CH_PD and VCC. The third example has a 1K resistor between CH_PD and VCC.

In theory, a resistor may ne required in the Rx line if you are using a 5v board such as an Uno or Mega, but in reality the ESP-01 can tolerate 5v logic levels, provided it has a 3.3v supply voltage.
With a 3.3v board like you are using, the resistor is not required, and in reality it attenuates the logic level signals to a point where they are unacceptably low.

Pete.

The non-Blynk code works continuously and repeatedly without an issue. Those errors are immediately followed by “Ready (ping: 25ms)” and the Blynk console shows the device is online (then flips to offline after 11 seconds.)

The 11 seconds cycle could conceivably be a timeout in Blynk.begin that for some reason restarts setup() instead of failing the method. It is apparent that setup() is completely restarting since the serial monitor shows my poor-mans debug of “argh” and “argh2”.

If this helps, here’s the sample code that works.

 * Based on these examples:
 * https://create.arduino.cc/projecthub/jeffpar0721/add-wifi-to-arduino-uno-663b9e
 * http://yaab-arduino.blogspot.nl/p/wifiesp.html
 * 
 * This sketch uses the library WiFiEsp. If it is not installed yet, install it via Tools > Manage Libraries
 * or get it from https://github.com/bportaluri/WiFiEsp and extract the Zip-file to Documents\Arduino\libraries\WiFiEsp
 * 
 * Check the output in the Serial Monitor.
 */

#include "WiFiEsp.h"

// Emulate EspSerial on pins 2/3 if not present
#ifndef HAVE_HWEspSerial
#include "SoftwareSerial.h"
SoftwareSerial EspSerial(2, 3); // RX, TX
#endif

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

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

  // 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
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
}

void loop()
{
  // check the network connection once 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);
}

and here’s the serial monitor output for that script.

11:24:14.251 -> [WiFiEsp] Initilization successful - 1.5.4
11:24:14.251 -> Attempting to connect to WPA SSID: [the WiFi router]
11:24:17.324 -> [WiFiEsp] Connected to [the WiFi router]
11:24:17.324 -> You're connected to the network
11:24:17.361 -> 
11:24:17.361 -> SSID: [the WiFi router]
11:24:17.503 -> BSSID: 88:D7:F6:65:2B:D0
11:24:17.571 -> Signal strength (RSSI): -56
11:24:17.641 -> IP Address: 192.168.50.235
11:24:17.743 -> MAC address: 7C:87:CE:81:E7:5A
11:24:27.718 -> 
11:24:27.718 -> SSID: [the WiFi router]
11:24:27.892 -> BSSID: 88:D7:F6:65:2B:D0
11:24:27.964 -> Signal strength (RSSI): -55
11:24:28.069 -> IP Address: 192.168.50.235
11:24:28.136 -> MAC address: 7C:87:CE:81:E7:5A
11:24:38.133 -> 
11:24:38.133 -> SSID: [the WiFi router]
11:24:38.310 -> BSSID: 88:D7:F6:65:2B:D0
11:24:38.379 -> Signal strength (RSSI): -58
11:24:38.483 -> IP Address: 192.168.50.235
11:24:38.552 -> MAC address: 7C:87:CE:81:E7:5A
11:24:48.566 -> 
11:24:48.566 -> SSID: [the WiFi router]
11:24:48.704 -> BSSID: 88:D7:F6:65:2B:D0
11:24:48.808 -> Signal strength (RSSI): -56
11:24:48.882 -> IP Address: 192.168.50.235
11:24:48.948 -> MAC address: 7C:87:CE:81:E7:5A
11:24:58.974 -> 
11:24:58.974 -> SSID: [the WiFi router]
11:24:59.118 -> BSSID: 88:D7:F6:65:2B:D0
11:24:59.185 -> Signal strength (RSSI): -57
11:24:59.288 -> IP Address: 192.168.50.235
11:24:59.361 -> MAC address: 7C:87:CE:81:E7:5A
11:25:09.366 -> 
11:25:09.366 -> SSID: [the WiFi router]
11:25:09.542 -> BSSID: 88:D7:F6:65:2B:D0
11:25:09.613 -> Signal strength (RSSI): -57
11:25:09.683 -> IP Address: 192.168.50.235
11:25:09.791 -> MAC address: 7C:87:CE:81:E7:5A
11:25:19.781 -> 
11:25:19.781 -> SSID: [the WiFi router]
11:25:19.962 -> BSSID: 88:D7:F6:65:2B:D0
11:25:20.035 -> Signal strength (RSSI): -58
11:25:20.103 -> IP Address: 192.168.50.235
11:25:20.208 -> MAC address: 7C:87:CE:81:E7:5A
11:25:30.211 -> 
11:25:30.211 -> SSID: [the WiFi router]
11:25:30.357 -> BSSID: 88:D7:F6:65:2B:D0
11:25:30.426 -> Signal strength (RSSI): -57
11:25:30.530 -> IP Address: 192.168.50.235
11:25:30.599 -> MAC address: 7C:87:CE:81:E7:5A
11:25:40.602 -> 
11:25:40.602 -> SSID: [the WiFi router]
11:25:40.748 -> BSSID: 88:D7:F6:65:2B:D0
11:25:40.851 -> Signal strength (RSSI): -58
11:25:40.918 -> IP Address: 192.168.50.235
11:25:41.026 -> MAC address: 7C:87:CE:81:E7:5A
11:25:51.025 -> 
[repeat ad infinitum]