application.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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, procRTimer;
  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. static int8_t status;
  103. if(dht.readTempAndHumidity(th))
  104. {
  105. status = 0;
  106. SensorT = th.temp;
  107. SensorH = th.humid;
  108. SensorHI = dht.getHeatIndex();
  109. SensorCR = dht.getComfortRatio(cf);
  110. switch(cf)
  111. {
  112. case Comfort_OK:
  113. StrCF = "OK";
  114. break;
  115. case Comfort_TooHot:
  116. StrCF = "Too Hot";
  117. break;
  118. case Comfort_TooCold:
  119. StrCF = "Too Cold";
  120. break;
  121. case Comfort_TooDry:
  122. StrCF = "Too Dry";
  123. break;
  124. case Comfort_TooHumid:
  125. StrCF = "Too Humid";
  126. break;
  127. case Comfort_HotAndHumid:
  128. StrCF = "Hot And Humid";
  129. break;
  130. case Comfort_HotAndDry:
  131. StrCF = "Hot And Dry";
  132. break;
  133. case Comfort_ColdAndHumid:
  134. StrCF = "Cold And Humid";
  135. break;
  136. case Comfort_ColdAndDry:
  137. StrCF = "Cold And Dry";
  138. break;
  139. default:
  140. StrCF = "Unknown";
  141. break;
  142. }
  143. }
  144. else
  145. {
  146. if (status < 6)
  147. {
  148. status ++;
  149. procTimer.
  150. }
  151. }
  152. }
  153. /*
  154. void startFTP()
  155. {
  156. // Start FTP server
  157. ftp.listen(21);
  158. ftp.addUser("user", "resu"); // FTP account
  159. // You can also use special FTP comand: "fsformat" for clearing file system (for example from TotalCMD)
  160. }
  161. */
  162. void connectOk()
  163. {
  164. WifiAccessPoint.enable(false);
  165. Serial.print("I'm connecteed. IP: ");
  166. Serial.println(WifiStation.getIP().toString());
  167. startWebServer();
  168. /* startFTP(); */
  169. }
  170. /*
  171. * â ñëó÷àå íåóäà÷è ïîäêëþ÷åíèÿ ïîäíèìàåì òî÷êó äîñòóïà áåç àâòîðèçàöèè
  172. */
  173. void connectFail()
  174. {
  175. WifiAccessPoint.config("MeteoConfig", "", AUTH_OPEN);
  176. WifiAccessPoint.enable(true);
  177. // Stop main screen output
  178. procTimer.stop();
  179. displayTimer.stop();
  180. Serial.println("WiFi MeteoConfig");
  181. Serial.println(WifiAccessPoint.getIP());
  182. startWebServer();
  183. WifiStation.waitConnection(connectOk); // Wait connection
  184. }
  185. /*
  186. * NTP Client
  187. */
  188. void onNtpReceive(NtpClient& client, time_t timestamp) {
  189. SystemClock.setTime(timestamp, eTZ_UTC);
  190. NTPLastUpdate = SystemClock.now();
  191. Serial.println("*** Time synchronized OK! ***"); // DEBUG
  192. }