Connection With Device Was Interrupted Blynk 2.0 ESP32

The problem is whenever I am trying to connect a device to the New Blynk 2.0 app, after I enter my WiFi credentials, it tells me “Connection With Device Was Interrupted”, I try to reconnect in many time, it just all same.

`
my code.

   
// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLnUExcZdb"
#define BLYNK_DEVICE_NAME "Sliding durability"
#define BLYNK_FIRMWARE_VERSION        "0.1.1 "

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG
#include <DHT.h>  
#define DHTTYPE DHT22
#define DHTPIN 27
// define the GPIO connected with Relays and switches
#define RelayPin1 16  //Relay1
#define RelayPin2 17  //Relay2
#define RelayPin3 18  //Relay3

#define VPIN_BUTTON_1    V0     //startsw
#define VPIN_BUTTON_2    V1     //stopsw
#define VPIN_BUTTON_3    V2     //resetesp32
#define VPIN_BUTTON_4    V6     //resetcounter
#define VPIN_COUNTER     V3     //counter
#define VPIN_TEMPERATURE V4
#define VPIN_HUMIDITY    V5
float temperature1 = 0;
float humidity1   = 0;
int toggleState_1 = 0;
int toggleState_2 = 0;
int toggleState_3 = 0;
int toggleState_4 = 0;
const int  CounterPIN   = 35; 
int swresetcounPin = 33;
int swresetcoun = 0;
int CounterDoorClose = 0;   // counter for the number of doorclose
int openDoorState = 0;         // current state of the closedoor
int openlastDoorState = 0;     // previous state of the closedoor
bool doorclose = false;

DHT dht(DHTPIN, DHTTYPE);
#include "BlynkEdgent.h"
BlynkTimer timer2;
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
} 
void checkUp(){
  openDoorState = digitalRead(CounterPIN);
  if (openDoorState != openlastDoorState) {
    if (openDoorState == LOW) {
        doorclose = true;
      CounterDoorClose++;
      Serial.println(CounterDoorClose);
    } 
    delay(50);
  }
  openlastDoorState = openDoorState;
  Blynk.virtualWrite(V3, CounterDoorClose);
}
void swrscounter(){
  swresetcoun = digitalRead(swresetcounPin);
  if (openDoorState != openlastDoorState) {
    if (openDoorState == LOW) 
    {
        doorclose = true;
    CounterDoorClose = 0;
    digitalWrite(RelayPin2, LOW);
    }
  else { 
    digitalWrite(RelayPin2, HIGH);
       }
  }
}
void readSensor(){
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  else {
    humidity1 = h;
    temperature1 = t;
  }  
}

void sendSensor()
{
  readSensor();
  Blynk.virtualWrite(VPIN_HUMIDITY, humidity1);
  Blynk.virtualWrite(VPIN_TEMPERATURE, temperature1);
}

BLYNK_WRITE(VPIN_BUTTON_1) {
  toggleState_1 = param.asInt();
  if(toggleState_1 == 1){
    digitalWrite(RelayPin1, LOW);
  }
  else { 
    digitalWrite(RelayPin1, HIGH);
  }
}

BLYNK_WRITE(VPIN_BUTTON_2) {
  toggleState_2 = param.asInt();
  if(toggleState_2 == 1){
    digitalWrite(RelayPin2, LOW);
  }
  else { 
    digitalWrite(RelayPin2, HIGH);
  }
}

BLYNK_WRITE(VPIN_BUTTON_3) {
  toggleState_3 = param.asInt();
  if(toggleState_3 == 1){
    ESP.restart();
  }
}

BLYNK_WRITE(VPIN_BUTTON_4) {
  toggleState_4 = param.asInt();
  if(toggleState_4 == 1){
    digitalWrite(RelayPin2, LOW);
    CounterDoorClose = 0;
    digitalWrite(RelayPin3, LOW);
  }
}
void setup()
{
  Serial.begin(115200);
  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);

  pinMode( CounterPIN , INPUT_PULLUP);
  
  digitalWrite(RelayPin1, LOW);
  digitalWrite(RelayPin2, LOW);
  digitalWrite(RelayPin3, LOW);
  
  dht.begin();
  timer2.setInterval(2000L, sendSensor);
  BlynkEdgent.begin();
}

void loop() {
   timer2.run();
   BlynkEdgent.run();
   
}```

Hey there.

First try to delete the template and create a new one.
Second don’t use delay in the void loop, try to keep it clean as possible so your loop should contain blynkedgent.run(); and timer2.run(); only.

1 Like

Now, I delete template and create it’s working, but new problem is " Connecting to blynk.cloud:443 Secure connection failed", what should I do.

@Phawit_Peesiri 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.

1 Like

okay, I fix it.

What does your amended code look like?

Pete.

1 Like

yeah, it looks easy to read, but now I stuck in Connecting to blynk.cloud:443 issue.

   
// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLnUExcZdb"
#define BLYNK_DEVICE_NAME "Sliding durability"
#define BLYNK_FIRMWARE_VERSION        "0.1.1 "

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG
#include <DHT.h>  
#define DHTTYPE DHT22
#define DHTPIN 27
// define the GPIO connected with Relays and switches
#define RelayPin1 16  //Relay1
#define RelayPin2 17  //Relay2
#define RelayPin3 18  //Relay3

#define VPIN_BUTTON_1    V0     //startsw
#define VPIN_BUTTON_2    V1     //stopsw
#define VPIN_BUTTON_3    V2     //resetesp32
#define VPIN_BUTTON_4    V6     //resetcounter
#define VPIN_COUNTER     V3     //counter
#define VPIN_TEMPERATURE V4
#define VPIN_HUMIDITY    V5
float temperature1 = 0;
float humidity1   = 0;
int toggleState_1 = 0;
int toggleState_2 = 0;
int toggleState_3 = 0;
int toggleState_4 = 0;
const int  CounterPIN   = 35; 
int swresetcounPin = 33;
int swresetcoun = 0;
int CounterDoorClose = 0;   // counter for the number of doorclose
int openDoorState = 0;         // current state of the closedoor
int openlastDoorState = 0;     // previous state of the closedoor
bool doorclose = false;

DHT dht(DHTPIN, DHTTYPE);
#include "BlynkEdgent.h"
BlynkTimer timer2;
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
} 
void checkUp(){
  openDoorState = digitalRead(CounterPIN);
  if (openDoorState != openlastDoorState) {
    if (openDoorState == LOW) {
        doorclose = true;
      CounterDoorClose++;
      Serial.println(CounterDoorClose);
    } 
    delay(50);
  }
  openlastDoorState = openDoorState;
  Blynk.virtualWrite(V3, CounterDoorClose);
}
void swrscounter(){
  swresetcoun = digitalRead(swresetcounPin);
  if (openDoorState != openlastDoorState) {
    if (openDoorState == LOW) 
    {
        doorclose = true;
    CounterDoorClose = 0;
    digitalWrite(RelayPin2, LOW);
    }
  else { 
    digitalWrite(RelayPin2, HIGH);
       }
  }
}
void readSensor(){
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  else {
    humidity1 = h;
    temperature1 = t;
  }  
}

void sendSensor()
{
  readSensor();
  Blynk.virtualWrite(VPIN_HUMIDITY, humidity1);
  Blynk.virtualWrite(VPIN_TEMPERATURE, temperature1);
}

BLYNK_WRITE(VPIN_BUTTON_1) {
  toggleState_1 = param.asInt();
  if(toggleState_1 == 1){
    digitalWrite(RelayPin1, LOW);
  }
  else { 
    digitalWrite(RelayPin1, HIGH);
  }
}

BLYNK_WRITE(VPIN_BUTTON_2) {
  toggleState_2 = param.asInt();
  if(toggleState_2 == 1){
    digitalWrite(RelayPin2, LOW);
  }
  else { 
    digitalWrite(RelayPin2, HIGH);
  }
}

BLYNK_WRITE(VPIN_BUTTON_3) {
  toggleState_3 = param.asInt();
  if(toggleState_3 == 1){
    ESP.restart();
  }
}

BLYNK_WRITE(VPIN_BUTTON_4) {
  toggleState_4 = param.asInt();
  if(toggleState_4 == 1){
    digitalWrite(RelayPin2, LOW);
    CounterDoorClose = 0;
    digitalWrite(RelayPin3, LOW);
  }
}
void setup()
{
  Serial.begin(115200);
  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);

  pinMode( CounterPIN , INPUT_PULLUP);
  
  digitalWrite(RelayPin1, LOW);
  digitalWrite(RelayPin2, LOW);
  digitalWrite(RelayPin3, LOW);
  
  dht.begin();
  timer2.setInterval(2000L, sendSensor);
  BlynkEdgent.begin();
}

void loop() {
   timer2.run();
   BlynkEdgent.run();
   
}

So, you haven’t listened to the advice you were given…

Pete.

1 Like

@Phawit_Peesiri please try with the empty sketch that does nothing. Does that help?