#define BLYNK_TEMPLATE_ID "TMPL6iRTl97h_"
#define BLYNK_TEMPLATE_NAME "Smart Hanger"
#define BLYNK_AUTH_TOKEN "1afpoFPdTeGGjocpbBtotZUUXZDULEK-"

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

#define ssid "Wokwi-GUEST"
#define pass ""

#define LoadCell_DT_Pin 19
#define LoadCell_SCK_Pin 21

#define Led_Work 15
#define Led_Dryer 2
#define Buzzer 4
#define PIR_sensor 5

Servo servo1, servo2;
HX711 scale;

bool systemOn = false;
unsigned long lasthungTime = 0;
unsigned long startTime = 0;
int lastWeight = 0;
int currentWeight = 0;
bool clothesHung = false;
bool finished = false;
bool warning = false;

void setup() {
  Serial.begin(9600);
  servo1.attach(14);
  servo2.attach(12);
  scale.begin(LoadCell_DT_Pin, LoadCell_SCK_Pin);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  pinMode(Led_Work, OUTPUT);
  pinMode(Led_Dryer, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  pinMode(PIR_sensor, INPUT);
}

BLYNK_WRITE(V0) {
  int angle = param.asInt();
  if (systemOn) {
    servoControl(angle);
    if ((digitalRead(PIR_sensor) == HIGH) && (!warning)) {
    {
      digitalWrite(Buzzer, HIGH);
      Blynk.logEvent("object_detection");
      warning = true;
    }  
    } else if (digitalRead(PIR_sensor) == LOW){
      digitalWrite(Buzzer, LOW);
      warning = false;
    }
  }
}

BLYNK_WRITE(V1) {
  int button = param.asInt();
  if (button == 1) 
  {
    digitalWrite(Led_Work, HIGH);
    systemOn = true;
    startTime = millis();
    lasthungTime = millis();
  } 
  else 
  {
    digitalWrite(Led_Work, LOW);
    systemOn = false;
    servoControl(0);
    reset();
    clothesHung = false;
  }
}

void loop() {
  Blynk.run();
  if (systemOn) 
  {
    currentWeight = kilograms();
    if ((currentWeight > 0)&&(!clothesHung)) 
    {
      digitalWrite(Led_Dryer, HIGH);
      clothesHung = true;
      lasthungTime = millis();
    } 
    else if (currentWeight > 0) //đồ khô
    {
      unsigned long elapsedTime = millis() - lasthungTime;
      if ((elapsedTime >= 20000) && (currentWeight == lastWeight)) //10s
      {
        Blynk.logEvent("finished");
        digitalWrite(Led_Dryer, LOW); 
        lasthungTime = millis();
      }
    }
    else // bật nhưng k dùng sau 10s
    {
      // If weight is zero, check if it has been zero for 30 seconds
      clothesHung = false;
      unsigned long elapsedTime = millis() - lasthungTime;
      if (elapsedTime >= 20000) { // 10s
        systemOn = false;
        servoControl(0);
        reset();
      }
    }
    lastWeight = currentWeight;
  }
  delay(100); // this speeds up the simulation
}


float kilograms() {
  float kilograms = 0;
  if (scale.is_ready()) {
    float weight = scale.get_units();
    weight *= 2.381;
    kilograms = weight / 1000;
  }
  return kilograms;
}

void servoControl(float angle)
{
  servo1.write(angle);
  servo2.write(angle);
}

void reset()
{
  systemOn = false;
  digitalWrite(Led_Work, LOW);
  digitalWrite(Led_Dryer, LOW);
  Blynk.virtualWrite(V0, 0);
  Blynk.virtualWrite(V1, 0);
  startTime = millis();
}
$abcdeabcde151015202530354045505560fghijfghij