application.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. void process();
  19. void connectOk();
  20. void connectFail();
  21. void showTime();
  22. // UDP server
  23. void onReceive(UdpConnection& connection, char *data, int size, IPAddress remoteIP, uint16_t remotePort); // Declaration
  24. const uint16_t EchoPort = 777;
  25. UdpConnection udp(onReceive);
  26. // NTP Client
  27. void onNtpReceive(NtpClient& client, time_t timestamp);
  28. NtpClient ntpClient ("ntps1-0.cs.tu-berlin.de", 300, onNtpReceive);
  29. // FTP Server;
  30. FTPServer ftp;
  31. void init()
  32. {
  33. spiffs_mount(); // Mount file system, in order to work with files
  34. Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
  35. Serial.systemDebugOutput(false); // Debug output to serial
  36. Serial.println("Wall Segment Clock");
  37. ActiveConfig = loadConfig();
  38. // Select control line
  39. pinMode(CONTROL_PIN, OUTPUT);
  40. PinRes(CONTROL_PIN);
  41. //wait for sensor startup
  42. delay(1000);
  43. // DHT sensor start
  44. dht.begin();
  45. // 7-segment output
  46. MAX7219_Init();
  47. // set timezone hourly difference to UTC
  48. SystemClock.setTimeZone(ActiveConfig.AddTZ);
  49. WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword);
  50. WifiStation.enable(true);
  51. WifiAccessPoint.enable(false);
  52. WifiStation.waitConnection(connectOk, 20, connectFail); // We recommend 20+ seconds for connection timeout at start
  53. // ðàç â ìèíóòó?
  54. procTimer.initializeMs(60000, process).start();
  55. process();
  56. // îáíîâëåíèå ýêðàíà äâà ðàçà â ñåêóíäó
  57. displayTimer.initializeMs(500, showTime).start();
  58. }
  59. void showTime()
  60. {
  61. static int8_t oldHour, oldMinute;
  62. static time_t oldTime;
  63. DateTime dt;
  64. int16_t *year;
  65. Time = SystemClock.now();
  66. if (oldTime != Time)
  67. {
  68. oldTime = Time;
  69. dt.setTime(Time);
  70. /*
  71. * òåïåðü â dt ó íàñ ñëåäóþùåå:
  72. * int8_t Hour;
  73. * int8_t Minute;
  74. * int8_t Second;
  75. * int16_t Milliseconds;
  76. * int8_t Day;
  77. * int8_t DayofWeek; -- Sunday is day 0
  78. * int8_t Month; // Jan is month 0
  79. * int16_t Year; // Full Year numer
  80. */
  81. MAX7219_writeData(MAX7219_DIGIT2, SYM_Minus);
  82. if (oldMinute != dt.Minute)
  83. {
  84. oldMinute = dt.Minute;
  85. MAX7219_writeData(MAX7219_DIGIT3, dt.Minute/10);
  86. MAX7219_writeData(MAX7219_DIGIT4, dt.Minute%10);
  87. if (oldHour != dt.Hour)
  88. {
  89. oldHour = dt.Hour;
  90. MAX7219_writeData(MAX7219_DIGIT0, dt.Hour/10);
  91. MAX7219_writeData(MAX7219_DIGIT1, dt.Hour%10);
  92. }
  93. }
  94. }
  95. else // time the same, output blank for "hh mm"
  96. {
  97. MAX7219_writeData(MAX7219_DIGIT2, SYM_Minus);
  98. }
  99. }
  100. /*
  101. * ×èòàåì è äàííûå ñ DHT22, â ñëó÷àå íåóäà÷è -- äàííûå îñòàíóòüñÿ ñòàðûìè.
  102. * ìåíÿ ýòî ïîëíîñòüþ óñòðàèâàåò.
  103. */
  104. void process()
  105. {
  106. TempAndHumidity th;
  107. ComfortState cf;
  108. static int8_t status;
  109. if(dht.readTempAndHumidity(th))
  110. {
  111. status = 0;
  112. SensorT = th.temp;
  113. SensorH = th.humid;
  114. SensorHI = dht.getHeatIndex();
  115. SensorCR = dht.getComfortRatio(cf);
  116. switch(cf)
  117. {
  118. case Comfort_OK:
  119. StrCF = "OK";
  120. break;
  121. case Comfort_TooHot:
  122. StrCF = "Too Hot";
  123. break;
  124. case Comfort_TooCold:
  125. StrCF = "Too Cold";
  126. break;
  127. case Comfort_TooDry:
  128. StrCF = "Too Dry";
  129. break;
  130. case Comfort_TooHumid:
  131. StrCF = "Too Humid";
  132. break;
  133. case Comfort_HotAndHumid:
  134. StrCF = "Hot And Humid";
  135. break;
  136. case Comfort_HotAndDry:
  137. StrCF = "Hot And Dry";
  138. break;
  139. case Comfort_ColdAndHumid:
  140. StrCF = "Cold And Humid";
  141. break;
  142. case Comfort_ColdAndDry:
  143. StrCF = "Cold And Dry";
  144. break;
  145. default:
  146. StrCF = "Unknown";
  147. break;
  148. }
  149. }
  150. else
  151. {
  152. /*
  153. *  ñëó÷àå, åñëè îò äàò÷èêà íè÷åãî íå ïîëó÷èëè, çàïóñòèì ïîâòîðíûé îïðîñ ÷åðåç
  154. * 10 ñåêóíä, íî íå áîëåå 5 ðàç ïîäðÿä.
  155. */
  156. if (status < 6)
  157. {
  158. status ++;
  159. procRTimer.initializeMs(10000, process).startOnce();
  160. }
  161. }
  162. }
  163. void startFTP()
  164. {
  165. // Start FTP server
  166. ftp.listen(21);
  167. ftp.addUser("user", "resu"); // FTP account
  168. // You can also use special FTP comand: "fsformat" for clearing file system (for example from TotalCMD)
  169. }
  170. void connectOk()
  171. {
  172. WifiAccessPoint.enable(false);
  173. Serial.print("I'm connecteed. IP: ");
  174. Serial.println(WifiStation.getIP().toString());
  175. startWebServer();
  176. startFTP();
  177. udp.listen(EchoPort);
  178. }
  179. /*
  180. * â ñëó÷àå íåóäà÷è ïîäêëþ÷åíèÿ ïîäíèìàåì òî÷êó äîñòóïà áåç àâòîðèçàöèè
  181. */
  182. void connectFail()
  183. {
  184. WifiAccessPoint.config("MeteoConfig", "", AUTH_OPEN);
  185. WifiAccessPoint.enable(true);
  186. // Stop main screen output
  187. procTimer.stop();
  188. displayTimer.stop();
  189. Serial.println("WiFi MeteoConfig");
  190. Serial.println(WifiAccessPoint.getIP());
  191. startWebServer();
  192. WifiStation.waitConnection(connectOk); // Wait connection
  193. }
  194. /*
  195. * NTP Client
  196. */
  197. void onNtpReceive(NtpClient& client, time_t timestamp) {
  198. SystemClock.setTime(timestamp, eTZ_UTC);
  199. NTPLastUpdate = SystemClock.now();
  200. Serial.println("*** Time synchronized OK! ***"); // DEBUG
  201. }
  202. /*
  203. * UDP Server
  204. */
  205. void onReceive(UdpConnection& connection, char *data, int size, IPAddress remoteIP, uint16_t remotePort)
  206. {
  207. debugf("UDP Sever callback from %s:%d, %d bytes", remoteIP.toString().c_str(), remotePort, size);
  208. // We implement string mode server for example
  209. Serial.print(">\t");
  210. Serial.print(data);
  211. if (data == "get_sensors")
  212. {
  213. String text = String("Temperature: ") + String(SensorT, 2) + String("\rHumidity: ") + String(SensorH, 2) + String("\r");
  214. udp.sendStringTo(remoteIP, EchoPort, text);
  215. }
  216. else
  217. {
  218. // Send echo to remote sender
  219. String text = String("echo: ") + data;
  220. udp.sendStringTo(remoteIP, EchoPort, text);
  221. }
  222. }