application.cpp 8.5 KB

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