#include #include #include /////////////////////////////////////////////////////////////////// // Set your SSID & Pass for initial configuration #include "../include/configuration.h" // application configuration /////////////////////////////////////////////////////////////////// #include "webserver.h" DHT dht(DHT_PIN, DHT22); Timer procTimer; Timer displayTimer; bool state = false; // Sensors string values String StrT, StrRH, StrHI, StrCR, StrCF, StrTime; String StrVDD, StrADC; Timer httpcTimer; HttpClient thingSpeak; /* FTPServer ftp; */ void process(); void showValues(); void connectOk(); void connectFail(); void onDataSent(HttpClient& client, bool successful); void sendData(); void onNtpReceive(NtpClient& client, time_t timestamp); NtpClient ntpClient ("ntps1-0.cs.tu-berlin.de", 60, onNtpReceive); void init() { spiffs_mount(); // Mount file system, in order to work with files Serial.begin(SERIAL_BAUD_RATE); // 115200 by default Serial.systemDebugOutput(false); // Debug output to serial Serial.println("Wall Segment Clock"); ActiveConfig = loadConfig(); // Select control line pinMode(CONTROL_PIN, OUTPUT); digitalWrite(CONTROL_PIN, LOW); // Restart main screen output procTimer.restart(); displayTimer.stop(); //wait for sensor startup delay(1000); // DHT sensor start dht.begin(); // set timezone hourly difference to UTC SystemClock.setTimeZone(ActiveConfig.AddTZ); WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword); WifiStation.enable(true); WifiAccessPoint.enable(false); WifiStation.waitConnection(connectOk, 20, connectFail); // We recommend 20+ seconds for connection timeout at start // раз в минуту? procTimer.initializeMs(60000, process).start(); process(); } void showValues() { Serial.print("Date & Time: "); Serial.println(SystemClock.getSystemTimeString()); Serial.print("Temperature: "); Serial.print(StrT); Serial.println("*C"); Serial.print("Humidity: "); Serial.print(StrRH); Serial.println("%"); Serial.print("Heatindex: "); Serial.print(StrHI); Serial.println("*C"); Serial.print("Comfort: "); Serial.print(StrCR); Serial.print("% - "); Serial.println(StrCF); Serial.print("ADC value: "); Serial.println(StrADC); Serial.print("VDD value: "); Serial.println(StrVDD); Serial.println(""); } void process() { StrTime = SystemClock.getSystemTimeString(); float t = dht.readTemperature() + ActiveConfig.AddT; float h = dht.readHumidity() + ActiveConfig.AddRH; float hi = dht.getHeatIndex(); ComfortState cf; float cr = dht.getComfortRatio(cf); if (ActiveConfig.Trigger == eTT_Temperature) state = t < ActiveConfig.RangeMin || t > ActiveConfig.RangeMax; else if (ActiveConfig.Trigger == eTT_Humidity) state = h < ActiveConfig.RangeMin || h > ActiveConfig.RangeMax; digitalWrite(CONTROL_PIN, state); StrT = String(t, 1); StrRH = String(h, 1); StrHI = String(hi, 1); StrCR = String(cr, 0); switch(cf) { case Comfort_OK: StrCF = "OK"; break; case Comfort_TooHot: StrCF = "Too Hot"; break; case Comfort_TooCold: StrCF = "Too Cold"; break; case Comfort_TooDry: StrCF = "Too Dry"; break; case Comfort_TooHumid: StrCF = "Too Humid"; break; case Comfort_HotAndHumid: StrCF = "Hot And Humid"; break; case Comfort_HotAndDry: StrCF = "Hot And Dry"; break; case Comfort_ColdAndHumid: StrCF = "Cold And Humid"; break; case Comfort_ColdAndDry: StrCF = "Cold And Dry"; break; default: StrCF = "Unknown:"; break; } StrADC = String(system_adc_read()); StrVDD = String(system_get_vdd33()); if (!displayTimer.isStarted()) displayTimer.initializeMs(20000, showValues).start(); // обновление вывода -- раз в 20 сек. обновление данных -- раз в минуту. } /* void startFTP() { if (!fileExist("index.html")) fileSetContent("index.html", "

Please connect to FTP and upload files from folder 'web/build' (details in code)

"); // Start FTP server ftp.listen(21); ftp.addUser("user", "resu"); // FTP account // You can also use special FTP comand: "fsformat" for clearing file system (for example from TotalCMD) } */ void connectOk() { // debugf("connected"); WifiAccessPoint.enable(false); Serial.print("I'm connecteed. IP: "); Serial.println(WifiStation.getIP().toString()); // Start send data loop httpcTimer.initializeMs(60 * 1000, sendData).start(); // every 60 seconds startWebServer(); /* startFTP(); */ } /* * в случае неудачи подключения поднимаем точку доступа без авторизации */ void connectFail() { // debugf("connection FAILED"); WifiAccessPoint.config("MeteoConfig", "", AUTH_OPEN); WifiAccessPoint.enable(true); // Stop main screen output procTimer.stop(); displayTimer.stop(); Serial.println("WiFi MeteoConfig"); Serial.println(WifiAccessPoint.getIP()); startWebServer(); WifiStation.waitConnection(connectOk); // Wait connection } /* * Отправка данных на внешний сервер */ void onDataSent(HttpClient& client, bool successful) { if (successful) Serial.println("Success sent"); else Serial.println("Failed"); String response = client.getResponseString(); Serial.println("Server response: '" + response + "'"); if (response.length() > 0) { int intVal = response.toInt(); if (intVal == 0) Serial.println("Sensor value wasn't accepted. May be we need to wait a little?"); } } void sendData() { if (thingSpeak.isProcessing()) return; // We need to wait while request processing was completed thingSpeak.downloadString("http://api.thingspeak.com/update?key=26WYU9LJCBC3AE1X&field1=" + StrT + "&field2=" + StrRH + "&field3=" + StrHI, onDataSent); } /* * NTP */ void onNtpReceive(NtpClient& client, time_t timestamp) { SystemClock.setTime(timestamp, eTZ_UTC); Serial.println("*** Time synchronized! ***"); // Serial.println(SystemClock.getSystemTimeString()); }