application.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "webserver.h"
  9. DHT dht(DHT_PIN, DHT22);
  10. Timer procTimer;
  11. Timer displayTimer;
  12. bool state = false;
  13. // Sensors string values
  14. String StrT, StrRH, StrHI, StrCR, StrCF, StrTime;
  15. String StrVDD, StrADC;
  16. Timer httpcTimer;
  17. HttpClient thingSpeak;
  18. /* FTPServer ftp; */
  19. void process();
  20. void showValues();
  21. void connectOk();
  22. void connectFail();
  23. void onDataSent(HttpClient& client, bool successful);
  24. void sendData();
  25. void onNtpReceive(NtpClient& client, time_t timestamp);
  26. NtpClient ntpClient ("ntps1-0.cs.tu-berlin.de", 60, onNtpReceive);
  27. void init()
  28. {
  29. spiffs_mount(); // Mount file system, in order to work with files
  30. Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
  31. Serial.systemDebugOutput(false); // Debug output to serial
  32. Serial.println("Wall Segment Clock");
  33. ActiveConfig = loadConfig();
  34. // Select control line
  35. pinMode(CONTROL_PIN, OUTPUT);
  36. digitalWrite(CONTROL_PIN, LOW);
  37. // Restart main screen output
  38. procTimer.restart();
  39. displayTimer.stop();
  40. //wait for sensor startup
  41. delay(1000);
  42. // DHT sensor start
  43. dht.begin();
  44. // set timezone hourly difference to UTC
  45. SystemClock.setTimeZone(ActiveConfig.AddTZ);
  46. WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword);
  47. WifiStation.enable(true);
  48. WifiAccessPoint.enable(false);
  49. WifiStation.waitConnection(connectOk, 20, connectFail); // We recommend 20+ seconds for connection timeout at start
  50. // ðàç â ìèíóòó?
  51. procTimer.initializeMs(60000, process).start();
  52. process();
  53. }
  54. void showValues()
  55. {
  56. Serial.print("Date & Time: ");
  57. Serial.println(SystemClock.getSystemTimeString());
  58. Serial.print("Temperature: ");
  59. Serial.print(StrT);
  60. Serial.println("*C");
  61. Serial.print("Humidity: ");
  62. Serial.print(StrRH);
  63. Serial.println("%");
  64. Serial.print("Heatindex: ");
  65. Serial.print(StrHI);
  66. Serial.println("*C");
  67. Serial.print("Comfort: ");
  68. Serial.print(StrCR);
  69. Serial.print("% - ");
  70. Serial.println(StrCF);
  71. Serial.print("ADC value: ");
  72. Serial.println(StrADC);
  73. Serial.print("VDD value: ");
  74. Serial.println(StrVDD);
  75. Serial.println("");
  76. }
  77. void process()
  78. {
  79. StrTime = SystemClock.getSystemTimeString();
  80. float t = dht.readTemperature() + ActiveConfig.AddT;
  81. float h = dht.readHumidity() + ActiveConfig.AddRH;
  82. float hi = dht.getHeatIndex();
  83. ComfortState cf;
  84. float cr = dht.getComfortRatio(cf);
  85. if (ActiveConfig.Trigger == eTT_Temperature)
  86. state = t < ActiveConfig.RangeMin || t > ActiveConfig.RangeMax;
  87. else if (ActiveConfig.Trigger == eTT_Humidity)
  88. state = h < ActiveConfig.RangeMin || h > ActiveConfig.RangeMax;
  89. digitalWrite(CONTROL_PIN, state);
  90. StrT = String(t, 1);
  91. StrRH = String(h, 1);
  92. StrHI = String(hi, 1);
  93. StrCR = String(cr, 0);
  94. switch(cf)
  95. {
  96. case Comfort_OK:
  97. StrCF = "OK";
  98. break;
  99. case Comfort_TooHot:
  100. StrCF = "Too Hot";
  101. break;
  102. case Comfort_TooCold:
  103. StrCF = "Too Cold";
  104. break;
  105. case Comfort_TooDry:
  106. StrCF = "Too Dry";
  107. break;
  108. case Comfort_TooHumid:
  109. StrCF = "Too Humid";
  110. break;
  111. case Comfort_HotAndHumid:
  112. StrCF = "Hot And Humid";
  113. break;
  114. case Comfort_HotAndDry:
  115. StrCF = "Hot And Dry";
  116. break;
  117. case Comfort_ColdAndHumid:
  118. StrCF = "Cold And Humid";
  119. break;
  120. case Comfort_ColdAndDry:
  121. StrCF = "Cold And Dry";
  122. break;
  123. default:
  124. StrCF = "Unknown:";
  125. break;
  126. }
  127. StrADC = String(system_adc_read());
  128. StrVDD = String(system_get_vdd33());
  129. if (!displayTimer.isStarted())
  130. displayTimer.initializeMs(20000, showValues).start();
  131. // îáíîâëåíèå âûâîäà -- ðàç â 20 ñåê. îáíîâëåíèå äàííûõ -- ðàç â ìèíóòó.
  132. }
  133. /*
  134. void startFTP()
  135. {
  136. if (!fileExist("index.html"))
  137. fileSetContent("index.html", "<h3>Please connect to FTP and upload files from folder 'web/build' (details in code)</h3>");
  138. // Start FTP server
  139. ftp.listen(21);
  140. ftp.addUser("user", "resu"); // FTP account
  141. // You can also use special FTP comand: "fsformat" for clearing file system (for example from TotalCMD)
  142. }
  143. */
  144. void connectOk()
  145. {
  146. // debugf("connected");
  147. WifiAccessPoint.enable(false);
  148. Serial.print("I'm connecteed. IP: ");
  149. Serial.println(WifiStation.getIP().toString());
  150. // Start send data loop
  151. httpcTimer.initializeMs(60 * 1000, sendData).start(); // every 60 seconds
  152. startWebServer();
  153. /* startFTP(); */
  154. }
  155. /*
  156. * â ñëó÷àå íåóäà÷è ïîäêëþ÷åíèÿ ïîäíèìàåì òî÷êó äîñòóïà áåç àâòîðèçàöèè
  157. */
  158. void connectFail()
  159. {
  160. // debugf("connection FAILED");
  161. WifiAccessPoint.config("MeteoConfig", "", AUTH_OPEN);
  162. WifiAccessPoint.enable(true);
  163. // Stop main screen output
  164. procTimer.stop();
  165. displayTimer.stop();
  166. Serial.println("WiFi MeteoConfig");
  167. Serial.println(WifiAccessPoint.getIP());
  168. startWebServer();
  169. WifiStation.waitConnection(connectOk); // Wait connection
  170. }
  171. /*
  172. * Îòïðàâêà äàííûõ íà âíåøíèé ñåðâåð
  173. */
  174. void onDataSent(HttpClient& client, bool successful)
  175. {
  176. if (successful)
  177. Serial.println("Success sent");
  178. else
  179. Serial.println("Failed");
  180. String response = client.getResponseString();
  181. Serial.println("Server response: '" + response + "'");
  182. if (response.length() > 0)
  183. {
  184. int intVal = response.toInt();
  185. if (intVal == 0)
  186. Serial.println("Sensor value wasn't accepted. May be we need to wait a little?");
  187. }
  188. }
  189. void sendData()
  190. {
  191. if (thingSpeak.isProcessing()) return; // We need to wait while request processing was completed
  192. thingSpeak.downloadString("http://api.thingspeak.com/update?key=26WYU9LJCBC3AE1X&field1=" + StrT + "&field2=" + StrRH + "&field3=" + StrHI, onDataSent);
  193. }
  194. /*
  195. * NTP
  196. */
  197. void onNtpReceive(NtpClient& client, time_t timestamp) {
  198. SystemClock.setTime(timestamp, eTZ_UTC);
  199. Serial.println("*** Time synchronized! ***");
  200. // Serial.println(SystemClock.getSystemTimeString());
  201. }