#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL3f8NjF3lP"
#define BLYNK_TEMPLATE_NAME "Real time Inventory Tracking System"
#define BLYNK_AUTH_TOKEN "v0x8Z3Cfu5sqACD8aYgkFGJbnlmWhiXP"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include "HX711.h"
/* WiFi credentials */
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
/* HX711 pins (match Wokwi) */
#define DT_PIN 4
#define SCK_PIN 5
HX711 scale;
/* Inventory settings */
const int TOTAL_BOOKS = 20;
const float WEIGHT_PER_BOOK = 0.50; // kg (change if needed)
float shelfWeight = 0;
int booksRemaining = 0;
/* Timer */
BlynkTimer timer;
/* -------- TARE BUTTON (V3) -------- */
BLYNK_WRITE(V3) {
int tareCmd = param.asInt();
if (tareCmd == 1) {
scale.tare();
Serial.println("Shelf tared");
}
}
/* -------- READ LOAD CELL -------- */
void readLoadCell() {
if (!scale.is_ready()) {
Serial.println("HX711 not ready");
return;
}
shelfWeight = scale.get_units(10);
if (shelfWeight < 0) shelfWeight = 0;
booksRemaining = shelfWeight / WEIGHT_PER_BOOK;
if (booksRemaining > TOTAL_BOOKS) booksRemaining = TOTAL_BOOKS;
if (booksRemaining < 0) booksRemaining = 0;
/* Shelf status logic */
String status;
if (booksRemaining == 0) {
status = "EMPTY";
} else if (booksRemaining <= 5) {
status = "LOW STOCK";
} else {
status = "OK";
}
/* Send to Blynk */
Blynk.virtualWrite(V0, shelfWeight); // Shelf Weight (kg)
Blynk.virtualWrite(V1, booksRemaining); // Books Remaining
Blynk.virtualWrite(V2, status); // Shelf Status
/* Serial monitor */
Serial.print("Weight (kg): ");
Serial.print(shelfWeight);
Serial.print(" | Books: ");
Serial.print(booksRemaining);
Serial.print(" | Status: ");
Serial.println(status);
}
void setup() {
Serial.begin(9600);
/* HX711 init */
scale.begin(DT_PIN, SCK_PIN);
scale.set_scale(420.0); // ⚠️ Adjust after calibration
scale.tare();
/* Blynk connection */
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
/* Read sensor every 2 seconds */
timer.setInterval(2000L, readLoadCell);
}
void loop() {
Blynk.run();
timer.run();
}