/* ESP32 HTTP IoT Server Example for Wokwi.com
https://wokwi.com/projects/320964045035274834
To test, you need the Wokwi IoT Gateway, as explained here:
https://docs.wokwi.com/guides/esp32-wifi#the-private-gateway
Then start the simulation, and open http://localhost:9080
in another browser tab.
Note that the IoT Gateway requires a Wokwi Club subscription.
To purchase a Wokwi Club subscription, go to https://wokwi.com/club
*/
#include <GyverButton.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>
#include <RTClib.h>
#include "HX711.h"
// #include <GyverHX711.h>
// #include "HX711.h"
// HX711 circuit wiring
// #include "HX711_ADC.h"
// button btnManualFeeding(32);
// button btnReset(33);
#define BTN_MANUAL_FEEDING_PIN 32
#define BTN_RESET_PIN 33
GButton btnManualFeeding(BTN_MANUAL_FEEDING_PIN);
GButton btnReset(BTN_RESET_PIN);
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const int LOADCELL_DOUT_PIN = 19;
const int LOADCELL_SCK_PIN = 18;
// HX711_ADC LoadCell(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
HX711 scale;
float calibration_factor = 0.42;
RTC_DS1307 rtc;
// const int btnManualFeedingPin = 32;
// int oldValueBtnManualFeeding = LOW; // default/idle value is low.
// const int btnResetPin = 33;
// int oldValueBtnReset = LOW; // default/idle value is low.
// #include <AccelStepper.h>
// #include <Stepper.h>
// const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// // for your motor
// // initialize the stepper library on pins 8 through 11:
// Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// #define dirPin 0
// #define steppin 4
// #define enablePin 16
// AccelStepper stepper(1, steppin, dirPin);
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
WebServer server(80);
const int LED1 = 26;
const int LED2 = 27;
bool led1State = false;
bool led2State = false;
const int dirPin = 5;
const int stepPin = 23;
const int stepsPerRevolution = 200;
// HX711 scale;
// GyverHX711 sensor(32, 33, HX_GAIN64_A);
// HX_GAIN128_A - канал А усиление 128
// HX_GAIN32_B - канал B усиление 32
// HX_GAIN64_A - канал А усиление 64
void sendHtml() {
String response = R"(
<!DOCTYPE html><html>
<head>
<title>ESP32 Web Server Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html { font-family: sans-serif; text-align: center; }
body { display: inline-flex; flex-direction: column; }
h1 { margin-bottom: 1.2em; }
h2 { margin: 0; }
div { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; grid-auto-flow: column; grid-gap: 1em; }
.btn { background-color: #5B5; border: none; color: #fff; padding: 0.5em 1em;
font-size: 2em; text-decoration: none }
.btn.OFF { background-color: #333; }
</style>
</head>
<body>
<h1>ESP32 Web Server</h1>
<div>
<h2>LED 1</h2>
<a href="/toggle/1" class="btn LED1_TEXT">LED1_TEXT</a>
<h2>LED 2</h2>
<a href="/toggle/2" class="btn LED2_TEXT">LED2_TEXT</a>
</div>
</body>
</html>
)";
response.replace("LED1_TEXT", led1State ? "ON" : "OFF");
response.replace("LED2_TEXT", led2State ? "ON" : "OFF");
server.send(200, "text/html", response);
}
void setup(void) {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.print("Загрузка...");
rtc.begin();
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
btnManualFeeding.setTickMode(AUTO);
btnManualFeeding.setType(LOW_PULL);
// btnManualFeeding.setDebounce(50);
btnReset.setTimeout(3000);
btnReset.setTickMode(AUTO);
btnReset.setType(LOW_PULL);
btnReset.setTimeout(10000);
// // Initialize the pin for reading the button.
// pinMode(btnManualFeedingPin, INPUT);
// pinMode(btnResetPin, INPUT);
// pinMode(enablePin, OUTPUT) ;
// // digitalWrite(enablePin, LOW);
// stepper.setMaxSpeed (1000);
// myStepper.setSpeed(60);
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
Serial.println("Initializing the scale");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
lcd.clear();
lcd.print("Подключение к Wi-Fi");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Подключен к Wi-Fi!");
lcd.setCursor(0, 1);
lcd.print("IP:");
lcd.setCursor(4, 1);
lcd.print(WiFi.localIP());
server.on("/", sendHtml);
server.on(UriBraces("/toggle/{}"), []() {
String led = server.pathArg(0);
Serial.print("Toggle LED #");
Serial.println(led);
switch (led.toInt()) {
case 1:
led1State = !led1State;
digitalWrite(LED1, led1State);
break;
case 2:
led2State = !led2State;
digitalWrite(LED2, led2State);
break;
}
sendHtml();
});
server.begin();
Serial.println("HTTP server started");
// by the SCALE parameter (not set yet)
// scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
// scale.tare(); // reset the scale to 0
// LoadCell.begin();
// LoadCell.start(1000);
// LoadCell.setCalFactor(0.4198); //419.8
scale.set_scale();
scale.tare();
long zero_factor = scale.read_average();
Serial.print("Zero factor: ");
Serial.println(zero_factor);
}
void loop(void) {
server.handleClient();
// delay(2);
// if (btnManualFeeding.click()) Serial.println("btnManualFeeding press");
// if (btnReset.click()) Serial.println("btnReset press");
if (btnManualFeeding.isHolded()) Serial.println("btnManualFeeding");
if (btnReset.isHolded()) Serial.println("btnReset Holded");
// // Read the value of pin 8.
// int newValue = digitalRead(btnManualFeedingPin);
// // Check if the value was changed,
// // by comparing it with the previous value.
// if(newValue != oldValueBtnManualFeeding)
// {
// if(newValue == HIGH)
// {
// Serial.println("The button is pressed.");
// }
// else
// {
// Serial.println("The button is released.");
// }
// // Remember the value for the next time.
// oldValueBtnManualFeeding = newValue;
// }
// stepper.moveTo(1000) ;
// // while (stepper.distanceToGo() != 1000) {
// stepper.run();
// // }
// myStepper.step(stepsPerRevolution);
// delay(500);
// // step one revolution in the other direction:
// Serial.println("counterclockwise");
// myStepper.step(-stepsPerRevolution);
// delay(500);
int weight = scale.get_units() / 0.4145;
if (weight < 60) {
digitalWrite(dirPin, HIGH);
for (int x = 0; x < stepsPerRevolution; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
}
// if (sensor.available()) {
// Serial.println(sensor.read());
// }
// delay(100);
// if (scale.is_ready()) {
// // scale.set_scale();
// // Serial.println("Tare... remove any weights from the scale.");
// // delay(5000);
// // scale.tare();
// // Serial.println("Tare done...");
// // Serial.print("Place a known weight on the scale...");
// // delay(5000);
// long reading = scale.get_units(10) / 419.8;
// Serial.print("Result: ");
// Serial.println(reading);
// }
// else {
// Serial.println("HX711 not found.");
// }
// delay(1000);
// LoadCell.update();
// float i = LoadCell.getData();
// Serial.println(i);
// Serial.println(weight, 1);
}