#include #include #include /////////////////////////////////////////////////////////////////// // Set your SSID & Pass for initial configuration #include "../include/configuration.h" // application configuration /////////////////////////////////////////////////////////////////// #include "max7219.h" #include "webserver.h" DHT dht(DHT_PIN, DHT22); Timer procTimer; //Timer serialTimer; Timer displayTimer; bool state = false; // Sensors string values String StrT, StrRH, StrHI, StrCR, StrCF; String oldStrTime, StrTime; String StrVDD, StrADC; // date and time uint16_t Year, Month, Day, Hour, Minute, Second; 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); void showTime(); 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(); // serialTimer.stop(); //wait for sensor startup delay(1000); // DHT sensor start dht.begin(); // 7-segment output MAX7219_Init(); // 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(); // обновление экрана два раза в секунду displayTimer.initializeMs(500, showTime).start(); } void showTime() { static uint16_t oldHour, oldMinute; StrTime = SystemClock.getSystemTimeString(); if (oldStrTime != StrTime) { Vector datetime; int idx = splitString(StrTime, ' ' , datetime); if (idx == 2) { // date Vector date; int di = splitString(datetime[0], '-', date); if(di == 3) { Year = date[0].toInt(); Month = date[1].toInt(); Day = date[2].toInt(); } // time Vector time; int ti = splitString(datetime[1], ':', time); if(ti == 3) { Hour = time[0].toInt(); Minute = time[1].toInt(); Second = time[2].toInt(); } } oldStrTime = StrTime; MAX7219_writeData(MAX7219_DIGIT2, SYM_Minus); if (oldMinute != Minute) { oldMinute = Minute; MAX7219_writeData(MAX7219_DIGIT3, Minute/10); MAX7219_writeData(MAX7219_DIGIT4, Minute%10); if (oldHour != Hour) { oldHour = Hour; MAX7219_writeData(MAX7219_DIGIT0, Hour/10); MAX7219_writeData(MAX7219_DIGIT1, Hour%10); } } } else // time the same, output blank for "hh mm" { MAX7219_writeData(MAX7219_DIGIT2, SYM_Minus); } } void process() { 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, 0); StrRH = String(h, 0); StrHI = String(hi, 0); 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()); } /* void startFTP() { // 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() { // We need to wait while request processing was completed if (thingSpeak.isProcessing()) return; 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()); }