#include <HX711.h>
#include <LiquidCrystal_I2C.h>
#include <FirebaseESP32.h>
// Define AUTH and HOST for Firebase
#define DATABASE_URL "e-scale-49b87-default-rtdb.firebaseio.com"
#define DATABASE_SECRET "WEA1Ok8dHf2rSteApbM1e3WpHensUTKhPGGsiDva"
// Define PIN
// First loadcell HX711
#define DOUT_PIN 12
#define SCK_PIN 14
// Second loadcell HX711
#define DOUT_PIN_1 26
#define SCK_PIN_1 27
#define BUTTON_PIN 17 // Green - Reset PIN
#define BUTTON_PIN_1 5 // Yellow - Change unit weight PIN
#define BUTTON_PIN_2 16 // Red
#define BUTTON_PIN_3 4 // Blue
#define HISTORY_SIZE 50
float weightHistory[HISTORY_SIZE];
int historyIndex = 0;
// Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
// 2 HX711
HX711 scale;
HX711 scale_1;
// Liquid Crystal I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
bool buttonState = HIGH;
bool buttonState_1 = HIGH;
bool buttonState_2 = HIGH;
bool buttonState_3 = HIGH;
bool lastButtonState = HIGH;
bool lastUpSizeState = HIGH;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
void setup() {
Serial.begin(115200);
// Connect to wifi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
config.database_url = DATABASE_URL;
config.signer.tokens.legacy_token = DATABASE_SECRET;
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
Serial.println("Firebase is connected");
// Khai báo 2 load cell
scale.begin(DOUT_PIN, SCK_PIN);
scale_1.begin(DOUT_PIN_1, SCK_PIN_1);
//Reset load cell và set_scale cho nó
scale.tare();
scale.set_scale(0.42f);
scale_1.tare();
scale_1.set_scale(0.42f);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUTTON_PIN_1, INPUT_PULLUP);
pinMode(BUTTON_PIN_2, INPUT_PULLUP);
pinMode(BUTTON_PIN_3, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("Hello!");
delay(2000);
lcd.clear();
}
void loop() {
delay(100);
}
/*
float firstWeight = scale.get_units(4);
float secondWeight = scale_1.get_units(4);
float weight = firstWeight + secondWeight;
buttonState = digitalRead(BUTTON_PIN);
buttonState_1 = digitalRead(BUTTON_PIN_1);
buttonState_2 = digitalRead(BUTTON_PIN_2);
buttonState_3 = digitalRead(BUTTON_PIN_3);
if (buttonState == LOW && lastButtonState == HIGH) { //Green
performReset();
}
if (buttonState_1 == LOW) { //Yellow
performConvert();
}
if (buttonState_2 == LOW && lastButtonState == HIGH) { //Red
performLogHistory();
}
if (buttonState_3 == LOW && lastButtonState == HIGH) { //Blue
performDisplayHistory();
}
lastButtonState = buttonState;
if (lastUpSizeState == LOW) {
display_Kilogram(weight);
} else {
display_Gram(weight);
}
delay(1000);
}
void display_Kilogram(float weight) {
weight = weight / 1000;
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Weight: ");
lcd.setCursor(3, 1);
lcd.print(weight);
lcd.print(" kg");
}
void display_Gram(float weight) {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Weight: ");
lcd.setCursor(3, 1);
lcd.print(weight);
lcd.print(" g");
}
void performConvert() {
Serial.println("Change from gram to kilogram.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Change g to kg");
lastUpSizeState = LOW;
delay(1000);
lcd.clear();
}
void performReset() {
Serial.println("You pressed Reset Button.");
scale.tare();
scale_1.tare();
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Resetting ...");
Serial.println("Resetting ...");
delay(1000);
lcd.clear();
}
void performLogHistory() {
Serial.println("Logging weight history...");
if (historyIndex < HISTORY_SIZE) {
float firstWeight = scale.get_units(4);
float secondWeight = scale_1.get_units(4);
float weight = firstWeight + secondWeight;
weightHistory[historyIndex] = weight;
historyIndex++;
Serial.print("Weight logged: ");
Serial.print(weight);
Serial.println(" g");
lcd.clear();
lcd.setCursor(2, 0);
lcd.println("Weigh logged.");
delay(1000);
lcd.clear();
} else {
Serial.println("History full. Cannot log more.");
lcd.clear();
lcd.setCursor(2, 0);
lcd.println("Cannot log more.");
delay(1000);
lcd.clear();
}
}
void performDisplayHistory() {
Serial.println("Displaying weight history...");
lcd.setCursor(0, 0);
lcd.println("Displaying history...");
String weightData = "";
for (int i = 0; i < historyIndex; i++) {
weightData += "Entry " + String(i) + ": " + String(weightHistory[i]) + " g\n";
}
}
*/