About Device offline


why it show like this

coding

#define BLYNK_TEMPLATE_ID "TMPL6mqBMDzCJ"
#define BLYNK_TEMPLATE_NAME "Petpal Feeder Pro"
#define BLYNK_AUTH_TOKEN "WIswWuhxulyWC2AGizDMNe_SONZ2cbD5"

#include <Wire.h>
#include <RTClib.h>
#include <ESP32Servo.h>
#include <NewPing.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

// Blynk Auth Token
char auth[] = "WIswWuhxulyWC2AGizDMNe_SONZ2cbD5";  // Replace with your Blynk Auth Token

// Your WiFi credentials
char ssid[] = "iPhone";  // Replace with your WiFi SSID
char pass[] = "yan12345678";     // Replace with your WiFi Password

// RTC
RTC_DS3231 rtc;

// Servo
Servo bowlServo;
Servo valveServo;

// Ultrasonic Sensor
#define TRIGGER_PIN 5
#define ECHO_PIN 17
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

// Blynk Virtual Pins for scheduling
#define VPIN_SCHEDULE_DAY V1
#define VPIN_SCHEDULE_HOUR V2
#define VPIN_SCHEDULE_MINUTE V3

// Schedule variables
int scheduleDay;
int scheduleHour;
int scheduleMinute;

// Flag to check if feeding is in progress
bool feedingInProgress = false;

void setup() {
  Serial.begin(115200);
  delay(100);

  // Initialize RTC
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  } else {
    Serial.println("RTC initialized successfully");
  }

  // Initialize Servos
  bowlServo.attach(18);
  valveServo.attach(19);
  Serial.println("Servos attached");

  // Initialize WiFi
  Serial.print("Connecting to WiFi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected");

  // Initialize Blynk
  Serial.println("Connecting to Blynk...");
  Blynk.begin(auth, ssid, pass);

  // Wait for Blynk connection
  while (!Blynk.connected()) {
    delay(1000);
    Serial.println("Attempting to connect to Blynk...");
  }
  Serial.println("Connected to Blynk");

  // Synchronize time
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set RTC to compile time
  // rtc.adjust(DateTime(2024, 5, 27, 12, 0, 0)); // Or set manually
}

void loop() {
  if (Blynk.connected()) {
    Blynk.run();
  } else {
    Serial.println("Blynk disconnected, attempting to reconnect...");
    Blynk.connect();  // Attempt to reconnect
  }

  DateTime now = rtc.now();
  if (now.dayOfTheWeek() == scheduleDay && now.hour() == scheduleHour && now.minute() == scheduleMinute && !feedingInProgress) {
    startFeedingCycle();
  }

  if (feedingInProgress) {
    checkFeedingStatus();
  }
}

void startFeedingCycle() {
  feedingInProgress = true;

  // Move bowl out
  bowlServo.write(90); // Adjust angle as necessary
  delay(1000); // Allow time for bowl to move

  // Open valve
  valveServo.write(90); // Adjust angle as necessary
  delay(10000); // Keep valve open for 10 seconds

  // Close valve
  valveServo.write(0); // Adjust angle as necessary

  // Start timer to retract bowl after 10 minutes
  delay(600000); // 10 minutes
  bowlServo.write(0); // Retract bowl
  feedingInProgress = false;
}

void checkFeedingStatus() {
  int distance = sonar.ping_cm();
  if (distance > 0 && distance < 20) {
    Serial.println("Food level low!");
    // Send notification via Blynk Event
    Blynk.logEvent("low_food_level", "Food level is below 20 cm!");
  }
}

BLYNK_WRITE(VPIN_SCHEDULE_DAY) {
  scheduleDay = param.asInt();
}

BLYNK_WRITE(VPIN_SCHEDULE_HOUR) {
  scheduleHour = param.asInt();
}

BLYNK_WRITE(VPIN_SCHEDULE_MINUTE) {
  scheduleMinute = param.asInt();
}

You’re doing some very weird stuff in your sketch. Is this something you’ve written yourself, or something you’ve found on the internet?

The first place to start debugging is to look at your serial monitor and see what that tells you.
However, this approach…

will never work with Blynk, because your blocking delays won’t work with Blynk, they will lead to disconnections.

Your use of Blynk.begin() and the manual WiFi connection code and Blynk.connected() logical tests aren’t compatible either.

You should also be using a BlynkTimer to do your checks on whether the feeding cycle is due, and not have that code in your void loop.

You also need to synchronise your hardware RTC against either the Blynk server time (using the Blynk RTC functionality) or an NTP time source.

Pete.