#include <WiFi.h>
#include <HX711.h>
#include <ThingSpeak.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* thingSpeakApiKey = "2J4LISME87ZWZ9AD";
const unsigned long postingInterval = 60000; // Interval at which to update ThingSpeak (in milliseconds)
const int HX711_dout = 21; // Pin connected to HX711's DOUT pin
const int HX711_sck = 22; // Pin connected to HX711's SCK pin
const int buzzerPin = 19; // Pin connected to the buzzer
HX711 scale;
WiFiClient client;
unsigned long lastConnectionTime = 0;
void setup() {
Serial.begin(115200);
pinMode(buzzerPin, OUTPUT);
scale.begin(HX711_dout, HX711_sck);
scale.set_scale();
scale.tare();
WiFi.begin(ssid, password);
ThingSpeak.begin(client);
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
if (millis() - lastConnectionTime >= postingInterval) {
float weight = scale.get_units(10); // Get weight in kg
Serial.print("Weight: ");
Serial.println(weight);
if (weight > 25) {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
} else {
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
ThingSpeak.writeField(1, (float)weight, thingSpeakApiKey);
lastConnectionTime = millis();
}
} else {
Serial.println("WiFi not connected!");
delay(1000);
}
}