application.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_ShowBin(dt.Minute, dt.Second);
  89. LED_SemicolonOn();
  90. if (dt.Second == 0x00) {
  91. Serial.printf("Time: %02d:%02d:00\r\n", dt.Hour, dt.Minute);
  92. }
  93. }
  94. }
  95. /*
  96. * Выводим текущее время [HH MM] на верхние индикаторы
  97. */
  98. void showTime(void) {
  99. static uint8_t oldHour = 0xFF, oldMinute = 0xFF;
  100. if (oldMinute != dt.Minute) {
  101. oldMinute = dt.Minute;
  102. // ...
  103. if (oldHour != dt.Hour) {
  104. oldHour = dt.Hour;
  105. // ...
  106. } // new hour
  107. } // new minute
  108. }
  109. /*
  110. * Show temperature, small indicators
  111. */
  112. void showTemperature(void) {
  113. uint8_t a, b;
  114. a = sensorData.Temperature / 100;
  115. b = (sensorData.Temperature % 100) / 10;
  116. TM1650_Out(a, b, 0, 0);
  117. TM1650_Out3(Sym_o);
  118. TM1650_Out4(Sym_C);
  119. }
  120. /*
  121. * Show humidity, small indicators
  122. */
  123. void showHumidity(void) {
  124. uint8_t a, b;
  125. a = sensorData.Humidity / 100;
  126. b = (sensorData.Humidity % 100) / 10;
  127. TM1650_Out(a, b, 0, 0);
  128. TM1650_Out3(Sym_Off);
  129. TM1650_Out4(Sym_H);
  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. SensorLux = GY49_GetData();
  175. }
  176. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel)
  177. {
  178. debugf("connected");
  179. WifiAccessPoint.enable(false);
  180. }
  181. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
  182. {
  183. Serial.print("Got IP address: ");
  184. Serial.println(ip);
  185. // Restart main screen output
  186. procTimer.restart();
  187. displayTimer.restart();
  188. // start NTP Client there?
  189. startWebServer();
  190. }
  191. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
  192. {
  193. debugf("connection FAILED: %s", WifiEvents.getDisconnectReasonDesc(reason).c_str());
  194. WifiAccessPoint.config("ClockConfig", "", AUTH_OPEN);
  195. WifiAccessPoint.enable(true);
  196. // Stop main screen output
  197. procTimer.stop();
  198. displayTimer.stop();
  199. Serial.println("WiFi ClockConfig");
  200. Serial.println(WifiAccessPoint.getIP());
  201. startWebServer();
  202. WifiStation.disconnect();
  203. WifiStation.connect();
  204. }
  205. /**
  206. * @brief NTP Client
  207. */
  208. void onNtpReceive(NtpClient& client, time_t timestamp)
  209. {
  210. SystemClock.setTime(timestamp, eTZ_UTC);
  211. NTPLastUpdate = SystemClock.now();
  212. Serial.println("*** Time synchronized OK! ***"); // DEBUG
  213. }