Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define INIT_ADDR 255
- #define INIT_KEY 36
- #include <NTPClient.h>
- #include <NTP.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <EEPROM.h>
- #include <WiFiUdp.h>
- const char* ssid = "";
- const char* password = "";
- IPAddress ip(10,250,32,133);
- IPAddress gateway(10,250,32,1);
- IPAddress subnet(255,0,0,0);
- WiFiServer server(80);
- volatile int counter = 0;
- volatile int DailyCount = 0;
- int InitDay = 0;
- int FirstKey =55;
- const char* CurrDate = "";
- const char* CurrTime = "";
- const long utcOffsetInSeconds = 25200;
- WiFiUDP ntpUDP;
- WiFiUDP wifiUdp;
- NTPClient timeClient(ntpUDP, "88.147.254.235", utcOffsetInSeconds);
- NTP ntp(wifiUdp);
- void setup()
- {
- pinMode (D3, INPUT_PULLUP);
- Serial.begin(115200);
- EEPROM.begin(512);
- EEPROM.get(INIT_ADDR,FirstKey);
- if (FirstKey != INIT_KEY)
- {
- EEPROM.put(INIT_ADDR, INIT_KEY);
- EEPROM.put(0, counter);
- EEPROM.put(20, counter);
- EEPROM.commit();
- }
- EEPROM.get(0, counter);
- EEPROM.get(20, InitDay);
- EEPROM.end();
- delay(1000);
- Serial.println();
- Serial.printf("Connecting to %s ", ssid);
- WiFi.begin(ssid, password);
- WiFi.config(ip, gateway, subnet);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println(" connected");
- server.begin();
- Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
- attachInterrupt(D3, CountImpulse, RISING);
- timeClient.begin();
- ntp.begin("88.147.254.235");
- }
- String OutPage()
- {
- ntp.update();
- CurrDate= ntp.formattedTime("%d.%m.%Y");
- String htmlPage;
- htmlPage.reserve(1024);
- htmlPage = F("HTTP/1.1 200 OK\r\n"
- "Content-Type: text/html;charset=utf-8\r\n"
- "Connection: close\r\n"
- "Refresh: 10\r\n"
- "\r\n"
- "<!DOCTYPE HTML>"
- "<html>"
- "<h1>Дата : ");
- htmlPage += CurrDate;
- htmlPage += F("</h1>"
- "\r\n");
- htmlPage += F("<h1> Показания на начало дня: ");
- htmlPage += InitDay ;
- htmlPage += F("</h>"
- "\r\n");
- htmlPage += F("<h1> Текущее показание: ");
- htmlPage += counter ;
- htmlPage += F( "</h>"
- "\r\n");
- htmlPage += F("<h1> Количество с начала дня: ");
- htmlPage += DailyCount ;
- htmlPage += F("</h><html>"
- "\r\n");
- return htmlPage;
- }
- void ResetDay()
- {
- timeClient.update();
- if (timeClient.getHours()==0 and timeClient.getMinutes()==0 and timeClient.getSeconds()==0)
- {
- system_restart();
- }
- }
- IRAM_ATTR void CountImpulse()
- {
- counter++;
- DailyCount=counter-InitDay;
- EEPROM.begin(512);
- EEPROM.put(0, counter);
- EEPROM.put(20, counter);
- EEPROM.commit();
- EEPROM.end();
- }
- void loop()
- {
- ResetDay();
- timeClient.update();
- WiFiClient client = server.available();
- if (client)
- {
- Serial.println("\n[Client connected at ]");
- Serial.print(timeClient.getHours());
- Serial.print(":");
- Serial.print(timeClient.getMinutes());
- Serial.print("\n");
- while (client.connected())
- {
- if (client.available())
- {
- String line = client.readStringUntil('\r');
- Serial.print(line);
- if (line.length() == 1 && line[0] == '\n')
- {
- client.println(OutPage());
- break;
- }
- }
- }
- while (client.available())
- {
- client.read();
- }
- client.stop();
- Serial.println("[Client disconnected]");
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement