application.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include <SmingCore.h>
  2. #include <Libraries/DHTesp/DHTesp.h>
  3. ///////////////////////////////////////////////////////////////////
  4. // Set your SSID & Pass for initial configuration
  5. #include "configuration.h" // application configuration
  6. ///////////////////////////////////////////////////////////////////
  7. #include "webserver.h"
  8. #include "tm1650.h"
  9. #include "AHTxx.h"
  10. /** DHT22 */
  11. #define DHT22_PIN 2
  12. DHTesp dht;
  13. TempAndHumidity th;
  14. Timer readTemperatureProcTimer;
  15. void onTimer_readDHT22();
  16. Timer procTimer, procRTimer;
  17. Timer displayTimer, tmpTimer;
  18. Timer showHighTimer, showLowTimer;
  19. Timer dotTimer;
  20. // Sensors values
  21. ahtxx_t sensorData;
  22. String StrCF;
  23. // Time values
  24. time_t Time, NTPLastUpdate;
  25. DateTime dt;
  26. float SensorT, SensorH, SensorHI, SensorCR;
  27. void GetData(void);
  28. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel);
  29. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason);
  30. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway);
  31. void showWatch(void);
  32. void showTime(void);
  33. void dotOff(void);
  34. void showTemperature(void);
  35. void showHumidity(void);
  36. void showError(void);
  37. // NTP Client
  38. void onNtpReceive(NtpClient& client, time_t timestamp);
  39. NtpClient ntpClient("ntp.time.in.ua", 1500, onNtpReceive); // every 15 min
  40. void init(void) {
  41. spiffs_mount(); // Mount file system, in order to work with files
  42. Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
  43. Serial.systemDebugOutput(false); // Debug output to serial
  44. Serial.println("Wall Segment Clock");
  45. ActiveConfig = loadConfig();
  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. WifiEvents.onStationConnect(connectOk);
  52. WifiEvents.onStationDisconnect(connectFail);
  53. WifiEvents.onStationGotIP(gotIP);
  54. // initialize I2C
  55. Wire.pins(4, 5);
  56. Wire.begin();
  57. // BIG digits
  58. TM1650_Init();
  59. // Low LED output
  60. // refresh big led
  61. displayTimer.initializeMs(1000, showWatch).start();
  62. /* AHTxx Sensor */
  63. // AHTxx_Init();
  64. // procTimer.initializeMs(2000, GetData).start();
  65. /* DHT22 */
  66. dht.setup(DHT22_PIN, DHTesp::DHT22);
  67. readTemperatureProcTimer.initializeMs(5 * 1000, onTimer_readDHT22).start(); // every so often.
  68. }
  69. void showWatch(void) {
  70. static time_t oldTime;
  71. Time = SystemClock.now();
  72. dt.setTime(Time);
  73. /*
  74. * Now, in dt we have:
  75. * int8_t Hour;
  76. * int8_t Minute;
  77. * int8_t Second;
  78. * int16_t Milliseconds;
  79. * int8_t Day;
  80. * int8_t DayofWeek; -- Sunday is day 0
  81. * int8_t Month; // Jan is month 0
  82. * int16_t Year; // Full Year numer
  83. */
  84. if (oldTime != Time) {
  85. // New Second
  86. oldTime = Time;
  87. showTime();
  88. if (dt.Second == 0x00) {
  89. Serial.printf("Time: %02d:%02d:00\r\n", dt.Hour, dt.Minute);
  90. }
  91. }
  92. }
  93. /*
  94. * Выводим текущее время [HH MM] на верхние индикаторы
  95. */
  96. void showTime(void) {
  97. static uint8_t oldHour = 0xFF, oldMinute = 0xFF;
  98. if (oldMinute != dt.Minute) {
  99. oldMinute = dt.Minute;
  100. // ...
  101. if (oldHour != dt.Hour) {
  102. oldHour = dt.Hour;
  103. // ...
  104. } // new hour
  105. } // new minute
  106. TM1650_Out(dt.Hour>>4, dt.Hour&0xf, dt.Minute>>4, dt.Minute&0xf);
  107. TM1650_DotSet(Dig_2);
  108. TM1650_DotSet(Dig_3);
  109. dotTimer.initializeMs(500, dotOff);
  110. dotTimer.startOnce();
  111. }
  112. void dotOff(void)
  113. {
  114. TM1650_DotRes(Dig_2);
  115. TM1650_DotRes(Dig_3);
  116. }
  117. /*
  118. * Show temperature, small indicators
  119. */
  120. void showTemperature(void)
  121. {
  122. SensorT = th.temperature;
  123. uint8_t a, b;
  124. a = (uint8_t)th.temperature / 10;
  125. b = (uint8_t)th.temperature % 10;
  126. TM1650_Out(a, b, 0, 0);
  127. TM1650_Out3(Sym_o);
  128. TM1650_Out4(Sym_C);
  129. }
  130. /*
  131. * Show humidity, small indicators
  132. */
  133. void showHumidity(void) {
  134. SensorH = th.humidity;
  135. uint8_t a, b;
  136. a = (uint8_t)th.humidity / 10;
  137. b = (uint8_t)th.humidity % 10;
  138. TM1650_Out(a, b, 0, 0);
  139. TM1650_Out3(Sym_Off);
  140. TM1650_Out4(Sym_H);
  141. }
  142. /*
  143. * Show error, small indicators
  144. */
  145. void showError(void) {
  146. TM1650_DotRes(Dig_2);
  147. TM1650_Out1(Sym_E);
  148. TM1650_Out2(Sym_r);
  149. TM1650_Out3(Sym_r);
  150. TM1650_Out4(Sym_Off);
  151. }
  152. /*
  153. * Выводим дату на верхние индикаторы [DD MM]
  154. */
  155. void showDate(void) {
  156. // ...
  157. }
  158. /**
  159. * @brief Get data from Temperature/Humidity Sensor.
  160. */
  161. void GetData(void) {
  162. static bool st = false;
  163. AHTxx_GetData(&sensorData);
  164. if (sensorData.Error != St_OK) {
  165. Serial.println("Sensor: Data error!");
  166. return;
  167. }
  168. th.temperature = (float)sensorData.Temperature / 10.0;
  169. th.humidity = (float)sensorData.Humidity / 10.0;
  170. if (st) {
  171. st = !st;
  172. showTemperature();
  173. } else {
  174. st = !st;
  175. showHumidity();
  176. }
  177. Serial.printf("Humidity: %d.%d %%; Temperature: %d.%d *C\r\n", sensorData.Humidity/10, sensorData.Humidity%10, sensorData.Temperature/10, sensorData.Temperature%10);
  178. }
  179. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel)
  180. {
  181. debugf("connected");
  182. WifiAccessPoint.enable(false);
  183. }
  184. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
  185. {
  186. Serial.print("Got IP address: ");
  187. Serial.println(ip);
  188. // Restart main screen output
  189. procTimer.restart();
  190. displayTimer.restart();
  191. // start NTP Client there?
  192. startWebServer();
  193. }
  194. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
  195. {
  196. debugf("connection FAILED: %s", WifiEvents.getDisconnectReasonDesc(reason).c_str());
  197. WifiAccessPoint.config("ClockConfig", "", AUTH_OPEN);
  198. WifiAccessPoint.enable(true);
  199. // Stop main screen output
  200. procTimer.stop();
  201. displayTimer.stop();
  202. Serial.println("WiFi ClockConfig");
  203. Serial.println(WifiAccessPoint.getIP());
  204. startWebServer();
  205. WifiStation.disconnect();
  206. WifiStation.connect();
  207. }
  208. /**
  209. * @brief NTP Client
  210. */
  211. void onNtpReceive(NtpClient& client, time_t timestamp)
  212. {
  213. SystemClock.setTime(timestamp, eTZ_UTC);
  214. NTPLastUpdate = SystemClock.now();
  215. Serial.println("*** Time synchronized OK! ***"); // DEBUG
  216. }
  217. void onTimer_readDHT22()
  218. {
  219. //* try different reading methods (Adafruit compatible) vs improved */
  220. static bool toggle = false;
  221. toggle = !toggle;
  222. float humidity = 0;
  223. float temperature = 0;
  224. Serial << _F("TickCount=") << RTC.getRtcNanoseconds() / 1000000 << endl;
  225. if(toggle) {
  226. Serial.println(_F("Read using Adafruit API methods"));
  227. humidity = dht.getHumidity();
  228. temperature = dht.getTemperature();
  229. th.humidity = humidity;
  230. th.temperature = temperature;
  231. // check if returns are valid, if they are NaN (not a number) then something went wrong!
  232. if(dht.getStatus() == DHTesp::ERROR_NONE) {
  233. Serial << _F("\tHumidity: ") << humidity << _F("% Temperature: ") << temperature << " °C" << endl;
  234. } else {
  235. Serial << _F("Failed to read from DHT: ") << dht.getStatus() << endl;
  236. }
  237. } else {
  238. //* improved reading method
  239. Serial.println(_F("\r\n"
  240. "Read using new API methods"));
  241. th = dht.getTempAndHumidity();
  242. humidity = th.humidity;
  243. temperature = th.temperature;
  244. if(dht.getStatus() == DHTesp::ERROR_NONE) {
  245. Serial << _F("\tHumidity: ") << th.humidity << _F("% Temperature: ") << th.temperature << " °C" << endl;
  246. } else {
  247. Serial << _F("Failed to read from DHT: ") << dht.getStatus() << endl;
  248. }
  249. }
  250. // Other goodies:
  251. //
  252. // Heatindex is the perceived temperature taking humidity into account
  253. // More: https://en.wikipedia.org/wiki/Heat_index
  254. //
  255. Serial << _F("Heatindex: ") << dht.computeHeatIndex(temperature, humidity) << " °C" << endl;
  256. //
  257. // Dewpoint is the temperature where condensation starts.
  258. // Water vapors will start condensing on an object having this temperature or below.
  259. // More: https://en.wikipedia.org/wiki/Dew_point
  260. //
  261. Serial << _F("Dewpoint: ") << dht.computeDewPoint(temperature, humidity) << " °C" << endl;
  262. //
  263. // Determine thermal comfort according to http://epb.apogee.net/res/refcomf.asp
  264. //
  265. ComfortState cf;
  266. Serial << _F("Comfort is at ") << dht.getComfortRatio(cf, temperature, humidity) << " %, (";
  267. switch(cf) {
  268. case Comfort_OK:
  269. Serial.print(_F("OK"));
  270. break;
  271. case Comfort_TooHot:
  272. Serial.print(_F("Too Hot"));
  273. break;
  274. case Comfort_TooCold:
  275. Serial.print(_F("Too Cold"));
  276. break;
  277. case Comfort_TooDry:
  278. Serial.print(_F("Too Dry"));
  279. break;
  280. case Comfort_TooHumid:
  281. Serial.print(_F("Too Humid"));
  282. break;
  283. case Comfort_HotAndHumid:
  284. Serial.print(_F("Hot And Humid"));
  285. break;
  286. case Comfort_HotAndDry:
  287. Serial.print(_F("Hot And Dry"));
  288. break;
  289. case Comfort_ColdAndHumid:
  290. Serial.print(_F("Cold And Humid"));
  291. break;
  292. case Comfort_ColdAndDry:
  293. Serial.print(_F("Cold And Dry"));
  294. break;
  295. default:
  296. Serial.print(_F("Unknown:"));
  297. Serial.print(cf);
  298. break;
  299. }
  300. Serial.println(')');
  301. }