application.cpp 8.4 KB

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