#include <WiFi.h>
#include <ThingSpeak.h>
#include <HX711.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int LOADCELL_DOUT_PIN = 15;
const int LOADCELL_SCK_PIN = 2 ;
const float calibration_factor = 100.0;
HX711 scale;
long units;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
unsigned long myChannelNumber = 2279390;
const char * myWriteAPIKey = "DMV9RHKW37I550XH";
int statusCode;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor);
scale.tare();
Serial.println("Hello, ESP32!");
lcd.init();
lcd.backlight();
}
//String normal="normal";
// int overload="overload";
void loop() {
units = scale.get_units();
if(WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass);
Serial.print(".");
delay(1000);
}
Serial.println("\nConnected.");
}
// Get a weight reading from the load cell
float weight = scale.get_units();
// LCD PRINT THE WEIGHT ON THE SERIAL MONITORING
// Print the weight on the serial monitor
Serial.print("Weight: ");
lcd.setCursor(2,0);
lcd.print("weight :");
lcd.print(weight);
lcd.print(" kg");
Serial.print(weight);
Serial.println(" kg");
delay(1000);
// Serial.println(units);
ThingSpeak.setField(1,weight);
statusCode = ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
if(statusCode ) { //successful writing code
Serial.println("Channel update successful.");
}
else {
Serial.println("Problem Writing data. HTTP error code :" +String(statusCode));
}
delay(1000);
}