#include <WiFi.h>
#include <HTTPClient.h>
#include <HX711.h>
// HX711 circuit wiring
#define LOADCELL_DOUT_PIN 21
#define LOADCELL_SCK_PIN 22
// Alarm settings
#define ALARM_PIN 23
#define OVERLOAD_THRESHOLD 2500 // kg
HX711 scale;
// Wi-Fi settings
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak settings
const char* server = "api.thingspeak.com";
const char* apiKey = "BTB198A7IWZRC8AS";
void setup() {
Serial.begin(115200);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(); // Set the scale factor here (calibrate as needed)
scale.tare(); // Reset the scale to 0
pinMode(ALARM_PIN, OUTPUT);
digitalWrite(ALARM_PIN, LOW); // Turn off the alarm initially
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop() {
float weight = scale.get_units(10); // Average over 10 readings
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String("http://") + server + "/update?api_key=" + apiKey + "&field1=" + String(weight);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Data sent to ThingSpeak");
} else {
Serial.println("Error sending data");
}
http.end();
}
// Check if weight exceeds the threshold
if (weight > OVERLOAD_THRESHOLD) {
Serial.println("Overload! Activating alarm...");
digitalWrite(ALARM_PIN, HIGH); // Activate alarm
} else {
digitalWrite(ALARM_PIN, LOW); // Deactivate alarm
}
delay(10000); // Update every 20 seconds
}