application.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "max7219.h"
  9. #include "webserver.h"
  10. DHT dht(DHT_PIN, DHT22);
  11. Timer procTimer;
  12. Timer displayTimer;
  13. // Sensors values
  14. float SensorT, SensorH, SensorHI, SensorCR;
  15. String StrCF;
  16. // Time values
  17. time_t Time, NTPLastUpdate;
  18. /* FTPServer ftp; */
  19. void process();
  20. void connectOk();
  21. void connectFail();
  22. void onNtpReceive(NtpClient& client, time_t timestamp);
  23. void showTime();
  24. NtpClient ntpClient ("ntps1-0.cs.tu-berlin.de", 300, onNtpReceive);
  25. void init()
  26. {
  27. spiffs_mount(); // Mount file system, in order to work with files
  28. Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
  29. Serial.systemDebugOutput(false); // Debug output to serial
  30. Serial.println("Wall Segment Clock");
  31. ActiveConfig = loadConfig();
  32. // Select control line
  33. pinMode(CONTROL_PIN, OUTPUT);
  34. PinRes(CONTROL_PIN);
  35. //wait for sensor startup
  36. delay(1000);
  37. // DHT sensor start
  38. dht.begin();
  39. // 7-segment output
  40. MAX7219_Init();
  41. // set timezone hourly difference to UTC
  42. SystemClock.setTimeZone(ActiveConfig.AddTZ);
  43. WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword);
  44. WifiStation.enable(true);
  45. WifiAccessPoint.enable(false);
  46. WifiStation.waitConnection(connectOk, 20, connectFail); // We recommend 20+ seconds for connection timeout at start
  47. // ðàç â ìèíóòó?
  48. procTimer.initializeMs(60000, process).start();
  49. process();
  50. // îáíîâëåíèå ýêðàíà äâà ðàçà â ñåêóíäó
  51. displayTimer.initializeMs(500, showTime).start();
  52. }
  53. void showTime()
  54. {
  55. static int8_t oldHour, oldMinute;
  56. static time_t oldTime;
  57. DateTime dt;
  58. int16_t *year;
  59. Time = SystemClock.now();
  60. if (oldTime != Time)
  61. {
  62. oldTime = Time;
  63. dt.setTime(Time);
  64. /*
  65. * òåïåðü â dt ó íàñ ñëåäóþùåå:
  66. * int8_t Hour;
  67. * int8_t Minute;
  68. * int8_t Second;
  69. * int16_t Milliseconds;
  70. * int8_t Day;
  71. * int8_t DayofWeek; -- Sunday is day 0
  72. * int8_t Month; // Jan is month 0
  73. * int16_t Year; // Full Year numer
  74. */
  75. MAX7219_writeData(MAX7219_DIGIT2, SYM_Minus);
  76. if (oldMinute != dt.Minute)
  77. {
  78. oldMinute = dt.Minute;
  79. MAX7219_writeData(MAX7219_DIGIT3, dt.Minute/10);
  80. MAX7219_writeData(MAX7219_DIGIT4, dt.Minute%10);
  81. if (oldHour != dt.Hour)
  82. {
  83. oldHour = dt.Hour;
  84. MAX7219_writeData(MAX7219_DIGIT0, dt.Hour/10);
  85. MAX7219_writeData(MAX7219_DIGIT1, dt.Hour%10);
  86. }
  87. }
  88. }
  89. else // time the same, output blank for "hh mm"
  90. {
  91. MAX7219_writeData(MAX7219_DIGIT2, SYM_Minus);
  92. }
  93. }
  94. /*
  95. * ×èòàåì è äàííûå ñ DHT22, â ñëó÷àå íåóäà÷è -- äàííûå îñòàíóòüñÿ ñòàðìè.
  96. * ìåíÿ ýòî ïîëíîñòüþ óñòðàèâàåò.
  97. */
  98. void process()
  99. {
  100. TempAndHumidity th;
  101. ComfortState cf;
  102. if(dht.readTempAndHumidity(th))
  103. {
  104. SensorT = th.temp;
  105. SensorH = th.humid;
  106. SensorHI = dht.getHeatIndex();
  107. SensorCR = dht.getComfortRatio(cf);
  108. switch(cf)
  109. {
  110. case Comfort_OK:
  111. StrCF = "OK";
  112. break;
  113. case Comfort_TooHot:
  114. StrCF = "Too Hot";
  115. break;
  116. case Comfort_TooCold:
  117. StrCF = "Too Cold";
  118. break;
  119. case Comfort_TooDry:
  120. StrCF = "Too Dry";
  121. break;
  122. case Comfort_TooHumid:
  123. StrCF = "Too Humid";
  124. break;
  125. case Comfort_HotAndHumid:
  126. StrCF = "Hot And Humid";
  127. break;
  128. case Comfort_HotAndDry:
  129. StrCF = "Hot And Dry";
  130. break;
  131. case Comfort_ColdAndHumid:
  132. StrCF = "Cold And Humid";
  133. break;
  134. case Comfort_ColdAndDry:
  135. StrCF = "Cold And Dry";
  136. break;
  137. default:
  138. StrCF = "Unknown";
  139. break;
  140. }
  141. }
  142. }
  143. /*
  144. void startFTP()
  145. {
  146. // Start FTP server
  147. ftp.listen(21);
  148. ftp.addUser("user", "resu"); // FTP account
  149. // You can also use special FTP comand: "fsformat" for clearing file system (for example from TotalCMD)
  150. }
  151. */
  152. void connectOk()
  153. {
  154. WifiAccessPoint.enable(false);
  155. Serial.print("I'm connecteed. IP: ");
  156. Serial.println(WifiStation.getIP().toString());
  157. startWebServer();
  158. /* startFTP(); */
  159. }
  160. /*
  161. * â ñëó÷àå íåóäà÷è ïîäêëþ÷åíèÿ ïîäíèìàåì òî÷êó äîñòóïà áåç àâòîðèçàöèè
  162. */
  163. void connectFail()
  164. {
  165. WifiAccessPoint.config("MeteoConfig", "", AUTH_OPEN);
  166. WifiAccessPoint.enable(true);
  167. // Stop main screen output
  168. procTimer.stop();
  169. displayTimer.stop();
  170. Serial.println("WiFi MeteoConfig");
  171. Serial.println(WifiAccessPoint.getIP());
  172. startWebServer();
  173. WifiStation.waitConnection(connectOk); // Wait connection
  174. }
  175. /*
  176. * NTP Client
  177. */
  178. void onNtpReceive(NtpClient& client, time_t timestamp) {
  179. SystemClock.setTime(timestamp, eTZ_UTC);
  180. NTPLastUpdate = SystemClock.now();
  181. Serial.println("*** Time synchronized OK! ***"); // DEBUG
  182. }