application.cpp 5.6 KB

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