Is anyone else having a connection problem?

My Blynk Arduino project keeps disconnecting and reconnecting. pretty much every 3 seconds. It is not my board, wifi, or a power issue.

Please give me a solution.

my code is below…

#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxx"

char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxxxx";
bool fetch_blynk_state = true;
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h> //liquid crystal with I2C library
#include <Wire.h> //wire library
LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
int SCL_pin = 14; //Pin D5
int SDA_pin = 12; //Pin D6


char array1[] = "xxxxxxxxxxxxxxxx   "; //an array to print on line 0
char array2[] = "xxxxxxxxxxxxx           "; //an array to print on line 1
char array3[] = "xxxxxxxxxxx   "; //an array to print on line 0
char array4[] = "xxxxxxxxxxxxxxx    "; //an array to print on line 1
char array5[] = "xxxxxxxxxxxxxx     "; //an array to print on line 0
char array6[] = "xxxxxxxxxxxxxx    "; //an array to print on line 1
char array7[] = "xxxxxxxxxxxxx  "; //an array to print on line 0
char array8[] = "xxxxxxxxxxxxxxxxxxxx    "; //an array to print on line 1
char array9[] = "xxxxxxxxxxxxxxxxx   "; //an array to print on line 0
char array10[] = "xxxxxxxxxxxxxxxxx   "; //an array to print on line 1
char array11[] = "**Thank You**    "; //an array to print on line 1

/************************* Pin Definition *********************************/
#define DHTPIN        D7
#define RelayPin5     D1
#define wifiLed       D0

//buzzer to use it for alert
#define buzzer    D2
//Selection pins for multiplexer module to switch between different sensors and give data on a single analog pin.
#define S0                D3
#define S1                D4

//Analog pin to read the incoming analog value from different sensors.
#define analogpin         A0

int wifiFlag = 0;

int gas;
int motion;
int sound;
int light;

char auth[] = BLYNK_AUTH_TOKEN;


// Uncomment whatever type we're using!
#define DHTTYPE DHT11     // DHT 11

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void checkBlynkStatus() 
{ // called every 3 seconds by SimpleTimer

 bool isconnected = Blynk.connected();
 if (isconnected == false) {
   wifiFlag = 1;
   Serial.println("Blynk Not Connected");
   digitalWrite(wifiLed, LOW);
 }
 if (isconnected == true) {
   wifiFlag = 0;
   Serial.println("Blynk Connected");
   digitalWrite(wifiLed, HIGH);  
 }
}

void sendSensor()
{
 float h = dht.readHumidity();
 float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

 if (isnan(h) || isnan(t)) 
 {
   Serial.println("Failed to read from DHT sensor!");
   return;
 }
 Blynk.virtualWrite(V0, t);
 Blynk.virtualWrite(V1, h);
 Blynk.virtualWrite(V2, gas);
 Blynk.virtualWrite(V3, motion);
 Blynk.virtualWrite(V4, sound);
 Blynk.virtualWrite(V5, light);
}
        

void setup()
{
 Serial.begin(9600);

 pinMode(wifiLed, OUTPUT);
 dht.begin();
 pinMode(S0, OUTPUT);
 pinMode(S1, OUTPUT);
 pinMode(RelayPin5, OUTPUT);
 pinMode(A0, INPUT);
 pinMode(buzzer, OUTPUT);

 pinMode(SCL_pin, OUTPUT);
 pinMode(SDA_pin, OUTPUT);
 digitalWrite(SDA_pin,LOW);
 digitalWrite(SCL_pin, LOW);
 Serial.println("Beginning");
 Wire.begin(SDA_pin ,SCL_pin);
 lcd.backlight(); //Turn on backlight


//  digitalWrite(wifiLed, HIGH);
 //Blynk.begin(auth, ssid, pass);
 WiFi.begin(ssid, pass);
 timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
 Blynk.config(auth);
 delay(1000);

}

void loop()
{ 

 lcd.setCursor(0, 0); //set cursor at top left
 lcd.print(array1);//will only print the first letter of the array
 lcd.setCursor(0, 1); //set cursor at left space on 2nd row
 lcd.print(array2);//will only print the first letter of the array
 delay(3000);

 lcd.clear();
 lcd.setCursor(0, 0);
 for ( int positionCounter1 = 0; positionCounter1 < 17; positionCounter1++)
 {

   lcd.print(array3[positionCounter1]); //prints a 17 character array without scrolling
   delay(250);

 }

 {
   lcd.setCursor(0, 1);
   for (int positionCounter1 = 0; positionCounter1 < 17; positionCounter1++)
   {
     lcd.print(array4[positionCounter1]);//prints a 17 character or array without scrolling
   }
   delay(3000);
   lcd.clear();
 }
 lcd.setCursor(0, 0);
 for ( int positionCounter1 = 0; positionCounter1 < 17; positionCounter1++)
 {

   lcd.print(array5[positionCounter1]); //prints a 17 character array without scrolling
   delay(250);

 }

 {
   lcd.setCursor(0, 1);
   for (int positionCounter1 = 0; positionCounter1 < 17; positionCounter1++)
   {
     lcd.print(array6[positionCounter1]);//prints a 17 character or array without scrolling
   }
   delay(3000);
   lcd.clear();
 }
 lcd.setCursor(0, 0);
 for ( int positionCounter1 = 0; positionCounter1 < 17; positionCounter1++)
 {

   lcd.print(array7[positionCounter1]); //prints a 17 character array without scrolling
   delay(250);

 }

 {
   lcd.setCursor(0, 1);
   for (int positionCounter1 = 0; positionCounter1 < 17; positionCounter1++)
   {
     lcd.print(array8[positionCounter1]);//prints a 17 character or array without scrolling
   }
   delay(3000);
   lcd.clear();
 }
 lcd.setCursor(0, 0);
 for ( int positionCounter1 = 0; positionCounter1 < 17; positionCounter1++)
 {

   lcd.print(array9[positionCounter1]); //prints a 17 character array without scrolling
   delay(250);

 }

 {
   lcd.setCursor(0, 1);
   for (int positionCounter1 = 0; positionCounter1 < 17; positionCounter1++)
   {
     lcd.print(array10[positionCounter1]);//prints a 17 character or array without scrolling
   }
   delay(3000);
   lcd.clear();
    lcd.setCursor(1, 0); //set cursor at top left
 lcd.print(array1);//will only print the first letter of the array
 lcd.setCursor(2, 1);
 for (int positionCounter1 = 0; positionCounter1 < 17; positionCounter1++)
   {
     lcd.print(array11[positionCounter1]);//prints a 17 character or array without scrolling
   }
 }
// Now we can publish stuff!
 // Address 00
 digitalWrite(S0, LOW);
 digitalWrite(S1, LOW);
 gas = analogRead(analogpin);
 Serial.print("Gas - "); Serial.println(gas);

// Address 10
 digitalWrite(S0, HIGH);
 digitalWrite(S1, LOW);
 motion = analogRead(analogpin);
 if (motion > 512)
 {
   motion = 1;
   digitalWrite(RelayPin5, LOW);
   Serial.print("Motion Detected - ");
 }
 else
 {
   motion = 0;
   digitalWrite(RelayPin5, HIGH);
   Serial.print("Nothing - ");
 }
 
// Address 01
 digitalWrite(S0, LOW);
 digitalWrite(S1, HIGH);
 sound = analogRead(analogpin);
 Serial.print("sound - "); Serial.println(sound);

 


// Address 11
 digitalWrite(S0, HIGH);
 digitalWrite(S1, HIGH);
 int raw_light = analogRead(analogpin);
 light = map(raw_light, 1024, 0, 0, 100);
 Serial.print("Light - "); Serial.println(light);

 Blynk.run();
 timer.run();
} ```

first, please edit your post and add triple backticks ( ``` ) before and after your sketch like this
triple backticks

Second, you must read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

2 Likes

:scream:

2 Likes

That’s right. Loop should only contain

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

Try to put the rest of the code in a function called by a timer.

2 Likes

I don’t know how to call it a timer function. please help.
I try to call a function and try to it in loop function but it failed.

Read this topic

I’d start by reading this article, which @John93 already provided a link to…

Pete.

You already used timers :wink: