application.cpp 5.0 KB

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