application.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <user_config.h>
  2. #include <SmingCore/SmingCore.h>
  3. #include <Libraries/DHT/DHT.h>
  4. ///////////////////////////////////////////////////////////////////
  5. // Set your SSID & Pass for initial configuration
  6. #include "../include/configuration.h" // application configuration
  7. ///////////////////////////////////////////////////////////////////
  8. #include "webserver.h"
  9. #include "max7219.h"
  10. DHT dht(DHT_PIN, DHT22);
  11. Timer procTimer;
  12. //Timer serialTimer;
  13. Timer displayTimer;
  14. bool state = false;
  15. // Sensors string values
  16. String StrT, StrRH, StrHI, StrCR, StrCF, StrTime;
  17. String StrVDD, StrADC;
  18. // date and time
  19. uint16_t Year, Month, Day, Hour, Minute, Second;
  20. Timer httpcTimer;
  21. HttpClient thingSpeak;
  22. /* FTPServer ftp; */
  23. void process(void);
  24. //void showValues(void);
  25. void connectOk(void);
  26. void connectFail(void);
  27. void onDataSent(HttpClient& client, bool successful);
  28. void sendData(void);
  29. void onNtpReceive(NtpClient& client, time_t timestamp);
  30. void showTime(void);
  31. NtpClient ntpClient ("ntps1-0.cs.tu-berlin.de", 60, onNtpReceive);
  32. void init()
  33. {
  34. spiffs_mount(); // Mount file system, in order to work with files
  35. Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
  36. Serial.systemDebugOutput(false); // Debug output to serial
  37. Serial.println("Wall Segment Clock");
  38. ActiveConfig = loadConfig();
  39. // Select control line
  40. pinMode(CONTROL_PIN, OUTPUT);
  41. digitalWrite(CONTROL_PIN, LOW);
  42. // Restart main screen output
  43. // procTimer.restart();
  44. // serialTimer.stop();
  45. //wait for sensor startup
  46. delay(1000);
  47. // DHT sensor start
  48. dht.begin();
  49. // 7-segment output
  50. MAX7219_Init();
  51. // set timezone hourly difference to UTC
  52. SystemClock.setTimeZone(ActiveConfig.AddTZ);
  53. WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword);
  54. WifiStation.enable(true);
  55. WifiAccessPoint.enable(false);
  56. WifiStation.waitConnection(connectOk, 20, connectFail); // We recommend 20+ seconds for connection timeout at start
  57. // ðàç â ìèíóòó?
  58. procTimer.initializeMs(60000, process).start();
  59. process();
  60. // îáíîâëåíèå ýêðàíà äâà ðàçà â ñåêóíäó
  61. displayTimer.initializeMs(500, showTime).start();
  62. }
  63. void showTime(void)
  64. {
  65. static String oldStrTime;
  66. static uint16_t oldHour, oldMinute;
  67. StrTime = SystemClock.getSystemTimeString();
  68. if (oldStrTime != StrTime)
  69. {
  70. Vector<String> datetime;
  71. int idx = splitString(StrTime, ' ' , datetime);
  72. if (idx == 2)
  73. {
  74. // date
  75. Vector<String> date;
  76. int di = splitString(datetime[0], '-', date);
  77. if(di == 3)
  78. {
  79. Year = date[0].toInt();
  80. Month = date[1].toInt();
  81. Day = date[2].toInt();
  82. }
  83. // time
  84. Vector<String> time;
  85. int ti = splitString(datetime[1], ':', time);
  86. if(ti == 3)
  87. {
  88. Hour = time[0].toInt();
  89. Minute = time[1].toInt();
  90. Second = time[2].toInt();
  91. }
  92. }
  93. oldStrTime = StrTime;
  94. MAX7219_writeData(MAX7219_DIGIT2, SYM_Minus);
  95. if (oldMinute != Minute)
  96. {
  97. oldMinute = Minute;
  98. MAX7219_writeData(MAX7219_DIGIT3, Minute/10);
  99. MAX7219_writeData(MAX7219_DIGIT4, Minute%10);
  100. if (oldHour != Hour)
  101. {
  102. oldHour = Hour;
  103. MAX7219_writeData(MAX7219_DIGIT0, Hour/10);
  104. MAX7219_writeData(MAX7219_DIGIT1, Hour%10);
  105. }
  106. }
  107. }
  108. else // time the same, output blank for "hh mm"
  109. {
  110. MAX7219_writeData(MAX7219_DIGIT2, SYM_Minus);
  111. }
  112. }
  113. /*
  114. void showValues(void)
  115. {
  116. Serial.print("Date & Time: ");
  117. Serial.println(SystemClock.getSystemTimeString());
  118. Serial.print("Temperature: ");
  119. Serial.print(StrT);
  120. Serial.println("*C");
  121. Serial.print("Humidity: ");
  122. Serial.print(StrRH);
  123. Serial.println("%");
  124. Serial.print("Heatindex: ");
  125. Serial.print(StrHI);
  126. Serial.println("*C");
  127. Serial.print("Comfort: ");
  128. Serial.print(StrCR);
  129. Serial.print("% - ");
  130. Serial.println(StrCF);
  131. Serial.print("ADC value: ");
  132. Serial.println(StrADC);
  133. Serial.print("VDD value: ");
  134. Serial.println(StrVDD);
  135. Serial.println("");
  136. }
  137. */
  138. void process()
  139. {
  140. float t = dht.readTemperature() + ActiveConfig.AddT;
  141. float h = dht.readHumidity() + ActiveConfig.AddRH;
  142. float hi = dht.getHeatIndex();
  143. ComfortState cf;
  144. float cr = dht.getComfortRatio(cf);
  145. if (ActiveConfig.Trigger == eTT_Temperature)
  146. state = t < ActiveConfig.RangeMin || t > ActiveConfig.RangeMax;
  147. else if (ActiveConfig.Trigger == eTT_Humidity)
  148. state = h < ActiveConfig.RangeMin || h > ActiveConfig.RangeMax;
  149. digitalWrite(CONTROL_PIN, state);
  150. StrT = String(t, 0);
  151. StrRH = String(h, 0);
  152. StrHI = String(hi, 0);
  153. StrCR = String(cr, 0);
  154. switch(cf)
  155. {
  156. case Comfort_OK:
  157. StrCF = "OK";
  158. break;
  159. case Comfort_TooHot:
  160. StrCF = "Too Hot";
  161. break;
  162. case Comfort_TooCold:
  163. StrCF = "Too Cold";
  164. break;
  165. case Comfort_TooDry:
  166. StrCF = "Too Dry";
  167. break;
  168. case Comfort_TooHumid:
  169. StrCF = "Too Humid";
  170. break;
  171. case Comfort_HotAndHumid:
  172. StrCF = "Hot And Humid";
  173. break;
  174. case Comfort_HotAndDry:
  175. StrCF = "Hot And Dry";
  176. break;
  177. case Comfort_ColdAndHumid:
  178. StrCF = "Cold And Humid";
  179. break;
  180. case Comfort_ColdAndDry:
  181. StrCF = "Cold And Dry";
  182. break;
  183. default:
  184. StrCF = "Unknown:";
  185. break;
  186. }
  187. StrADC = String(system_adc_read());
  188. StrVDD = String(system_get_vdd33());
  189. /*
  190. if (!serialTimer.isStarted())
  191. serialTimer.initializeMs(20000, showValues).start();
  192. // îáíîâëåíèå âûâîäà -- ðàç â 20 ñåê. îáíîâëåíèå äàííûõ -- ðàç â ìèíóòó.
  193. */
  194. }
  195. /*
  196. void startFTP()
  197. {
  198. if (!fileExist("index.html"))
  199. fileSetContent("index.html", "<h3>Please connect to FTP and upload files from folder 'web/build' (details in code)</h3>");
  200. // Start FTP server
  201. ftp.listen(21);
  202. ftp.addUser("user", "resu"); // FTP account
  203. // You can also use special FTP comand: "fsformat" for clearing file system (for example from TotalCMD)
  204. }
  205. */
  206. void connectOk()
  207. {
  208. // debugf("connected");
  209. WifiAccessPoint.enable(false);
  210. Serial.print("I'm connecteed. IP: ");
  211. Serial.println(WifiStation.getIP().toString());
  212. // Start send data loop
  213. httpcTimer.initializeMs(60 * 1000, sendData).start(); // every 60 seconds
  214. startWebServer();
  215. /* startFTP(); */
  216. }
  217. /*
  218. * â ñëó÷àå íåóäà÷è ïîäêëþ÷åíèÿ ïîäíèìàåì òî÷êó äîñòóïà áåç àâòîðèçàöèè
  219. */
  220. void connectFail()
  221. {
  222. // debugf("connection FAILED");
  223. WifiAccessPoint.config("MeteoConfig", "", AUTH_OPEN);
  224. WifiAccessPoint.enable(true);
  225. // Stop main screen output
  226. procTimer.stop();
  227. displayTimer.stop();
  228. Serial.println("WiFi MeteoConfig");
  229. Serial.println(WifiAccessPoint.getIP());
  230. startWebServer();
  231. WifiStation.waitConnection(connectOk); // Wait connection
  232. }
  233. /*
  234. * Îòïðàâêà äàííûõ íà âíåøíèé ñåðâåð
  235. */
  236. void onDataSent(HttpClient& client, bool successful)
  237. {
  238. if (successful)
  239. Serial.println("Success sent");
  240. else
  241. Serial.println("Failed");
  242. String response = client.getResponseString();
  243. Serial.println("Server response: '" + response + "'");
  244. if (response.length() > 0)
  245. {
  246. int intVal = response.toInt();
  247. if (intVal == 0)
  248. Serial.println("Sensor value wasn't accepted. May be we need to wait a little?");
  249. }
  250. }
  251. void sendData()
  252. {
  253. if (thingSpeak.isProcessing()) return; // We need to wait while request processing was completed
  254. thingSpeak.downloadString("http://api.thingspeak.com/update?key=26WYU9LJCBC3AE1X&field1=" + StrT + "&field2=" + StrRH + "&field3=" + StrHI, onDataSent);
  255. }
  256. /*
  257. * NTP
  258. */
  259. void onNtpReceive(NtpClient& client, time_t timestamp) {
  260. SystemClock.setTime(timestamp, eTZ_UTC);
  261. Serial.println("*** Time synchronized! ***");
  262. // Serial.println(SystemClock.getSystemTimeString());
  263. }