Disconnecting from device

hello … i’ve connected my esp8266 to blynk app and it says that my device is online and i tried many instructions and it works … but sometimes i get disconnected from the server for a reason i don’t know . it says my device offline and then trining to reconnect to the server … it take a few seconds to reconnect again … but why it happens?

37 reasons which are specific to your hardware and sketch.

@Costas and what are these 37 reasons?

@Mohamed_Tantawy and what is your hardware and sketch?

@Costas hardware is esp 8266 and the sketch is reading the temprature using DHT11 using blynk’s example

Which one?

Paste your formatted sketch.

And your sketch?

sorry but what do u mean by sketch ? the code?

Yes and which version of ESP8266 do you have?

Node MCU

and that’s the code

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You can send/receive any data using WidgetTerminal object.

  App project setup:
    Terminal widget attached to Virtual Pin V1
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


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

// 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[] = "GoldeN";
char pass[] = "assd203020";

//////////////////////////////////////////////////////
#define DHTPIN 2          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
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;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

/////////////////////////////////////////////////////
// Attach virtual serial terminal to Virtual Pin V1
WidgetTerminal terminal(V1);
//Blynk.virtualWrite(V5,5);

// You can send commands from Terminal to your hardware. Just use
// the same Virtual Pin as your Terminal Widget
BLYNK_WRITE(V1)
{
  pinMode(15,OUTPUT);
String val;
  

   // Send it back
   // terminal.print("You said:");
  //  terminal.write(param.getBuffer(), param.getLength());
    terminal.println();
     val=param.asStr();
   //  Serial.println(val);
     if(val=="led1")
      {
        digitalWrite(15,1);
        
      }else {digitalWrite(15,0);}
  
  // Ensure everything is sent 
  terminal.flush();
}

void setup()
{
  // Debug console
  Serial.begin(230400);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  // This will print Blynk Software version to the Terminal Widget when
  // your hardware gets connected to Blynk Server
//  terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
//  terminal.println(F("-------------"));
//  terminal.println(F("Type 'Marco' and get a reply, or type"));
//  terminal.println(F("anything else and get it printed back."));
  terminal.flush();
   dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

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

@Mohamed_Tantawy you said you were using Blynk’s DHT11 sketch. You have modified the sketch.

It’s normal, but not compulsory, to set pinMode in setup().
I have never tried it within a BLYNK_WRITE function but I would move it to setup().
String val; will not pick up details from the Terminal (see Terminal sketch for procedure)
Do you have V5 and V6 set to PUSH in your project?

Reasons 1 to 37 for disconnects:

  1. Bad code

This may not affect your connection… but the DHT11 is notoriously slow, as it generally take 2-5 seconds to process a new signal. No sense in a 1 second polling for it… try 5 seconds instead.