#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
AsyncWebServer server(80);
// REPLACE WITH YOUR NETWORK CREDENTIALS
const char* ssid = "ESP32-Access-Point";
const char* password = "123456789";
const char* PARAM_STRING = "inputString";
String realTime = "";
//HTML код страницы
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML>
<html>
<head>
<title>ESP32</title>
<style>
#test_id {
width: 0px;
}
#subm_id {
margin: 0 auto;
display: block;
min-width: 100px;
font-family: inherit;
appearance: none;
border: 0;
border-radius: 5px;
background: #4676d7;
color: #fff;
padding: 8px 16px;
font-size: 1rem;
cursor: pointer;
}
</style>
<h1>
<p align="center"> Time Synchronization</p>
</h1>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
var timestring = "";
function submitMessage() {
//alert("Saved value to ESP SPIFFS");
setTimeout(function () { document.location.reload(false); }, 500);
}
function zero_first_format(value) {
if (value < 10) {
value = '0' + value;
}
return value;
}
function date_time() {
var current_datetime = new Date();
var day = zero_first_format(current_datetime.getDate());
var month = zero_first_format(current_datetime.getMonth() + 1);
var year = current_datetime.getFullYear();
var hours = zero_first_format(current_datetime.getHours());
var minutes = zero_first_format(current_datetime.getMinutes());
var seconds = zero_first_format(current_datetime.getSeconds());
timestring = hours + ":" + minutes + ":" + seconds + " " + day + "." + month + "." + year;
return timestring;
}
function viewtime() {
document.getElementById('control_id').innerHTML = date_time();
document.getElementById('test_id').value = date_time();
}
setInterval(viewtime, 1000);
</script>
</head>
<body>
<h2>
<p id="control_id" align="center">now time</p>
</h2>
<form action="/get" target="hidden-form">
<input type="hidden" id="test_id" name="inputString">
<input type="submit" id="subm_id" value="Set" onclick="submitMessage()">
</form><br>
<iframe style="display:none" name="hidden-form"></iframe>
</body>
</html>)rawliteral";
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
// Replaces placeholder with stored values
String processor(const String& var) {
if (var == "inputString") {
return realTime;
}
return String();
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.begin(ssid, password);
Serial.println();
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Send web page with input fields to client
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/html", index_html, processor);
});
// Send a GET request to <ESP_IP>/get?inputString=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest * request) {
// GET inputString value on <ESP_IP>/get?inputString=<inputMessage>
if (request->hasParam(PARAM_STRING)) {
realTime = request->getParam(PARAM_STRING)->value();
}
else {
realTime = "";
}
Serial.println("Время клиента: " + realTime);
request->send(200, "text/text", realTime);
});
server.onNotFound(notFound);
server.begin();
}
void loop() {
// Время в виде 'hh:mm:ss DD.MM.YYYY' сохраняется в переменной realTime типа String
while (1) {};
}