I constantly drop my blynk server

I made a blynk server on my ubuntu server, it happens to me that I am wanting to put some sensors, but I just put two sensors and it drops all the time I don’t know if the code is wrong or is something from my server I have the wrong code for the reconnection…

#include <SPI.h>
#include <DHT.h>
#define BLYNK_PRINT Serial  
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D5 // DS18B20 on arduino pin2 corresponds to D4 on physical board "D4 pin on the ndoemcu Module"

char auth[] = "bJa4MaHmNr_YOrwoDS3lIBQUVJFazr-R";
char ssid[] = "Pablo y Maca 2.4";
char pass[] = "laputaclave";

BlynkTimer timer;

#define DHTPIN D4
   
#define DHTTYPE DHT22
#define RelayPin D6
#define RelayPin2 D7

int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter
int UpdateFrequency = 2000L;

DHT dht(DHTPIN, DHTTYPE);


OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature DS18B20(&oneWire);

float tempW;
float t,h;

WidgetLED led1(V11);
WidgetLED led2(V12);
WidgetLED led3(V13);

void setup()
{
  DS18B20.begin();
  dht.begin();
  delay(2250);

  pinMode(RelayPin,OUTPUT);
  digitalWrite(RelayPin,HIGH);
 
  pinMode(RelayPin2,OUTPUT);
  digitalWrite(RelayPin2,HIGH);
   

  Serial.begin(9600);

delay(100);
lcd.begin(20,4);  
lcd.init();
lcd.backlight();
lcd.setCursor (2,1);
lcd.print ("Conectando ... ");
lcd.setCursor (3,2);
lcd.print ("  Red Wifi ... ");
delay(200);

Blynk.begin(auth, ssid, pass, IPAddress(201,212,65,156), 8080);

while (Blynk.connect() == false) 
{
 // Wait until connected
}  

lcd.clear ();
lcd.setCursor (0,0);
lcd.print ("Conectado con.. ");
lcd.setCursor (0,1);
lcd.print (ssid);
lcd.setCursor (0,3);
lcd.print("IP:");
lcd.print( WiFi.localIP() );
delay(3000);
lcd.clear();

lcd.setCursor (3,1);
lcd.print("Iniciando Grow");
lcd.setCursor (4,3);
lcd.print("LOS LUISES...");
delay(3000);
lcd.clear();

timer.setInterval(UpdateFrequency, Temperatura);
timer.setInterval(UpdateFrequency, Lcd);
timer.setInterval(UpdateFrequency, Estados);
}

BLYNK_CONNECTED() {
  Serial.println("Cconnected");
  ReCnctCount = 0;
}


void Temperatura () {

h = dht.readHumidity(); // Read humidity (percent)
t = dht.readTemperature(); // Read temperature as C
tempW = DS18B20.getTempCByIndex(0);

DS18B20.requestTemperatures(); 

Blynk.virtualWrite(V10, tempW); //virtual pin V3
Blynk.virtualWrite(V6, t);
Blynk.virtualWrite(V5, h);
 
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
            return; 
 }

 }
 
void Lcd () {
    
    Temperatura();
    
    lcd.setCursor(0,0);
    lcd.print("Temp Tierra: ");
    lcd.print(tempW);
    lcd.print((char)223);
    lcd.print("c");

    lcd.setCursor(0,1);
    lcd.print("Temp Int: ");
    lcd.print(t);
    lcd.print((char)223);
    lcd.print("c");

    lcd.setCursor(0,2);
    lcd.print("Hum  Int: ");
    lcd.print(h);
    lcd.print("%");
}
  
void Estados ()  {

if (( t <= 27 ) && (h <= 62 )) {
led1.on();
led2.off();
led3.off();
}
else if (( t >= 28  and t < 29) && (h >= 71 and h < 80 )) {
led1.off();
led2.on();
led3.off();   
}

else if (( t >= 29) && (h >= 80 )) {

led1.off();
led2.off();
led3.on();  
 
}
}
 
 
 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 does your Ubuntu server say?

As always with timers, try a lower update frequency and see if it helps (i e higher value).

Your void loop() starts with the timer.run(). What happens if you start with the blynk.run() and then your timers, which also checks your connection?!

Something like this:

void loop() {
	if(Blynk.connected()) {
		Blynk.run();
	}
	timer.run();
}

timer.setInterval(30000L, reconnectBlynk);

void reconnectBlynk() {

	if (!Blynk.connected()) {
	
		Serial.println("Lost connection");
	
		if(Blynk.connect()) {
			Serial.println("Reconnected");
		}
	
		else {
			Serial.println("Not reconnected");
		}
	}
}

Thank you very much for the help, I was right, the frequency was very low, with respect to reconnections in this way I do not drop so much, in advance thank you very much for the time spent

1 Like