/*********
Rui Santos
Complete project details at
https://RandomNerdTutorials.com/esp32-e ... eb-server/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*********/
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#else
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "LiquidCrystal_I2C.h"
#include <EEPROM.h>
#include <sbutton.h>
//==================================================================================================
//==================================================================================================
// REPLACE WITH YOUR NETWORK CREDENTIALS
const char* ssid = "r23";
const char* password = "123456789";
IPAddress ip(192,168,0,104); //статический IP ESP8266
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
// Default Threshold Temperature Value
//String inputMessage = "25.0";
String T_ON_Message = "25";
String T_OFF_Message = "27";
String lastTemperature;
String heater_state_str = "OFF";
String inputMessage2 = "true";
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<meta http-equiv="refresh" content="5">
<title>Chickens WEB control page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head><body>
<h3>Chickens WEB</h2>
<h3>Current temperature: %CURRENT_TEMPERATURE% °C</h3>
<h2>Heater setup: </h2>
<form action="/get">
Heater ON: <input type="number" step="1" name="T_ON_input" value="%T_ON_Message%" required><br>
Heater OFF: <input type="number" step="1" name="T_OFF_input" value="%T_OFF_Message%" required><br>
<input type="submit" value="Submit">
</form>
<h3>Heater is %HEATER_STATE%</h3>
</body></html>)rawliteral";
//---------------------------------------------------------------------------------------------------------------
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
AsyncWebServer server(80);
// Replaces placeholder with DS18B20 values
String processor(const String& var){
//Serial.println(var);
if(var == "CURRENT_TEMPERATURE"){return lastTemperature;}
else if(var == "T_ON_Message"){return T_ON_Message;}
else if(var == "T_OFF_Message"){return T_OFF_Message;}
else if(var == "HEATER_STATE"){return heater_state_str;}
return String();
}
//const char* PARAM_INPUT_2 = "enable_arm_input";
const char* PARAM_INPUT_1 = "T_ON_input";
const char* PARAM_INPUT_2 = "T_OFF_input";
// Interval between sensor readings. Learn more about ESP32 timers:
https://RandomNerdTutorials.com/esp32-p ... ts-timers/
unsigned long previousMillis = 0;
const long interval = 3000; //3 seconds
// GPIO where the Rele is connected to
const int rele_pin = 14; //D5
// GPIO where the Buttons is connected to
const int button_ok_pin = 12; //D6
const int button_mns_pin = 13; //D7
const int button_pls_pin = 15; //D8
// GPIO where the DS18B20 is connected to
const int oneWireBus = 2; //D4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Button button_pls(button_pls_pin, 300);
Button button_mns(button_mns_pin, 300);
Button button_ok(button_ok_pin, 300);
float temperature = 125;
int temp_on_addr = 0;
int temp_off_addr = 2;
float heater_on_temp;
float heater_off_temp;
bool heater_state = false;
int counter = 0;
//==================================================================================================
//--------------------------------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed!");
return;
}
Serial.println();
Serial.print("ESP IP Address: http://");
Serial.println(WiFi.localIP());
pinMode(rele_pin, OUTPUT);
digitalWrite(rele_pin, LOW);
pinMode(button_pls_pin, INPUT_PULLUP);
pinMode(button_mns_pin, INPUT_PULLUP);
pinMode(button_ok_pin, INPUT_PULLUP);
if (digitalRead (button_ok_pin)==LOW)
{
EEPROM.put (temp_on_addr,25); EEPROM.put (temp_off_addr,27);
heater_on_temp = 25; heater_off_temp = 27;
};
// Start the DS18B20 sensor
sensors.begin();
// Send web page to client
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);});
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
// GET threshold_input value on <ESP_IP>/get?threshold_input=<inputMessage>
if (request->hasParam(PARAM_INPUT_1)) {T_ON_Message = request->getParam(PARAM_INPUT_1)->value();
// GET enable_arm_input value on <ESP_IP>/get?enable_arm_input=<inputMessage2>
if (request->hasParam(PARAM_INPUT_2)) {T_OFF_Message = request->getParam(PARAM_INPUT_2)->value();}
}
request->send(200, "text/html", "HTTP GET request sent to your ESP.<br><a href=\"/\">Return to Home Page</a>");
heater_on_temp = T_ON_Message.toFloat ();
heater_off_temp = T_OFF_Message.toFloat();
});
server.onNotFound(notFound);
server.begin();
EEPROM.get (temp_on_addr, heater_on_temp);
EEPROM.get (temp_off_addr, heater_off_temp);
T_ON_Message = String(heater_on_temp);
T_OFF_Message = String(heater_off_temp);
//heater_off_temp = eeprom_read_word (temp_off_addr);
lcd.init();
lcd.backlight();
lcd.clear ();
temperature = sensors.getTempCByIndex(0);
lcd.print ("Current T:");
lcd.print(temperature, 1);
lcd.setCursor (1,1); lcd.print ("ON:"); lcd.print(heater_on_temp, 0);
lcd.setCursor (7,1); lcd.print (" OFF:"); lcd.print(heater_off_temp, 0);
}
//==================================================================================================
void loop() {
button_pls.scanState();
button_mns.scanState();
button_ok.scanState();
if (button_pls.push == true) {counter = counter + 1;};
if (button_mns.push == true) {counter = counter - 1;};
if (button_ok.push == true) {counter = 0;};
lcd.setCursor (0,1);
lcd.print (counter); lcd.print (" ");
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
sensors.requestTemperatures();
// Temperature in Celsius degrees
temperature = sensors.getTempCByIndex(0);
lcd.clear ();
lcd.print ("Current T: "); lcd.print (temperature);
//lcd.setCursor (1,1); lcd.print ("ON:"); lcd.print(heater_on_temp, 0);
//lcd.setCursor (7,1); lcd.print (" OFF:"); lcd.print(heater_off_temp, 0);
lcd.setCursor (0,1);
lcd.print (counter);
lastTemperature = String(temperature);
// Check if temperature is above threshold and if it needs to trigger output
if(temperature <= T_ON_Message.toFloat()){
heater_state = true;
heater_state_str = "ON";
digitalWrite(rele_pin, HIGH);
};
// Check if temperature is below threshold and if it needs to trigger output
if((temperature > T_OFF_Message.toFloat())) {
heater_state = false;
heater_state_str = "OFF";
digitalWrite(rele_pin, LOW);
}
if (heater_state==true) {lcd.setCursor (15,1); lcd.print ("h");} else {lcd.setCursor (7,1); lcd.print (" ");};
}
}