application.cpp 8.8 KB

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