application.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /* AHTxx Sensor */
  59. AHTxx_Init();
  60. procTimer.initializeMs(2000, GetData).start();
  61. }
  62. void showWatch(void) {
  63. static time_t oldTime;
  64. Time = SystemClock.now();
  65. dt.setTime(Time);
  66. /*
  67. * Now, in dt we have:
  68. * int8_t Hour;
  69. * int8_t Minute;
  70. * int8_t Second;
  71. * int16_t Milliseconds;
  72. * int8_t Day;
  73. * int8_t DayofWeek; -- Sunday is day 0
  74. * int8_t Month; // Jan is month 0
  75. * int16_t Year; // Full Year numer
  76. */
  77. if (oldTime == Time) {
  78. // Old Second
  79. LED_SemicolonOFF();
  80. } else {
  81. // New Second
  82. oldTime = Time;
  83. LED_ShowBin(dt.Hour, dt.Minute);
  84. // LED_ShowBin(dt.Minute, dt.Second);
  85. LED_SemicolonOn();
  86. if (dt.Second == 0x00) {
  87. Serial.printf("Time: %02d:%02d:00\r\n", dt.Hour, dt.Minute);
  88. }
  89. }
  90. }
  91. /*
  92. * Выводим текущее время [HH MM] на верхние индикаторы
  93. */
  94. void showTime(void) {
  95. static uint8_t oldHour = 0xFF, oldMinute = 0xFF;
  96. if (oldMinute != dt.Minute) {
  97. oldMinute = dt.Minute;
  98. // ...
  99. if (oldHour != dt.Hour) {
  100. oldHour = dt.Hour;
  101. // ...
  102. } // new hour
  103. } // new minute
  104. }
  105. /*
  106. * Show temperature, small indicators
  107. */
  108. void showTemperature(void) {
  109. uint8_t a, b;
  110. a = sensorData.Temperature / 100;
  111. b = (sensorData.Temperature % 100) / 10;
  112. TM1650_Out(a, b, 0, 0);
  113. TM1650_Out3(Sym_o);
  114. TM1650_Out4(Sym_C);
  115. }
  116. /*
  117. * Show humidity, small indicators
  118. */
  119. void showHumidity(void) {
  120. uint8_t a, b;
  121. a = sensorData.Humidity / 100;
  122. b = (sensorData.Humidity % 100) / 10;
  123. TM1650_Out(a, b, 0, 0);
  124. TM1650_Out3(Sym_Off);
  125. TM1650_Out4(Sym_H);
  126. }
  127. /*
  128. * Show error, small indicators
  129. */
  130. void showError(void) {
  131. TM1650_DotRes(Dig_2);
  132. TM1650_Out1(Sym_E);
  133. TM1650_Out2(Sym_r);
  134. TM1650_Out3(Sym_r);
  135. TM1650_Out4(Sym_Off);
  136. }
  137. /*
  138. * Выводим дату на верхние индикаторы [DD MM]
  139. */
  140. void showDate(void) {
  141. // ...
  142. }
  143. /*
  144. * Автоматическая регулировка яркости индикаторов
  145. * GY-49 (MAX44009)
  146. */
  147. void setBright(void) {
  148. // ...
  149. }
  150. /**
  151. * @brief Get data from Temperature/Humidity Sensor.
  152. */
  153. void GetData(void) {
  154. static bool st = false;
  155. AHTxx_GetData(&sensorData);
  156. if (sensorData.Error != St_OK) {
  157. Serial.println("Sensor: Data error!");
  158. return;
  159. }
  160. SensorT = (float)sensorData.Temperature / 10;
  161. SensorH = (float)sensorData.Humidity / 10;
  162. if (st) {
  163. st = !st;
  164. showTemperature();
  165. } else {
  166. st = !st;
  167. showHumidity();
  168. }
  169. Serial.printf("Humidity: %d.%d %%; Temperature: %d.%d *C\r\n", sensorData.Humidity/10, sensorData.Humidity%10, sensorData.Temperature/10, sensorData.Temperature%10);
  170. }
  171. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel)
  172. {
  173. debugf("connected");
  174. WifiAccessPoint.enable(false);
  175. }
  176. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
  177. {
  178. Serial.print("Got IP address: ");
  179. Serial.println(ip);
  180. // Restart main screen output
  181. procTimer.restart();
  182. displayTimer.restart();
  183. // start NTP Client there?
  184. startWebServer();
  185. }
  186. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
  187. {
  188. debugf("connection FAILED: %s", WifiEvents.getDisconnectReasonDesc(reason).c_str());
  189. WifiAccessPoint.config("ClockConfig", "", AUTH_OPEN);
  190. WifiAccessPoint.enable(true);
  191. // Stop main screen output
  192. procTimer.stop();
  193. displayTimer.stop();
  194. Serial.println("WiFi ClockConfig");
  195. Serial.println(WifiAccessPoint.getIP());
  196. startWebServer();
  197. WifiStation.disconnect();
  198. WifiStation.connect();
  199. }
  200. /**
  201. * @brief NTP Client
  202. */
  203. void onNtpReceive(NtpClient& client, time_t timestamp)
  204. {
  205. SystemClock.setTime(timestamp, eTZ_UTC);
  206. NTPLastUpdate = SystemClock.now();
  207. Serial.println("*** Time synchronized OK! ***"); // DEBUG
  208. }