Commenting out BLYNK_PRINT causing unstable connection

I’m working on an ESP8266-01 + UNO project and I was having issues with the ESP staying connected to the server. I checked the H/W and everything seems fine. I went through the S/W and ended up discovering that I can get it to stay connected by replacing the line…

// #define BLYNK_PRINT Serial

…with the line…

#define BLYNK_PRINT Serial1

… while keeping everything else exactly the same. I used Serial1 here because I don’t want the blynk printout to interfere with my serial communication with the UNO. I’m absolutely flummoxed as to why or how this could lead to a stable connection! I’ve tested it multiple times and the results keep being the same. With line 1 commented out the esp keeps connecting and disconnecting with occasional data coming through.

I’m wondering if anyone has any insights as to what is going on here? I’m glad that I got it to work but I would very much like to solve the underlying issue.

Project Info:

The intention of this project is to learn how to use the ESP8266 for future projects.

Arduino Uno rev 3 uses a temp sensor and a heater controlled by a relay to control the temperature of a cupboard. The UNO communicates to an ESP8266 via serial port. The ESP connects to blink via WIFI so that I can control the UNO through the app, as well as see some LED’s and temperature readings.

H/W:

generic rev 3 arduino uno
esp8266-01 black

ESP TX —> arduino pin 10 (softwareSerial RX)
ESP RX —> arduino pin 11 (softwareSerial TX) via resistor level shift
ESP GND & VCC —> filtered 3.3v supply from LM1117
CH_PD —> 10k pull-up
RST —> 10k pull-up
GPIO’s —> floating

Using Blynk server, Android App, blynk library 0.6.1, and ESP8266 board manager 2.5.0

S/W:

here’s my code, the version of it that is STABLE (line 1 not commented out)

#define BLYNK_PRINT Serial1

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxx";
char ssid[] = "yyy";
char pass[] = "zzz";

// initialize timer
BlynkTimer timer;

// assign the virtual pins for Blynk
WidgetLED heatLED(V0);
WidgetLED timeoutErrorLED(V1);
WidgetLED cycleErrorLED(V2);
WidgetLED commErrorLED(V3);

// assign variables
unsigned long previousCommErrorLED = 0;       // timestamp variable for comm error led
const int commErrorTimeout = 2000;            // how long comm error LED stays on after error
bool commError = false;                       // becomes true when comm LED turns on

void setup() {

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(300, recvDataFromUNO);

}

void loop() {

  Blynk.run();
  timer.run();

}

void recvDataFromUNO() {

  int dataIn[5] = {0, 0, 0, 0, 0};
  int dataGarbage = 0;
  int setTempFromUNO = 0;
  int readTempFromUNO = 0;

  if (Serial.available() >= 5) {

    for (int n = 0; n < 5; n++) {
      dataIn[n] = Serial.read();
    }

    // Validate dataIn and proceed accordingly
    if (dataIn[0] == 18 && dataIn[4] == 19) {

      // update heat LED on blynk
      if (dataIn[1] & 0x01) {
        heatLED.on();
      }
      else {
        heatLED.off();
      }

      // update timeout LED on blynk
      if (dataIn[1] & 0x02) {
        timeoutErrorLED.on();
      }
      else {
        timeoutErrorLED.off();
      }

      // update cycle LED on blynk
      if (dataIn[1] & 0x04) {
        cycleErrorLED.on();
      }
      else {
        cycleErrorLED.off();
      }

      // update set temp and read temp on blynk
      setTempFromUNO = dataIn[2] * 5;
      readTempFromUNO = dataIn[3] * 5;
      Blynk.virtualWrite(V6, setTempFromUNO);
      Blynk.virtualWrite(V5, readTempFromUNO);
    }

    else {

      for (int n = 0; n < 32; n++) {
        dataGarbage = Serial.read();
      }

      commErrorLED.on();
      previousCommErrorLED = millis();
      commError = true;
    }
  }

  // turn off comm error LED after 2 seconds of being on
  if (millis() - previousCommErrorLED >= commErrorTimeout && commError == true) {
    commErrorLED.off();
    commError = false;
  }
}

// this code runs when the temp is changed through the app
BLYNK_WRITE(V4) {

  int setTempTimesTen = param.asInt();
  byte dataOut[5] = {18, 0, 0, 0, 19};

  // prepare dataOut array and send to UNO
  dataOut[2] = setTempTimesTen / 5;
  Serial.write(dataOut, 5);

}

Okay, you’re using your ESP-01 and Uno in a rather unconventional way, with the ESP running the sketch that you’ve included above.
Normally, people would use the ESP-01 as a Wi-Fi modem for the Uno, and have the ESP-01 running the factory default “AT” software and upload their sketch to the Uno. In that scenario, the Uno would be using the BlynkSimpleShieldEsp8266.h library and connecting the ESP-01 via a SoftwareSerial port.

When used in this way, the Uno’s large USB connector (which is connected to Serial) would be used as the debug port and #define BLYNK_PRINT Serial would send information to the serial monitor about the Blynk connection, ping time etc.

By including #define BLYNK_PRINT Serial in your ESP-01 sketch you are telling the Blunk library to send all of this data to the Serial port on your ESP-01, which is obviously tying-up that port.
Changing this to #define BLYNK_PRINT Serial1 is telling the Blynk library to send this data to the (non-existent) Serial1 port of your ESP-01. Personally, I’m somewhat surprised that this compiles, but to be honest, the best thing would be to remove this line altogether as it serves no purpose with your setup.

Actually, the best thing to do would be to replace the ESP-01 with a NodeMCU or Wemos D1 Mini and throw the ESP-01 and Uno in the scrap box.

Pete.

Thanks Pete for the very swift and detailed reply. I’ll get a NodeMCU and surely most of my ESP-01 frustration will subside!

TBH I was also surprised that the code compiled at all with Serial1. Please note that the unstable version of my code had the first line commented out i.e. the compiler is ignoring it. That’s the part that I found confusing!