123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- #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 "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", "<h3>Please connect to FTP and upload files from folder 'web/build' (details in code)</h3>");
- // 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());
- }
|