123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- #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;
- //Timer serialTimer;
- Timer displayTimer;
- bool state = false;
- // Sensors values
- float SensorT, SensorH, SensorHI, SensorCR;
- String StrCF;
- // Time values
- String oldStrTime, StrTime, StrTimeLastUpdate;
- //String StrVDD, StrADC;
- // date and time
- uint16_t Year, Month, Day, Hour, Minute, Second;
- /* FTPServer ftp; */
- void process();
- void connectOk();
- void connectFail();
- void onNtpReceive(NtpClient& client, time_t timestamp);
- void showTime();
- 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 uint16_t oldHour, oldMinute;
- StrTime = SystemClock.getSystemTimeString();
- if (oldStrTime != StrTime)
- {
- Vector<String> datetime;
- int idx = splitString(StrTime, ' ' , datetime);
- if (idx == 2)
- {
- // date
- Vector<String> date;
- int di = splitString(datetime[0], '-', date);
- if(di == 3)
- {
- Year = date[0].toInt();
- Month = date[1].toInt();
- Day = date[2].toInt();
- }
- // time
- Vector<String> 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);
- }
- }
- /*
- * ×èòàåì è äàííûå ñ DHT22, â ñëó÷àå íåóäà÷è -- äàííûå îñòàíóòüñÿ ñòàðìè.
- * ìåíÿ ýòî ïîëíîñòüþ óñòðàèâàåò.
- */
- void process()
- {
- PinSet(CONTROL_PIN); // DEBUG
- TempAndHumidity th;
- ComfortState cf;
- if(dht.readTempAndHumidity(th))
- {
- 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;
- }
- }
- PinRes(CONTROL_PIN); // DEBUG
- }
- /*
- 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()
- {
- WifiAccessPoint.enable(false);
- Serial.print("I'm connecteed. IP: ");
- Serial.println(WifiStation.getIP().toString());
- startWebServer();
- /* startFTP(); */
- }
- /*
- * â ñëó÷àå íåóäà÷è ïîäêëþ÷åíèÿ ïîäíèìàåì òî÷êó äîñòóïà áåç àâòîðèçàöèè
- */
- 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);
- StrTimeLastUpdate = SystemClock.getSystemTimeString();
- Serial.println("*** Time synchronized OK! ***"); // DEBUG
- Serial.println(SystemClock.now()); // DEBUG
- }
|