Advertisement
SergoM73

Untitled

Apr 20th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | Fixit | 0 0
  1. #define INIT_ADDR 255  
  2. #define INIT_KEY 36    
  3.  
  4. #include <NTPClient.h>
  5. #include <NTP.h>
  6. #include <ESP8266WiFi.h>
  7. #include <ESP8266WebServer.h>
  8. #include <EEPROM.h>
  9. #include <WiFiUdp.h>
  10.  
  11. const char* ssid = "";        
  12. const char* password = "";
  13.  
  14. IPAddress ip(10,250,32,133);          
  15. IPAddress gateway(10,250,32,1);        
  16. IPAddress subnet(255,0,0,0);      
  17. WiFiServer server(80);
  18.  
  19. volatile int counter = 0;
  20. volatile int DailyCount = 0;
  21. int InitDay = 0;
  22. int FirstKey =55;
  23. const char* CurrDate = "";
  24. const char* CurrTime = "";
  25. const long utcOffsetInSeconds = 25200;
  26.  
  27. WiFiUDP ntpUDP;
  28. WiFiUDP wifiUdp;
  29. NTPClient timeClient(ntpUDP, "88.147.254.235", utcOffsetInSeconds);
  30. NTP ntp(wifiUdp);
  31.  
  32. void setup()
  33. {
  34. pinMode (D3, INPUT_PULLUP);
  35. Serial.begin(115200);
  36.     EEPROM.begin(512);
  37.     EEPROM.get(INIT_ADDR,FirstKey);
  38.     if (FirstKey != INIT_KEY)
  39.     {
  40.       EEPROM.put(INIT_ADDR, INIT_KEY);    
  41.       EEPROM.put(0, counter);
  42.       EEPROM.put(20, counter);
  43.       EEPROM.commit();
  44.     }
  45. EEPROM.get(0, counter);
  46. EEPROM.get(20, InitDay);
  47. EEPROM.end();
  48. delay(1000);
  49. Serial.println();
  50. Serial.printf("Connecting to %s ", ssid);
  51. WiFi.begin(ssid, password);
  52. WiFi.config(ip, gateway, subnet);
  53.  
  54.     while (WiFi.status() != WL_CONNECTED)
  55.  {
  56.    delay(500);
  57.    Serial.print(".");
  58.  }
  59. Serial.println(" connected");
  60. server.begin();
  61. Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
  62. attachInterrupt(D3, CountImpulse, RISING);
  63. timeClient.begin();
  64. ntp.begin("88.147.254.235");
  65. }
  66.  
  67. String OutPage()
  68. {
  69.   ntp.update();
  70.   CurrDate= ntp.formattedTime("%d.%m.%Y");
  71.   String htmlPage;
  72.   htmlPage.reserve(1024);              
  73.   htmlPage = F("HTTP/1.1 200 OK\r\n"
  74.                "Content-Type: text/html;charset=utf-8\r\n"
  75.                "Connection: close\r\n"  
  76.                "Refresh: 10\r\n"        
  77.                "\r\n"
  78.                "<!DOCTYPE HTML>"
  79.                "<html>"
  80.                "<h1>Дата :  ");
  81.   htmlPage += CurrDate;
  82.   htmlPage += F("</h1>"
  83.                 "\r\n");
  84.   htmlPage += F("<h1> Показания на начало дня: ");
  85.   htmlPage += InitDay ;
  86.   htmlPage += F("</h>"
  87.                 "\r\n");
  88.   htmlPage += F("<h1> Текущее показание: ");
  89.   htmlPage += counter ;
  90.   htmlPage += F( "</h>"
  91.                 "\r\n");
  92.   htmlPage += F("<h1> Количество с начала дня: ");
  93.   htmlPage += DailyCount ;
  94.   htmlPage += F("</h><html>"
  95.                 "\r\n");
  96.   return htmlPage;
  97. }
  98. void ResetDay()
  99. {
  100.   timeClient.update();
  101.   if (timeClient.getHours()==0 and timeClient.getMinutes()==0 and timeClient.getSeconds()==0)
  102.   {
  103.       system_restart();
  104.   }
  105. }
  106.  
  107. IRAM_ATTR void CountImpulse()
  108. {
  109.    counter++;  
  110.    DailyCount=counter-InitDay;
  111.    EEPROM.begin(512);
  112.    EEPROM.put(0, counter);
  113.    EEPROM.put(20, counter);
  114.    EEPROM.commit();
  115.    EEPROM.end();
  116. }
  117.  
  118. void loop()
  119. {
  120. ResetDay();
  121. timeClient.update();  
  122.   WiFiClient client = server.available();
  123.      if (client)
  124.        {
  125.         Serial.println("\n[Client connected at ]");
  126.         Serial.print(timeClient.getHours());
  127.         Serial.print(":");
  128.         Serial.print(timeClient.getMinutes());
  129.         Serial.print("\n");
  130.  
  131.          while (client.connected())
  132.           {
  133.             if (client.available())
  134.               {
  135.                String line = client.readStringUntil('\r');
  136.                Serial.print(line);
  137.                 if (line.length() == 1 && line[0] == '\n')
  138.                  {
  139.                    client.println(OutPage());
  140.                    break;
  141.                  }
  142.               }
  143.           }
  144.          while (client.available())
  145.           {
  146.             client.read();
  147.           }
  148.         client.stop();
  149.         Serial.println("[Client disconnected]");
  150.       }
  151.       delay(1000);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement