application.cpp 8.4 KB

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