Blynk APP works for like 200+ seconds then automatically disconnect! HELP

Hello, I have searched for a way to fix my problem from cleaning the void loop, and such. But my journey is not successful!!

The thing is my Blynk APP and Codes works perfectly if these conditions are met, Arduino is connected to my computer via USB, and the power jack is connected to 12v power supply (optional) plus the esp8266 connection is established.
12V power supply is mainly used to power up my 12v fan.

I am using Arduino UNO and Esp8266.
Now what I am trying to do now is to remove that USB connection and only use the 12V power supply as my main power source (for now).

My guess is that when my project is connected to the computer, it is not disconnecting because of the serial monitoring being used at the time. On the other hand, it automatically disconnects because blynk server describe it as something unrelevant, cant find the words.

Codes:

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#include <TimeLib.h>
#include <WidgetRTC.h>

BlynkTimer timer;
WidgetRTC rtc;
String currentTime;
String currentDate;
int sensorValue;
int PWM = 3;
int PWMVal;

//ARDUINO CONNECTION
int ledpin5 = 5;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "********************************";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***"; //Network SSID for HOME: ***, SPHONE: ***
char pass[] = "***";  //Network PASS for HOME: ***, PHONE: ***
// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&Serial);
void setup()  
{
 // Debug console
 Serial.begin(9600);
 delay(10);
 // Set ESP8266 baud rate
 Serial.begin(ESP8266_BAUD);
 delay(10);
 Blynk.begin(auth, wifi, ssid, pass);
 rtc.begin();
 timer.setInterval(5000L, sendUptime);
 timer.setInterval(5100L, clockDisplay);
 timer.setInterval(5200L, sensorDataSend);
}
void sendUptime() {
  Blynk.virtualWrite(0, millis() / 1000);
}
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
int HOUR = hour();
  if ( HOUR > 12 ) {
    HOUR = HOUR - 12;
    String currentTime = String(HOUR) + ":" + minute() + ":" + second() + " PM";
    Blynk.virtualWrite(V2, currentTime);  // Send PM time to the App
  } else {
    String currentTime = String(hour()) + ":" + minute() + ":" + second() + " AM"; ;
    Blynk.virtualWrite(V2, currentTime);  // Send AM time to the App
  }
  String currentDate = String(month()) + "-" + day() + "-" + year();
  Blynk.virtualWrite(V1, currentDate);  // Send date to the App
}
void sensorDataSend(){
sensorValue = analogRead(1);
Blynk.virtualWrite(A1, sensorValue);
Serial.print("Air Quality = ");
Serial.print(sensorValue, DEC);               // prints the value read
Serial.println(" PPM");
delay(100);

if(sensorValue >50){
sensorValue = 100;
analogWrite(ledpin5, HIGH);
}  
PWMVal = map(sensorValue, 50, 100, 15, 255);
if(sensorValue <25){
PWMVal = 0;
}
if(sensorValue <90){
analogWrite(ledpin5, LOW);
}
analogWrite(PWM, PWMVal);
}
void loop()
{
Blynk.run();
timer.run();
}

@Reyster please edit your post using the pencil icon at the bottom and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Hello, I updated my post

I’m surprised that your code works at all.
You’re using the same serial port for both debugging messages, and communication between the Uno and the ESP-01. You can’t do this.

The Uno only has one hardware serial port and its best to use that for debug messages to the serial monitor. As you need an additional serial port to connect to the ESP-01, the solution is to use a software emulator to create another, and that’s normally the SoftwareSerial library.
You’ve included the SoftwareSerial library, but not initiated an instance of the software serial object. You’d normally do that like this…

SoftwareSerial EspSerial(2, 3); // RX, TX

which tells the SoftwareSerial library to create a second serial port called EspSerial and use pins 2 and 3 of the Uno for this.
Pin 2 is the Rx pin, which will connect to the Tx pin on the EXP-01 and pin 3 is the Tx which connects to the ESP-01’s Rx pin.

You then have to tell the Blynk library to use this new software port as the way to talk to the ESDP-01, like this:

ESP8266 wifi(&EspSerial);

and initiate the new serial port in the void setup like this:

EspSerial.begin(ESP8266_BAUD);

This obviously means that you have some redundant lines of code that need to go, like these:

ESP8266 wifi(&Serial);

Serial.begin(ESP8266_BAUD);

Pete.

1 Like

Wow, thanks for the detailed reply. That is probably why i have communication problem between Uno and Esp. I used to have that code but i deleted that out for some reason.

Just to make it clear when you said pin 2 of the Uno is Rx, while the pin 3 of the Uno is Tx, are you talking about the Digital pins? or the SoftwareSerial library is responsible of those changes/controls??

Should i transfer my currently connection of Rx pin of Uno to Tx pin of Esp & Tx pin of Uno to Rx pin of Esp to those said pins. Correct me if i am wrong please.

Also thanks for helping me!!!

When you add this line:

SoftwareSerial EspSerial(2, 3); // RX, TX

you’re telling the Software Serial library to create a second serial port, which is referenced as EspSerial, and which uses pins 2 and 3 of the Uno as the Rx and Tx pins.

Yes.
The ESP-01 Tx pin will connect to the Uno’s Pin 2 and
The ESP-01 Rx pin will connect to the Uno’s Pin 3.

Your PC will still connect via the Uno’s onboard USB connector when required.

Pete.

1 Like

Thank you a lot!!

It is working now for 1500 seconds and counting. Unlike before it automatically disconnects after 200+ seconds. Without the connection of Uno USB to PC.

Now i am monitoring it if it will disconnect after some time and stop working.

P.S.
I have to change/adjust one of my pins since i have a connection on pin 3 to control my fan, so i transferred my fan controls to pin 6.

Hello, again and Happy Holidays.

One thing, after days of monitoring it still disconnects from blynk server from time to time, but this only happens when not connected to a computer (without serial monitoring).
Yes, only on blynk server i checked the connection on internet/wifi. It is still connected, but on app it says it was “offline”. My system/hardware is still working though.

Also i added a feature where it should automatically reconnect when not connected to the wifi and server. I tested on wifi it works, on server it still works. But if it is not connected to a computer (without serial monitoring) sometimes it don’t automatically reconnect to wifi unless i pressed the reset button or sometimes unplug and plug it.

Here is my latest code:

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <SoftwareSerial.h>
#include <WiFi.h>

SoftwareSerial EspSerial(2, 3); // RX, TX
BlynkTimer timer;
WidgetRTC rtc;
String currentTime;
String currentDate;
int sensorValue;
int PWM = 6;
int PWMVal;
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter
//int port = 8080;

//ARDUINO CONNECTION
int ledpin5 = 5;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "E5MMgwuPtw5fV8EFwW65ltSaP9d3RCCB";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "R01_00000"; //Network SSID for HOME: R01_00000, PHONE: A5_Pro
char pass[] = "32681363";  //Network PASS for HOME: 32681363, PHONE: 12345678
//char server[] = "xxx.xxx.xxx.xxx";  // IP for Local Cloud Server
//char server[] = "blynk-cloud.com";  // URL for Blynk Cloud Server


// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
void setup()  
{
// Debug console
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10); 


Blynk.config(wifi, auth);
Blynk.connectWiFi(ssid, pass);

rtc.begin();
timer.setInterval(5000L, sendUptime);
timer.setInterval(5100L, clockDisplay);
timer.setInterval(5200L, sensorDataSend);
}
BLYNK_CONNECTED() {
Serial.println("Connected");
ReCnctCount = 0;
}

void sendUptime() {
Blynk.virtualWrite(0, millis() / 1000);
}

void clockDisplay()
{
 // You can call hour(), minute(), ... at any time
 // Please see Time library examples for details
int HOUR = hour();
 if ( HOUR > 12 ) {
   HOUR = HOUR - 12;
   String currentTime = String(HOUR) + ":" + minute() + ":" + second() + " PM";
   Blynk.virtualWrite(V2, currentTime);  // Send PM time to the App
 } else {
   String currentTime = String(hour()) + ":" + minute() + ":" + second() + " AM"; ;
   Blynk.virtualWrite(V2, currentTime);  // Send AM time to the App
 }
 String currentDate = String(month()) + "-" + day() + "-" + year();
 Blynk.virtualWrite(V1, currentDate);  // Send date to the App
}

void sensorDataSend(){
sensorValue = analogRead(1);
Blynk.virtualWrite(A1, sensorValue);
Serial.print("Air Quality = ");
Serial.print(sensorValue, DEC);               // prints the value read
Serial.println(" PPM");
delay(100);

if(sensorValue >100){
sensorValue = 100;
analogWrite(ledpin5, HIGH);
}  
PWMVal = map(sensorValue, 50, 100, 15, 255);
if(sensorValue <50){
PWMVal = 0;
}
if(sensorValue <90){
analogWrite(ledpin5, LOW);
}
analogWrite(PWM, PWMVal);
}

void loop()
{
timer.run();
if (Blynk.connected()) {  // If connected run as normal
   Blynk.run();
 } else if (ReCnctFlag == 0) {  // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
   ReCnctFlag = 1;  // Set reconnection Flag
   Serial.println("Starting reconnection timer in 30 seconds...");
   timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
     ReCnctFlag = 0;  // Reset reconnection Flag
     ReCnctCount++;  // Increment reconnection Counter
     Serial.print("Attempting reconnection #");
     Serial.println(ReCnctCount);
     Blynk.connect();  // Try to reconnect to the server
   });  // END Timer Function
 }
}

What i want is to make my system have a real-time monitoring through blynk app and reconnect whenever its connection is lost without the serial-monitoring from a computer.

Please help me achieve my goals, any help will be appreciated!!! Happy Holidays again

P.S.
Thanks to GTT for his codes/examples i used that as reference. Generally thanks to everyone who contributed on the forum, it is really a big help!!! Much appreciated

How are you powering your Arduino and ESP-01 when the ESP isn’t connected to your computer?
Do you have a separate power supply for your ESP-01, with a scared ground?
Is your Arduino power supply a good quality device providing the correct current and voltage?

Pete.

12V power supply through power jack, i dont have a separate power supply for esp-01. Esp-01 is connected to 3.3V of arduino … GND to GND of arduino.

Do you think the problem is power?? But the thing is Esp-01 built-in LED are on, red is on while the blue is blinking, so i think it’s not the power supply. But my fans is 12V, should i change it to 5V fans instead??

I am sorry if it really is the power supply, then it is not a blynk-related problem therefore my topic is on a different page.

Happy Holidays, i’ll try to get a 5V fan and see if it will become stable!! Thanks for the help

The general rule is that you should power the ESP-01 via an independent power supply that is capable of delivering 3.3v at 1A.

At the very least, you need a decent smoothing capacitor across the ESP-01’s power rail.

You mention fans. What is the role of these fans, and how are they connected to the UNO? Are you using a relay with these fans? Are they powered from the same power supply?

Pete.

The purpose of my project is to control the fans based on the readings of my sensor, i am not using relays, instead i am using a kind of transistor called MOSFET.

If not connected to the computer. Arduino, sensor, fans and Esp-01 is powered by a 12V power supply.

Fans Connection:
Red Wire(FANS) - Vin(ARDUINO)
Black Wire(FANS) - Drain(MOSFET)

MOSFET Connection:
Gate(MOSFET) - D6(ARDUINO)
Gate(MOSFET) - Resistor - Source(MOSFET)
Source(MOSFET) - GND BUS

Well, I’d say that there’s a good chance that…
a) the Arduino 3v3 pin isn’t providing enough current to the ESP-01
b) when the fans kick-in they will be causing a voltage drop on your Arduino supply. If you’re using PWM then they could be drawing quite a high level of current to start-up.
c) the fans can be creating electrical noise which may cause interference with the ESP-01’s Wi-Fi signals. I guess it depends if they are brushless motors or not.

Personally, when I’m using MOSFETS I never put a resistor between the Gate & Source.

You almost certainly need a good sized capacitor across the supply rail of the ESP-01 and it’s always recommended to use a separate 3v3 power supply capable of delivering at least 1 Amp and that has a nice clean output.

I’ve experienced problems in the past with a gate release system where a 12v AC supply is used to release the gate lock and it was being switched by a relay. The system worked fine on the bench, but kept rebooting the Arduino controlling the relay every time the gate was released. It needed a capacitor across the relay load contacts to filter-out the EMF being produced by the gate release coil, even though the circuit was totally separate from the Arduino supply circuit.

In your case you’re sharing the same supply to power the Arduino and the fans, so there is a high probability of back EMF from the fans into the supply line to the Arduino.

I’d start by powering the ESP-01 separately and putting a capacitor across the supply rails (but ensuring you have a common ground with the Arduino supply) and disconnecting the fans. If your system runs without disconnections then try re-connecting the fans and see what happens.

Pete.

1 Like

Okay, i will try to do that. Thanks for helping, i’ll update when the results are in.
And yes, i think my fun is not brushless not sure lol.

Thank you pete!!

1 Like