#include "WiFi.h"
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include "HX711.h"
static char *ssid = "Wokwi-GUEST";
static char *pass = "";
HTTPClient http;
String Firebase("https://pikzaza65-default-rtdb.asia-southeast1.firebasedatabase.app/");
#define SW1_1 12
#define SW1_0 13
#define SPK 32
#define DOUT 26
#define CLK 27
HX711 scale;
float calibration_factor = 420;
byte OUT, Turn = 0;
int JP1, JP2, JP3;
String Message, MessageTMP;
float Level_1_TMP = 0.0; // ค่า weight ปัจจุบัน
float weight = 0.0; // ค่า weight ที่อ่านได้จากเซ็นเซอร์
bool weightAdjusted = false; // ตรวจสอบว่าค่า weight ถูกปรับแล้วหรือไม่
void SW1_ON() {
OUT = 2; digitalWrite(LED_BUILTIN, 1);
}
void SW1_OFF() {
OUT = 1; digitalWrite(LED_BUILTIN, 0);
}
void Beep(int F, int L) {
for (int i = 0; i <= L; i++) {
digitalWrite(SPK, 1); delayMicroseconds(F);
digitalWrite(SPK, 0); delayMicroseconds(F);
}
}
void setup() {
Serial.begin(115200);
pinMode(SPK, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SW1_1, INPUT_PULLUP);
pinMode(SW1_0, INPUT_PULLUP);
attachInterrupt(SW1_1, SW1_ON, FALLING);
attachInterrupt(SW1_0, SW1_OFF, FALLING);
scale.begin(DOUT, CLK);
scale.set_scale(calibration_factor);
scale.tare();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
Serial.print("Connecting to WiFi .");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.'); delay(100);
}
Serial.println(WiFi.localIP());
}
void loop() {
byte Level_ = 0;
JsonDocument JP;
weight = scale.get_units(); // อ่านค่า weight จากเซ็นเซอร์
Serial.print("Weight: ");
Serial.println(weight, 5);
// เปรียบเทียบค่า weight กับ Level_1_TMP
float delta = 0.01; // ค่าความแตกต่างที่ยอมรับได้
if (abs(weight - Level_1_TMP) > delta) {
Level_1_TMP = weight;
JP["o1"] = Level_1_TMP;
Level_++;
}
// รับคำสั่งจาก Firebase (o3)
http.begin(Firebase + ".json");
int httpCode = http.GET();
if (httpCode == 200) {
String response = http.getString();
deserializeJson(JP, response);
if (JP["o3"] != nullptr) {
JP3 = JP["o3"];
if (JP3 == 1 && !weightAdjusted) { // ถ้า o3 เป็น 1 (ON)
Level_1_TMP += 1.0; // เพิ่มค่า weight ขึ้น 1 kg
weightAdjusted = true;
Serial.print("Adjusted Weight (ON): ");
Serial.println(Level_1_TMP, 5); // แสดงค่า Level_1_TMP ใน Serial Monitor ทันที
} else if (JP3 == 0 && !weightAdjusted) { // ถ้า o3 เป็น 0 (OFF)
Level_1_TMP -= 1.0; // ลดค่า weight ลง 1 kg
weightAdjusted = true;
Serial.print("Adjusted Weight (OFF): ");
Serial.println(Level_1_TMP, 5); // แสดงค่า Level_1_TMP ใน Serial Monitor ทันที
}
}
} else {
Serial.println("Failed to fetch data from Firebase. HTTP Code: " + String(httpCode));
}
// ส่งค่า weight ไปยัง Firebase
JP["o1"] = Level_1_TMP;
serializeJson(JP, Message);
http.begin(Firebase + ".json");
delay(1000); // ดีเลย์ 1 วินาที (ปรับค่าได้ตามต้องการ)
if (Level_ > 0 && Turn == 0) {
Serial.println("Sending to Firebase: " + Message);
httpCode = http.PATCH(Message);
Level_ = 0;
Turn = 1;
} else {
httpCode = http.GET();
Turn = 0;
}
// รีเซ็ตค่า weightAdjusted เมื่อคำสั่ง ON/OFF ถูกประมวลผลแล้ว
if (JP3 == 1 || JP3 == 0) {
weightAdjusted = false;
}
if (httpCode == 200) {
String Message = http.getString();
if (!MessageTMP.equals(Message)) {
MessageTMP = Message;
deserializeJson(JP, Message);
if (JP["o1"] != nullptr) JP1 = JP["o1"];
if (JP["o2"] != nullptr) JP2 = JP["o2"];
if (JP["o3"] != nullptr) JP3 = JP["o3"];
if (JP3 >= 1) {
digitalWrite(LED_BUILTIN, 1);
} else {
digitalWrite(LED_BUILTIN, 0);
}
Beep(1000, 4);
Serial.println("Received from Firebase: " + Message);
}
} else {
Serial.println("HTTP ERROR " + String(httpCode));
Beep(2000, 10);
Beep(300, 50);
}
}
ON
OFF