application.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include <SmingCore.h>
  2. ///////////////////////////////////////////////////////////////////
  3. // Set your SSID & Pass for initial configuration
  4. #include "configuration.h" // application configuration
  5. ///////////////////////////////////////////////////////////////////
  6. #include "webserver.h"
  7. #include "tm1650.h"
  8. #include "AHTxx.h"
  9. #include "led_spi.h"
  10. #include "gy49.h"
  11. Timer procTimer, procRTimer;
  12. Timer displayTimer, tmpTimer;
  13. Timer showHighTimer, showLowTimer;
  14. Timer brightTimer;
  15. // Sensors values
  16. ahtxx_t sensorData;
  17. float SensorT, SensorH, SensorHI, SensorCR;
  18. String StrCF;
  19. uint32_t SensorLux;
  20. // Time values
  21. time_t Time, NTPLastUpdate;
  22. DateTime dt;
  23. void GetData(void);
  24. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel);
  25. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason);
  26. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway);
  27. void showWatch(void);
  28. void showTime(void);
  29. void showTemperature(void);
  30. void showHumidity(void);
  31. void showError(void);
  32. void setBright(void);
  33. // NTP Client
  34. void onNtpReceive(NtpClient& client, time_t timestamp);
  35. NtpClient ntpClient("ntp.time.in.ua", 1500, onNtpReceive); // every 15 min
  36. void init(void) {
  37. spiffs_mount(); // Mount file system, in order to work with files
  38. Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
  39. Serial.systemDebugOutput(false); // Debug output to serial
  40. Serial.println("Wall Segment Clock");
  41. ActiveConfig = loadConfig();
  42. // set timezone hourly difference to UTC
  43. SystemClock.setTimeZone(ActiveConfig.AddTZ);
  44. WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword);
  45. WifiStation.enable(true);
  46. WifiAccessPoint.enable(false);
  47. WifiEvents.onStationConnect(connectOk);
  48. WifiEvents.onStationDisconnect(connectFail);
  49. WifiEvents.onStationGotIP(gotIP);
  50. // initialize I2C
  51. Wire.pins(4, 5);
  52. Wire.begin();
  53. // BIG digits
  54. LED_Init();
  55. // Low LED output
  56. TM1650_Init();
  57. brightTimer.initializeMs(1000, setBright).start();
  58. // refresh big led
  59. displayTimer.initializeMs(500, showWatch).start();
  60. // init sensors
  61. AHTxx_Init();
  62. GY49_Init();
  63. // polling sensors - once per two seconds
  64. procTimer.initializeMs(2000, GetData).start();
  65. }
  66. void showWatch(void) {
  67. static time_t oldTime;
  68. Time = SystemClock.now();
  69. dt.setTime(Time);
  70. /*
  71. * Now, in dt we have:
  72. * int8_t Hour;
  73. * int8_t Minute;
  74. * int8_t Second;
  75. * int16_t Milliseconds;
  76. * int8_t Day;
  77. * int8_t DayofWeek; -- Sunday is day 0
  78. * int8_t Month; // Jan is month 0
  79. * int16_t Year; // Full Year numer
  80. */
  81. if (oldTime == Time) {
  82. // Old Second
  83. LED_SemicolonOFF();
  84. } else {
  85. // New Second
  86. oldTime = Time;
  87. LED_ShowBin(dt.Hour, dt.Minute);
  88. LED_SemicolonOn();
  89. if (dt.Second == 0x00) {
  90. Serial.printf("Time: %02d:%02d:00\r\n", dt.Hour, dt.Minute);
  91. }
  92. }
  93. }
  94. /*
  95. * Выводим текущее время [HH MM] на верхние индикаторы
  96. */
  97. void showTime(void) {
  98. static uint8_t oldHour = 0xFF, oldMinute = 0xFF;
  99. if (oldMinute != dt.Minute) {
  100. oldMinute = dt.Minute;
  101. // ...
  102. if (oldHour != dt.Hour) {
  103. oldHour = dt.Hour;
  104. // ...
  105. } // new hour
  106. } // new minute
  107. }
  108. /*
  109. * Show temperature, small indicators
  110. */
  111. void showTemperature(void) {
  112. uint8_t a, b, c;
  113. a = sensorData.Temperature / 100;
  114. c = sensorData.Temperature % 100;
  115. b = c / 10;
  116. c = c % 10;
  117. TM1650_Out(a, b, c, 0);
  118. TM1650_Out4(Sym_C);
  119. TM1650_DotSet(Dig_2);
  120. }
  121. /*
  122. * Show humidity, small indicators
  123. */
  124. void showHumidity(void) {
  125. uint8_t a, b, c;
  126. a = sensorData.Humidity / 100;
  127. c = sensorData.Humidity % 100;
  128. b = c / 10;
  129. c = c % 10;
  130. TM1650_Out(a, b, c, 0);
  131. TM1650_Out4(Sym_H);
  132. TM1650_DotSet(Dig_2);
  133. }
  134. /*
  135. * Show error, small indicators
  136. */
  137. void showError(void) {
  138. TM1650_DotRes(Dig_2);
  139. TM1650_Out1(Sym_E);
  140. TM1650_Out2(Sym_r);
  141. TM1650_Out3(Sym_r);
  142. TM1650_Out4(Sym_Off);
  143. }
  144. /*
  145. * Выводим дату на верхние индикаторы [DD MM]
  146. */
  147. void showDate(void) {
  148. // ...
  149. }
  150. /*
  151. * Автоматическая регулировка яркости индикаторов
  152. * GY-49 (MAX44009)
  153. */
  154. void setBright(void) {
  155. // ...
  156. }
  157. /**
  158. * @brief Get data from Temperature/Humidity Sensor.
  159. */
  160. void GetData(void) {
  161. static bool st = false;
  162. AHTxx_GetData(&sensorData);
  163. if (sensorData.Error != St_OK) {
  164. Serial.println("Sensor: Data error!");
  165. return;
  166. }
  167. SensorT = (float)sensorData.Temperature / 10;
  168. SensorH = (float)sensorData.Humidity / 10;
  169. if (st) {
  170. st = !st;
  171. showTemperature();
  172. } else {
  173. st = !st;
  174. showHumidity();
  175. }
  176. Serial.printf("Humidity: %d.%d %%; Temperature: %d.%d *C\r\n", sensorData.Humidity/10, sensorData.Humidity%10, sensorData.Temperature/10, sensorData.Temperature%10);
  177. SensorLux = GY49_GetData();
  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. }