123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #include <user_config.h>
- #include <SmingCore/SmingCore.h>
- #include <Libraries/DHT/DHT.h>
- ///////////////////////////////////////////////////////////////////
- // 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, procRTimer;
- Timer displayTimer;
- // Sensors values
- float SensorT, SensorH, SensorHI, SensorCR;
- String StrCF;
- // Time values
- time_t Time, NTPLastUpdate;
- void process();
- void connectOk();
- void connectFail();
- void showTime();
- // NTP Client
- void onNtpReceive(NtpClient& client, time_t timestamp);
- NtpClient ntpClient ("ntps1-0.cs.tu-berlin.de", 300, 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);
- PinRes(CONTROL_PIN);
- //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 int8_t oldHour, oldMinute;
- static time_t oldTime;
- DateTime dt;
- int16_t *year;
- Time = SystemClock.now();
- if (oldTime != Time)
- {
- oldTime = Time;
- dt.setTime(Time);
- /*
- * òåïåðü â dt ó íàñ ñëåäóþùåå:
- * int8_t Hour;
- * int8_t Minute;
- * int8_t Second;
- * int16_t Milliseconds;
- * int8_t Day;
- * int8_t DayofWeek; -- Sunday is day 0
- * int8_t Month; // Jan is month 0
- * int16_t Year; // Full Year numer
- */
- MAX7219_writeData(MAX7219_DIGIT2, SYM_Minus);
- if (oldMinute != dt.Minute)
- {
- oldMinute = dt.Minute;
- MAX7219_writeData(MAX7219_DIGIT3, dt.Minute/10);
- MAX7219_writeData(MAX7219_DIGIT4, dt.Minute%10);
- if (oldHour != dt.Hour)
- {
- oldHour = dt.Hour;
- MAX7219_writeData(MAX7219_DIGIT0, dt.Hour/10);
- MAX7219_writeData(MAX7219_DIGIT1, dt.Hour%10);
- }
- }
- }
- else // time the same, output blank for "hh mm"
- {
- MAX7219_writeData(MAX7219_DIGIT2, SYM_Minus);
- }
- }
- /*
- * ×èòàåì è äàííûå ñ DHT22, â ñëó÷àå íåóäà÷è -- äàííûå îñòàíóòüñÿ ñòàðûìè.
- * ìåíÿ ýòî ïîëíîñòüþ óñòðàèâàåò.
- */
- void process()
- {
- TempAndHumidity th;
- ComfortState cf;
- static int8_t status;
- if(dht.readTempAndHumidity(th))
- {
- status = 0;
- SensorT = th.temp;
- SensorH = th.humid;
- SensorHI = dht.getHeatIndex();
- SensorCR = dht.getComfortRatio(cf);
- 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;
- }
- }
- else
- {
- /*
- *  ñëó÷àå, åñëè îò äàò÷èêà íè÷åãî íå ïîëó÷èëè, çàïóñòèì ïîâòîðíûé îïðîñ ÷åðåç
- * 10 ñåêóíä, íî íå áîëåå 5 ðàç ïîäðÿä.
- */
- if (status < 6)
- {
- status ++;
- procRTimer.initializeMs(10000, process).startOnce();
- }
- }
- }
- void connectOk()
- {
- WifiAccessPoint.enable(false);
- Serial.print("I'm connecteed. IP: ");
- Serial.println(WifiStation.getIP().toString());
- startWebServer();
- }
- /*
- * â ñëó÷àå íåóäà÷è ïîäêëþ÷åíèÿ ïîäíèìàåì òî÷êó äîñòóïà áåç àâòîðèçàöèè
- */
- void connectFail()
- {
- 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
- }
- /*
- * NTP Client
- */
- void onNtpReceive(NtpClient& client, time_t timestamp) {
- SystemClock.setTime(timestamp, eTZ_UTC);
- NTPLastUpdate = SystemClock.now();
- Serial.println("*** Time synchronized OK! ***"); // DEBUG
- }
|