#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>
#include "time.h"
#define WIFI_SSID "163"
#define WIFI_PASSWORD "23vfz24rd163"
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
const int PumpPin = 2;
const int ButtonPin=3;
const int HumidityPin=13;
bool PumpState=0;
bool PupmManualStart=0;
int humidity=0;
// Переменные для хранения параметров
int workTime_Set_Value = 10; // время работы в секундах
int pauseTime_Set_Value = 5; // время паузы в секундах
int Humidity_Set_Value = 50; // влажность в процентах
// NTP серверы для синхронизации времени
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600 * 3; // Смещение времени (например, +3 часа для Москвы)
const int daylightOffset_sec = 0; // Смещение летнего времени
struct tm timeinfo;
WebServer server(80);
void ioState() {
humidity=analogRead(HumidityPin);
digitalWrite(PumpPin,PumpState);
PupmManualStart=digitalRead(ButtonPin);
}
void sendHtml() {
String response = R"(
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<style>
body { font-family: Arial; text-align: center; margin-top: 50px; }
input { padding: 10px; margin: 5px; width: 200px; font-size: 16px; }
button { padding: 10px 20px; margin: 10px; font-size: 16px; cursor: pointer; }
.container { max-width: 400px; margin: 0 auto; padding: 20px; border: 1px solid #ccc; border-radius: 10px; }
.status { margin: 20px; padding: 10px; border-radius: 5px; }
.working { background-color: #4CAF50; color: white; }
.paused { background-color: #f44336; color: white; }
.disabled { background-color: #9e9e9e; color: white; }
</style>
</head>
<body>
<div class="container">
<h1>Управление системой</h1>
<form action="/update" method="POST">
<label>Время работы (сек):</label><br>
<input type="number" name="work" value="%WORK%" min="1" max="3600"><br>
<label>Время паузы (сек):</label><br>
<input type="number" name="pause" value="%PAUSE%" min="1" max="3600"><br>
<label>Влажность (%):</label><br>
<input type="number" name="humidity" value="%HUMIDITY%" min="0" max="100"><br>
<input type="submit" value="Сохранить параметры">
</form>
<div id="status" class="status %STATUS_CLASS%">
<h3>Текущее состояние: %STATE%</h3>
<p>Время работы: %WORK% сек</p>
<p>Время паузы: %PAUSE% сек</p>
<p>Влажность: %HUMIDITY% %%</p>
<p>Реле: %RELAY%</p>
</div>
<form action="/toggle" method="POST">
<button type="submit">%TOGGLE_TEXT%</button>
</form>
<br>
<a href="/">Обновить страницу</a>
</div>
</body>
</html>
)";
server.send(200, "text/html", response);
}
void setup() {
//настройка выходов, входов
pinMode(PumpPin, OUTPUT);
digitalWrite(PumpPin,LOW);
pinMode(ButtonPin, INPUT_PULLUP);
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Serial.begin(115200);
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());
server.on("/", sendHtml);
server.begin();
Serial.println("HTTP server started");
// Инициализация и получение времени
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
void loop() {
if(!getLocalTime(&timeinfo)){
Serial.println("Ошибка получения времени");
return;
}
server.handleClient();
ioState();
// put your main code here, to run repeatedly:
delay(100); // this speeds up the simulation
}