// #if defined(ESP8266)
// #include <ESP8266WiFi.h>
// #elif defined(ESP32)
// #include <WiFi.h>
// #endif
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h> // เปิดการใช้งาน eeprom
#endif
//pins:
const int HX711_dout = 13; //mcu > HX711 dout pin
const int HX711_sck = 12; //mcu > HX711 sck pin
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck); // เปิดการใช้งาน โหลด cell
const int calVal_calVal_eepromAdress = 0;
unsigned long t = 0;
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define SERIAL_DEBUG_BAUD 115200
#define LED 15
int status = WL_IDLE_STATUS;
void InitWiFi(){
Serial.println("Connecting to AP ...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.println(".");
}
Serial.println("Connected to AP");
}
void reconnect(){
status = WiFi.status();
if (status != WL_CONNECTED){
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.println(".");
}
Serial.println("Connected to AP");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(SERIAL_DEBUG_BAUD);
Serial.println();
InitWiFi();
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print(WIFI_SSID);
LCD.setCursor(0, 1);
LCD.print(WiFi.localIP());
pinMode(LED, OUTPUT);
float calibrationValue; // calibration value 136916.35;145186.00 155574.12
// calibrationValue = 155574.12;// uncomment this if you want to set this value in the sketch
calibrationValue = 420;
#if defined(ESP8266) || defined(ESP32)
//EEPROM.begin(512); // uncomment this if you use ESP8266 and want to fetch this value from eeprom
#endif
//EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch this value from eeprom
LoadCell.begin();
//LoadCell.setReverseOutput();
unsigned long stabilizingtime = 0; // tare preciscion can be improved by adding a few seconds of stabilizing time
boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
}
else {
LoadCell.setCalFactor(calibrationValue); // set calibration factor (float)
Serial.println("Startup is complete");
}
while (!LoadCell.update());
Serial.print("Calibration value: ");
Serial.println(LoadCell.getCalFactor());
Serial.print("HX711 measured conversion time ms: ");
Serial.println(LoadCell.getConversionTime());
Serial.print("HX711 measured sampling rate HZ: ");
Serial.println(LoadCell.getSPS());
Serial.print("HX711 measured settlingtime ms: ");
Serial.println(LoadCell.getSettlingTime());
Serial.println("Note that the settling time may increase significantly if you use delay() in your sketch!");
if (LoadCell.getSPS() < 7) {
Serial.println("!!Sampling rate is lower than specification, check MCU>HX711 wiring and pin designations");
}
else if (LoadCell.getSPS() > 100) {
Serial.println("!!Sampling rate is higher than specification, check MCU>HX711 wiring and pin designations");
}
}
void loop() {
static boolean newDataReady = 0;
const int serialPrintInterval = 100; //increase value to slow down serial print activity
// check for new data/start next conversion:
if (LoadCell.update()) newDataReady = true;
// get smoothed value from the dataset:
if (newDataReady) {
if (millis() > t + serialPrintInterval) {
float i = LoadCell.getData();
Serial.print("Load_cell output val: ");
Serial.println(i);
newDataReady = 0;
t = millis();
}
}
delay(1000);
if (WiFi.status() != WL_CONNECTED){
reconnect();
}
else {
Serial.print("WiFi SSID ");
Serial.println(WIFI_SSID);
Serial.print("IP: ");
Serial.println(WiFi.localIP());
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
}
}