application.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_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, c;
  110. a = sensorData.Temperature / 100;
  111. c = sensorData.Temperature % 100;
  112. b = c / 10;
  113. c = c % 10;
  114. TM1650_Out(a, b, c, 0);
  115. TM1650_Out4(Sym_C);
  116. TM1650_DotSet(Dig_2);
  117. }
  118. /*
  119. * Show humidity, small indicators
  120. */
  121. void showHumidity(void) {
  122. uint8_t a, b, c;
  123. a = sensorData.Humidity / 100;
  124. c = sensorData.Humidity % 100;
  125. b = c / 10;
  126. c = c % 10;
  127. TM1650_Out(a, b, c, 0);
  128. TM1650_Out4(Sym_H);
  129. TM1650_DotSet(Dig_2);
  130. }
  131. /*
  132. * Show error, small indicators
  133. */
  134. void showError(void) {
  135. TM1650_DotRes(Dig_2);
  136. TM1650_Out1(Sym_E);
  137. TM1650_Out2(Sym_r);
  138. TM1650_Out3(Sym_r);
  139. TM1650_Out4(Sym_Off);
  140. }
  141. /*
  142. * Выводим дату на верхние индикаторы [DD MM]
  143. */
  144. void showDate(void) {
  145. // ...
  146. }
  147. /*
  148. * Автоматическая регулировка яркости индикаторов
  149. * GY-49 (MAX44009)
  150. */
  151. void setBright(void) {
  152. // ...
  153. }
  154. /**
  155. * @brief Get data from Temperature/Humidity Sensor.
  156. */
  157. void GetData(void) {
  158. static bool st = false;
  159. AHTxx_GetData(&sensorData);
  160. if (sensorData.Error != St_OK) {
  161. Serial.println("Sensor: Data error!");
  162. return;
  163. }
  164. SensorT = (float)sensorData.Temperature / 10;
  165. SensorH = (float)sensorData.Humidity / 10;
  166. if (st) {
  167. st = !st;
  168. showTemperature();
  169. } else {
  170. st = !st;
  171. showHumidity();
  172. }
  173. Serial.printf("Humidity: %d.%d %%; Temperature: %d.%d *C\r\n", sensorData.Humidity/10, sensorData.Humidity%10, sensorData.Temperature/10, sensorData.Temperature%10);
  174. }
  175. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel)
  176. {
  177. debugf("connected");
  178. WifiAccessPoint.enable(false);
  179. }
  180. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
  181. {
  182. Serial.print("Got IP address: ");
  183. Serial.println(ip);
  184. // Restart main screen output
  185. procTimer.restart();
  186. displayTimer.restart();
  187. // start NTP Client there?
  188. startWebServer();
  189. }
  190. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
  191. {
  192. debugf("connection FAILED: %s", WifiEvents.getDisconnectReasonDesc(reason).c_str());
  193. WifiAccessPoint.config("ClockConfig", "", AUTH_OPEN);
  194. WifiAccessPoint.enable(true);
  195. // Stop main screen output
  196. procTimer.stop();
  197. displayTimer.stop();
  198. Serial.println("WiFi ClockConfig");
  199. Serial.println(WifiAccessPoint.getIP());
  200. startWebServer();
  201. WifiStation.disconnect();
  202. WifiStation.connect();
  203. }
  204. /**
  205. * @brief NTP Client
  206. */
  207. void onNtpReceive(NtpClient& client, time_t timestamp)
  208. {
  209. SystemClock.setTime(timestamp, eTZ_UTC);
  210. NTPLastUpdate = SystemClock.now();
  211. Serial.println("*** Time synchronized OK! ***"); // DEBUG
  212. }