-1

My ESP32 just connects for a few minutes, then it auto-disconnects from the Blynk Cloud. Here is my code:

#define BLYNK_TEMPLATE_ID "TMPL6Wh61nbcI"
#define BLYNK_TEMPLATE_NAME "Parcel Notification"
#define BLYNK_AUTH_TOKEN "e74YnhO-dduRqYb00jUxX2g-Nxtprgzf"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <SimpleTimer.h> 

SimpleTimer timer;

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Tank60_2.4Ghz";
char pass[] = "a1b0124746780"; 
//char ssid[] = "Kingjames2003";
//char pass[] = "1234King"; 

#define VPIN_BUTTON_1    V1
#define VPIN_BUTTON_2    V2

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#include <Keypad.h>
#define Password_Lenght 4
#define Otp_Lenght 4
int otp;
char customKey;
String otpstring;
String password = "1234";
bool door = 1;

#include <ESP32Servo.h>
Servo Dservo; // Door for use collect
Servo Rservo; // Right servo
Servo Lservo; // Left servo
#define DSERVO_PIN 15
#define RSERVO_PIN 2
#define LSERVO_PIN 4

#define TRIG_PIN 5
#define ECHO_PIN 18
int empty_cm = 75 , max_cm = 25; // Have to reset after finish the prototype 
int pervious_cm = empty_cm; 
float duration_us, distance_cm; 

#define UV_LED 19

const byte ROWS = 4;
const byte COLS = 3;

char keys [ROWS] [COLS] = {

  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}

};

byte rowPins[ROWS] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33};

Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

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

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

BLYNK_CONNECTED() {
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
}

void setup() {
  Serial.begin(9600);
  timer.setInterval(2000L, checkBlynkStatus);
  Blynk.begin(auth, ssid, pass);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  pinMode(UV_LED,OUTPUT);

  Dservo.attach(DSERVO_PIN);
  Rservo.attach(RSERVO_PIN);
  Lservo.attach(LSERVO_PIN);
  ServoClose();

  lcd.init();
  lcd.backlight();
  lcd.print("      Smart     ");
  lcd.setCursor(0, 1);
  lcd.print("   Parcel Box   ");
  delay(3000);
  lcd.clear();
}

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

void start() {
  if (door == 0) {
    customKey = myKeypad.getKey();
    if (customKey == '#') {
      lcd.clear();
      lcd.print("Closing ...");
      ServoClose();
      delay(1000);
      door = 1;
      Check();
      UV();
    }
  } else
    Open();
}

void ServoOpenUser() {
  Dservo.write(90);
}

void ServoOpenDelivery() {
  Rservo.write(90);
  Lservo.write(90);
}

void ServoClose() {
  Dservo.write(0);
  Rservo.write(0);
  Lservo.write(180);
}

void UV() {
  delay(5000);
  digitalWrite(UV_LED, HIGH);
  delay(5000);
  digitalWrite(UV_LED, LOW);
}

void Open() {
  lcd.setCursor(0,0);
  lcd.print("Press '*' to get");
  lcd.setCursor(0,1);
  lcd.print("   OTP code   ");

  customKey = myKeypad.getKey();

  if (customKey) {
    if (customKey == '*') {
      Serial.print("OTP generate");
      generateOTP();
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Enter OTP:");
      String y = "";
      int a = y.length();

      while (a < Otp_Lenght) {
        char customKey = myKeypad.getKey();

        if (customKey) {
          lcd.setCursor(0, 1);
          y = y + customKey;
          lcd.print(y);
          a = y.length();
        }
      }
      Serial.print("Entered OTP is ");
      Serial.println(y);

      if (otpstring == y) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print(" Access Granted");
        ServoOpenDelivery();
        Serial.print("Access Granted");
        delay(2000);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("   Put Parcel   ");
        lcd.setCursor(0,1);
        lcd.print("    On Marker   ");
        door = 0;

      } else {
        lcd.clear();
        lcd.print(" Access Denied ");
        delay(1000);
        door = 1;
      }
    } else {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter password:");
      String y = "";
      int a = y.length();
      while (a < Password_Lenght) {

        char customKey = myKeypad.getKey();
        if (customKey) {
          lcd.setCursor(0, 1);
          y = y + customKey;
          lcd.print(y);
          a = y.length();
        }
      }

      Serial.print("Entered password is ");
      Serial.println(y);

      if (password == y) {
        lcd.clear();
        ServoOpenUser();
        lcd.print(" Access Granted");
        delay(2000);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("   Get Parcel   ");
        pervious_cm = empty_cm;
        door = 0;;
      } else {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("owner Failed");
        Serial.print("Access Failed");
        delay(3000);
      }
    }
  }
}

void generateOTP() {
  otp = random(2000, 9999);
  otpstring = String(otp);
  Serial.print("Your OTP code is: ");
  Serial.print(otpstring);
  Blynk.logEvent("otp");
  Blynk.virtualWrite(VPIN_BUTTON_2, (String(otpstring)));
  delay(100);
}

void Check() {
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration_us = pulseIn(ECHO_PIN, HIGH);
  distance_cm = 0.017 * duration_us;
  int percent = ((distance_cm - max_cm) / empty_cm)*100;

  if ((pervious_cm - distance_cm) >= 2) {
    Blynk.logEvent("parcel_arrived");
    Serial.println("Parcel get");
    Serial.print("Percent: ");
    Serial.print(percent);
    Serial.println(" %");
    Blynk.virtualWrite(VPIN_BUTTON_1, percent);
    pervious_cm = distance_cm;

    if (distance_cm <= max_cm) {
      Blynk.logEvent("parcel_box_full");
      Serial.println("Full");
    }
  } else if (distance_cm = empty_cm) {
      Blynk.logEvent("parcel_collected");
      Serial.println("Collected");
  } else {
    Blynk.logEvent("parcel_is_not_arrived");
    Serial.println("Parcel is not arrived");
  }
}
5
  • the ``` must be placed on a separate line
    – jsotola
    Apr 1 at 15:57
  • I notice your code does not try to reconnect when it is detecting that it has lost the connection to Blynk in checkBlynkStatus(). Also, there is no Blynk.run() in your loop() or anywhere else.
    – StarCat
    Apr 2 at 7:43
  • could I reconnect my wifi by code? Apr 2 at 13:19
  • How could I reconnect my wifi by code? Apr 2 at 13:45
  • owh i have sucess after adding blynk.run thanks StarCat Apr 2 at 13:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.