application.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. float SensorT, SensorH, SensorHI, SensorCR;
  16. String StrCF;
  17. // Time values
  18. time_t Time, NTPLastUpdate;
  19. DateTime dt;
  20. void RequestData(void);
  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 setBright(void);
  30. // NTP Client
  31. void onNtpReceive(NtpClient& client, time_t timestamp);
  32. NtpClient ntpClient("ntp.time.in.ua", 1500, onNtpReceive); // every 15 min
  33. AHTxx sensor;
  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. // polling sensors - once per two seconds
  54. procTimer.initializeMs(2000, RequestData).start();
  55. // Low LED output
  56. TM1650_Init();
  57. brightTimer.initializeMs(1000, setBright).start();
  58. // обновление нижнего экрана два раза в секунду
  59. displayTimer.initializeMs(500, showWatch).start();
  60. }
  61. void showWatch(void) {
  62. static time_t oldTime;
  63. Time = SystemClock.now();
  64. dt.setTime(Time);
  65. /*
  66. * Now, in dt we have:
  67. * int8_t Hour;
  68. * int8_t Minute;
  69. * int8_t Second;
  70. * int16_t Milliseconds;
  71. * int8_t Day;
  72. * int8_t DayofWeek; -- Sunday is day 0
  73. * int8_t Month; // Jan is month 0
  74. * int16_t Year; // Full Year numer
  75. */
  76. if (oldTime == Time) {
  77. // Old Second
  78. LED_SemicolonOFF();
  79. } else {
  80. // New Second
  81. oldTime = Time;
  82. LED_ShowBin(dt.Hour, dt.Minute);
  83. LED_SemicolonOn();
  84. if (dt.Second == 0x00) {
  85. Serial.printf("Time: %02d:%02d:00\r\n", dt.Hour, dt.Minute);
  86. }
  87. }
  88. }
  89. /*
  90. * Выводим текущее время [HH MM] на верхние индикаторы
  91. */
  92. void showTime(void) {
  93. static uint8_t oldHour = 0xFF, oldMinute = 0xFF;
  94. if (oldMinute != dt.Minute) {
  95. oldMinute = dt.Minute;
  96. // ...
  97. if (oldHour != dt.Hour) {
  98. oldHour = dt.Hour;
  99. // ...
  100. } // new hour
  101. } // new minute
  102. }
  103. /*
  104. * Выводим температуру на нижние индикаторы
  105. */
  106. void showTemperature(void) {
  107. // ...
  108. TM1650_Out(dt.Hour >> 4, dt.Hour & 0xf, dt.Minute >>4, dt.Minute & 0xf);
  109. }
  110. /*
  111. * Выводим влажность на нижние индикаторы
  112. */
  113. void showHumidity(void) {
  114. // ...
  115. }
  116. /*
  117. * Выводим дату на верхние индикаторы [DD MM]
  118. */
  119. void showDate(void) {
  120. // ...
  121. }
  122. /*
  123. * Автоматическая регулировка яркости индикаторов
  124. * GY-49 (MAX44009)
  125. */
  126. void setBright(void) {
  127. // ...
  128. }
  129. /**
  130. * @brief Start Sensor measure.
  131. */
  132. void RequestData(void) {
  133. if (sensor.IsReadyToRequest()) {
  134. sensor.StartMeasure();
  135. tmpTimer.initializeMs(600, GetData).startOnce();
  136. } else {
  137. Serial.println("Sensor: not ready to request.");
  138. }
  139. }
  140. /**
  141. * @brief Get data from Temperature/Humidity Sensor.
  142. */
  143. void GetData(void) {
  144. ahtxx_t data;
  145. sensor.GetData(&data);
  146. if (data.Error != St_OK) {
  147. Serial.println("Sensor: Data error!");
  148. return;
  149. }
  150. SensorH = data.Humidity / 10;
  151. SensorT = data.Temperature / 10;
  152. Serial.print("Humidity: ");
  153. Serial.print(SensorH);
  154. Serial.print("%. Temperature: ");
  155. Serial.print(SensorT);
  156. Serial.println("*C");
  157. }
  158. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel)
  159. {
  160. debugf("connected");
  161. WifiAccessPoint.enable(false);
  162. }
  163. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
  164. {
  165. Serial.print("Got IP address: ");
  166. Serial.println(ip);
  167. // Restart main screen output
  168. procTimer.restart();
  169. displayTimer.restart();
  170. // start NTP Client there?
  171. startWebServer();
  172. }
  173. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
  174. {
  175. debugf("connection FAILED: %s", WifiEvents.getDisconnectReasonDesc(reason).c_str());
  176. WifiAccessPoint.config("ClockConfig", "", AUTH_OPEN);
  177. WifiAccessPoint.enable(true);
  178. // Stop main screen output
  179. procTimer.stop();
  180. displayTimer.stop();
  181. Serial.println("WiFi ClockConfig");
  182. Serial.println(WifiAccessPoint.getIP());
  183. startWebServer();
  184. WifiStation.disconnect();
  185. WifiStation.connect();
  186. }
  187. /**
  188. * @brief NTP Client
  189. */
  190. void onNtpReceive(NtpClient& client, time_t timestamp)
  191. {
  192. SystemClock.setTime(timestamp, eTZ_UTC);
  193. NTPLastUpdate = SystemClock.now();
  194. Serial.println("*** Time synchronized OK! ***"); // DEBUG
  195. }