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);
}