application.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 string values
  16. String StrT, StrRH, StrHI, StrCR, StrCF;
  17. String oldStrTime, StrTime;
  18. String StrVDD, StrADC;
  19. // date and time
  20. uint16_t Year, Month, Day, Hour, Minute, Second;
  21. Timer httpcTimer;
  22. HttpClient thingSpeak;
  23. /* FTPServer ftp; */
  24. void process();
  25. //void showValues();
  26. void connectOk();
  27. void connectFail();
  28. void onDataSent(HttpClient& client, bool successful);
  29. void sendData();
  30. void onNtpReceive(NtpClient& client, time_t timestamp);
  31. void showTime();
  32. NtpClient ntpClient ("ntps1-0.cs.tu-berlin.de", 60, onNtpReceive);
  33. void init()
  34. {
  35. spiffs_mount(); // Mount file system, in order to work with files
  36. Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
  37. Serial.systemDebugOutput(false); // Debug output to serial
  38. Serial.println("Wall Segment Clock");
  39. ActiveConfig = loadConfig();
  40. // Select control line
  41. pinMode(CONTROL_PIN, OUTPUT);
  42. digitalWrite(CONTROL_PIN, LOW);
  43. // Restart main screen output
  44. // procTimer.restart();
  45. // serialTimer.stop();
  46. //wait for sensor startup
  47. delay(1000);
  48. // DHT sensor start
  49. dht.begin();
  50. // 7-segment output
  51. MAX7219_Init();
  52. // set timezone hourly difference to UTC
  53. SystemClock.setTimeZone(ActiveConfig.AddTZ);
  54. WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword);
  55. WifiStation.enable(true);
  56. WifiAccessPoint.enable(false);
  57. WifiStation.waitConnection(connectOk, 20, connectFail); // We recommend 20+ seconds for connection timeout at start
  58. // ðàç â ìèíóòó?
  59. procTimer.initializeMs(60000, process).start();
  60. process();
  61. // îáíîâëåíèå ýêðàíà äâà ðàçà â ñåêóíäó
  62. displayTimer.initializeMs(500, showTime).start();
  63. }
  64. void showTime()
  65. {
  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. void process()
  114. {
  115. float t = dht.readTemperature() + ActiveConfig.AddT;
  116. float h = dht.readHumidity() + ActiveConfig.AddRH;
  117. float hi = dht.getHeatIndex();
  118. ComfortState cf;
  119. float cr = dht.getComfortRatio(cf);
  120. if (ActiveConfig.Trigger == eTT_Temperature)
  121. state = t < ActiveConfig.RangeMin || t > ActiveConfig.RangeMax;
  122. else if (ActiveConfig.Trigger == eTT_Humidity)
  123. state = h < ActiveConfig.RangeMin || h > ActiveConfig.RangeMax;
  124. digitalWrite(CONTROL_PIN, state);
  125. StrT = String(t, 0);
  126. StrRH = String(h, 0);
  127. StrHI = String(hi, 0);
  128. StrCR = String(cr, 0);
  129. switch(cf)
  130. {
  131. case Comfort_OK:
  132. StrCF = "OK";
  133. break;
  134. case Comfort_TooHot:
  135. StrCF = "Too Hot";
  136. break;
  137. case Comfort_TooCold:
  138. StrCF = "Too Cold";
  139. break;
  140. case Comfort_TooDry:
  141. StrCF = "Too Dry";
  142. break;
  143. case Comfort_TooHumid:
  144. StrCF = "Too Humid";
  145. break;
  146. case Comfort_HotAndHumid:
  147. StrCF = "Hot And Humid";
  148. break;
  149. case Comfort_HotAndDry:
  150. StrCF = "Hot And Dry";
  151. break;
  152. case Comfort_ColdAndHumid:
  153. StrCF = "Cold And Humid";
  154. break;
  155. case Comfort_ColdAndDry:
  156. StrCF = "Cold And Dry";
  157. break;
  158. default:
  159. StrCF = "Unknown:";
  160. break;
  161. }
  162. StrADC = String(system_adc_read());
  163. StrVDD = String(system_get_vdd33());
  164. }
  165. /*
  166. void startFTP()
  167. {
  168. // Start FTP server
  169. ftp.listen(21);
  170. ftp.addUser("user", "resu"); // FTP account
  171. // You can also use special FTP comand: "fsformat" for clearing file system (for example from TotalCMD)
  172. }
  173. */
  174. void connectOk()
  175. {
  176. // debugf("connected");
  177. WifiAccessPoint.enable(false);
  178. Serial.print("I'm connecteed. IP: ");
  179. Serial.println(WifiStation.getIP().toString());
  180. // Start send data loop
  181. httpcTimer.initializeMs(60 * 1000, sendData).start(); // every 60 seconds
  182. startWebServer();
  183. /* startFTP(); */
  184. }
  185. /*
  186. * â ñëó÷àå íåóäà÷è ïîäêëþ÷åíèÿ ïîäíèìàåì òî÷êó äîñòóïà áåç àâòîðèçàöèè
  187. */
  188. void connectFail()
  189. {
  190. // debugf("connection FAILED");
  191. WifiAccessPoint.config("MeteoConfig", "", AUTH_OPEN);
  192. WifiAccessPoint.enable(true);
  193. // Stop main screen output
  194. procTimer.stop();
  195. displayTimer.stop();
  196. Serial.println("WiFi MeteoConfig");
  197. Serial.println(WifiAccessPoint.getIP());
  198. startWebServer();
  199. WifiStation.waitConnection(connectOk); // Wait connection
  200. }
  201. /*
  202. * Îòïðàâêà äàííûõ íà âíåøíèé ñåðâåð
  203. */
  204. void onDataSent(HttpClient& client, bool successful)
  205. {
  206. if (successful)
  207. Serial.println("Success sent");
  208. else
  209. Serial.println("Failed");
  210. String response = client.getResponseString();
  211. Serial.println("Server response: '" + response + "'");
  212. if (response.length() > 0)
  213. {
  214. int intVal = response.toInt();
  215. if (intVal == 0)
  216. Serial.println("Sensor value wasn't accepted. May be we need to wait a little?");
  217. }
  218. }
  219. void sendData()
  220. {
  221. // We need to wait while request processing was completed
  222. if (thingSpeak.isProcessing()) return;
  223. thingSpeak.downloadString("http://api.thingspeak.com/update?key=26WYU9LJCBC3AE1X&field1=" + StrT + "&field2=" + StrRH + "&field3=" + StrHI, onDataSent);
  224. }
  225. /*
  226. * NTP
  227. */
  228. void onNtpReceive(NtpClient& client, time_t timestamp) {
  229. SystemClock.setTime(timestamp, eTZ_UTC);
  230. Serial.println("*** Time synchronized! ***");
  231. // Serial.println(SystemClock.getSystemTimeString());
  232. }