About weight sensor

#define BLYNK_TEMPLATE_NAME "Cat Feeder"
#define BLYNK_AUTH_TOKEN "3ZN1hubxV62y7SPnYxZKFFqxuplX9C5Q"

#include <WiFi.h>
#include <ESP32Servo.h>
#include <BlynkSimpleEsp32.h>
#include <HX711.h>

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 23;
const int LOADCELL_SCK_PIN = 22;

// Ultrasonic sensor pins
const int ECHO_PIN = 19;
const int TRIG_PIN = 18;
const float LOW_FOOD_THRESHOLD = 20.0; // Distance in cm indicating low food level

HX711 scale;
Servo myservo;
BlynkTimer timer;

int statusFeeding = 0; // Declare the variable to store the feeding status
const float targetWeight = 200.0; // Target weight of food to dispense in grams
float currentWeight = 0;
float calibration_factor = 1.0; // This should be set to the correct value after calibration

void sendWeightToBlynk() {
  currentWeight = scale.get_units(10); // Take the average of 10 readings for more stable value
  Blynk.virtualWrite(V4, currentWeight);
  Serial.print("Weight: ");
  Serial.println(currentWeight);
}

float getDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  long duration = pulseIn(ECHO_PIN, HIGH);
  float distance = duration * 0.034 / 2; // Calculate distance in cm
  return distance;
}

void checkFoodLevel() {
  float distance = getDistance();
  Serial.print("Food Level Distance: ");
  Serial.println(distance);
  Blynk.virtualWrite(V3, distance); // Send distance to Blynk

  static bool notificationSent = false; // Flag to track notification status

  if (distance < LOW_FOOD_THRESHOLD && !notificationSent) {
    Blynk.logEvent("food_level_");
    notificationSent = true; // Set flag to true after sending notification
  } else if (distance >= LOW_FOOD_THRESHOLD && notificationSent) {
    notificationSent = false; // Reset flag if distance goes above threshold
  }
}

void setup() {
  myservo.attach(12);
  myservo.write(0);

  Serial.begin(115200);

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

  WiFi.begin("iPhone", "yan12345678");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println("WiFi Connected");

  Blynk.begin(BLYNK_AUTH_TOKEN, "iPhone", "yan12345678");

  Serial.println("Blynk Connected");

  // Initialize the scale
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); // Set the calibration factor
  scale.tare(); // Reset the scale to 0

  Serial.println("Scale initialized");

  // Setup timers
  timer.setInterval(1000L, sendWeightToBlynk);
  timer.setInterval(5000L, checkFoodLevel); // Check food level every 5 seconds
}

void loop() {
  Blynk.run();
  timer.run(); // Run the BlynkTimer

  Serial.println("Status Feeding: " + String(statusFeeding));

  if (statusFeeding == 1) {
    scale.tare(); // Reset the scale to 0
    currentWeight = 0;

    // Move servo from 0 to 180 degrees until the target weight is reached
    myservo.write(180);
    while (currentWeight < targetWeight) {
      currentWeight = scale.get_units(10); // Take the average of 10 readings for more stable value
      Serial.print("Current Weight: ");
      Serial.println(currentWeight);
      delay(100);
    }

    // Move servo back to 0 degrees
    myservo.write(0);
    delay(500); // Small delay to ensure servo has returned

    // Reset the statusFeeding variable to 0
    statusFeeding = 0;
    Blynk.virtualWrite(V0, 0);
  }
}

BLYNK_WRITE(V0) {
  statusFeeding = param.asInt();
}

may i ask how i want to make sure that the value of this weight not all the time is 5000 even i dont put any things on load cell

Difficult to say without more information, but as well as still doing the weird stuff with your WiFi connection whilst using Blynk.begin() you don’t appear to have done any calibration of your loadcells, and you’re doing some weird stuff with repeated scale.tare() commands.

You also need to read this…

Pete.

actually i want to do the smart pet feeder where the food come out from tank storage through to the bowl plate that have weight sensor how much gram food like that. im using esp 32

Yes, i figured that much out already, but it’s the way that you’re approaching this that is the issue.

Pete.

#define BLYNK_TEMPLATE_NAME "Cat Feeder"
#define BLYNK_AUTH_TOKEN "3ZN1hubxV62y7SPnYxZKFFqxuplX9C5Q"

#include <WiFi.h>
#include <ESP32Servo.h>
#include <BlynkSimpleEsp32.h>
#include <HX711.h>

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 23;
const int LOADCELL_SCK_PIN = 22;

// Ultrasonic sensor pins
const int ECHO_PIN = 19;
const int TRIG_PIN = 18;
const float LOW_FOOD_THRESHOLD = 20.0; // Distance in cm indicating low food level

HX711 scale;
Servo myservo;
BlynkTimer timer;

int statusFeeding = 0; // Variable to store the feeding status
const float targetWeight = 200.0; // Target weight of food to dispense in grams
float currentWeight = 0;
float calibration_factor = -7050.0; // Replace with your calibration factor after calibration

char ssid[] = "iPhone";
char pass[] = "yan12345678";

// Function to calibrate the scale
void calibrateScale() {
  scale.set_scale(); // Reset the scale to default
  scale.tare(); // Reset the scale to 0

  Serial.println("Place a known weight on the scale...");
  delay(5000); // Wait for you to place the weight

  float knownWeight = 100.0; // Replace with the weight you used for calibration
  float reading = scale.get_units(10);
  calibration_factor = reading / knownWeight;
  Serial.print("Calibration Factor: ");
  Serial.println(calibration_factor);

  scale.set_scale(calibration_factor); // Set the new calibration factor
}

void sendWeightToBlynk() {
  currentWeight = scale.get_units(10); // Take the average of 10 readings for a more stable value
  Blynk.virtualWrite(V4, currentWeight);
  Serial.print("Weight: ");
  Serial.println(currentWeight);
}

float getDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  long duration = pulseIn(ECHO_PIN, HIGH);
  float distance = duration * 0.034 / 2; // Calculate distance in cm
  return distance;
}

void checkFoodLevel() {
  float distance = getDistance();
  Serial.print("Food Level Distance: ");
  Serial.println(distance);
  Blynk.virtualWrite(V3, distance); // Send distance to Blynk

  static bool notificationSent = false; // Flag to track notification status

  if (distance < LOW_FOOD_THRESHOLD && !notificationSent) {
    Blynk.logEvent("food_level_");
    notificationSent = true; // Set flag to true after sending notification
  } else if (distance >= LOW_FOOD_THRESHOLD && notificationSent) {
    notificationSent = false; // Reset flag if distance goes above threshold
  }
}

void setup() {
  myservo.attach(12);
  myservo.write(0);

  Serial.begin(115200);

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

  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("\nWiFi Connected");

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  Serial.println("Blynk Connected");

  // Initialize the scale
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  calibrateScale(); // Calibrate the scale during setup

  Serial.println("Scale initialized");

  // Setup timers
  timer.setInterval(1000L, sendWeightToBlynk);
  timer.setInterval(5000L, checkFoodLevel); // Check food level every 5 seconds
}

void loop() {
  Blynk.run();
  timer.run(); // Run the BlynkTimer

  if (statusFeeding == 1) {
    Serial.println("Starting feeding process...");
    scale.tare(); // Reset the scale to 0
    currentWeight = 0;

    myservo.write(180); // Move servo to 180 degrees to dispense food
    while (currentWeight < targetWeight) {
      currentWeight = scale.get_units(10); // Take the average of 10 readings for a more stable value
      Serial.print("Current Weight: ");
      Serial.println(currentWeight);
      delay(100);
    }

    myservo.write(0); // Move servo back to 0 degrees
    delay(500); // Small delay to ensure servo has returned

    statusFeeding = 0; // Reset the statusFeeding variable to 0
    Blynk.virtualWrite(V0, 0);
    Serial.println("Feeding process completed.");
  }
}

BLYNK_WRITE(V0) {
  statusFeeding = param.asInt();
}

is it okay this code or is it have some part

I don’t understand the question, but it appears to be the same as the code you posted previously.

Pete.