application.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. Timer procTimer, procRTimer;
  10. Timer displayTimer, tmpTimer;
  11. Timer showHighTimer, showLowTimer;
  12. Timer brightTimer;
  13. // Sensors values
  14. float SensorT, SensorH, SensorHI, SensorCR;
  15. String StrCF;
  16. // Time values
  17. time_t Time, NTPLastUpdate;
  18. DateTime dt;
  19. void RequestData(void);
  20. void GetData(void);
  21. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel);
  22. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason);
  23. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway);
  24. void showWatch(void);
  25. void showTime(void);
  26. void showTemperature(void);
  27. void showHumidity(void);
  28. void setBright(void);
  29. // NTP Client
  30. void onNtpReceive(NtpClient& client, time_t timestamp);
  31. NtpClient ntpClient("ntp.time.in.ua", 1500, onNtpReceive); // every 15 min
  32. AHTxx sensor;
  33. void init(void) {
  34. spiffs_mount(); // Mount file system, in order to work with files
  35. Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
  36. Serial.systemDebugOutput(false); // Debug output to serial
  37. Serial.println("Wall Segment Clock");
  38. ActiveConfig = loadConfig();
  39. // set timezone hourly difference to UTC
  40. SystemClock.setTimeZone(ActiveConfig.AddTZ);
  41. WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword);
  42. WifiStation.enable(true);
  43. WifiAccessPoint.enable(false);
  44. WifiEvents.onStationConnect(connectOk);
  45. WifiEvents.onStationDisconnect(connectFail);
  46. WifiEvents.onStationGotIP(gotIP);
  47. // initialize I2C
  48. Wire.pins(4, 5);
  49. Wire.begin();
  50. // Sensors start. Possible infinity loop...
  51. // Serial.println("Wait for Sensor...");
  52. // while (sensor.IsReadyToRequest() == false);
  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. * теперь в dt у нас следующее:
  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. // Старая секунда - нужно гасить точки
  78. TM1650_DotRes(Dig_2);
  79. } else {
  80. // Новая секунда
  81. oldTime = Time;
  82. TM1650_Out(dt.Hour >> 4, dt.Hour & 0xf, dt.Minute >>4, dt.Minute & 0xf);
  83. TM1650_DotSet(Dig_2);
  84. }
  85. }
  86. /*
  87. * Выводим текущее время [HH MM] на верхние индикаторы
  88. */
  89. void showTime(void) {
  90. static uint8_t oldHour = 0xFF, oldMinute = 0xFF;
  91. if (oldMinute != dt.Minute) {
  92. oldMinute = dt.Minute;
  93. // ...
  94. if (oldHour != dt.Hour) {
  95. oldHour = dt.Hour;
  96. // ...
  97. } // new hour
  98. } // new minute
  99. }
  100. /*
  101. * Выводим температуру на нижние индикаторы
  102. */
  103. void showTemperature(void) {
  104. // ...
  105. }
  106. /*
  107. * Выводим влажность на нижние индикаторы
  108. */
  109. void showHumidity(void) {
  110. // ...
  111. }
  112. /*
  113. * Выводим дату на верхние индикаторы [DD MM]
  114. */
  115. void showDate(void) {
  116. // ...
  117. }
  118. /*
  119. * Автоматическая регулировка яркости индикаторов
  120. * GY-49 (MAX44009)
  121. */
  122. void setBright(void) {
  123. // ...
  124. }
  125. /**
  126. * @brief Start Sensor measure.
  127. */
  128. void RequestData(void) {
  129. if (sensor.IsReadyToRequest()) {
  130. sensor.StartMeasure();
  131. tmpTimer.initializeMs(600, GetData).startOnce();
  132. } else {
  133. Serial.println("Sensor: not ready to request.");
  134. }
  135. }
  136. /**
  137. * @brief Get data from Temperature/Humidity Sensor.
  138. */
  139. void GetData(void) {
  140. ahtxx_t data;
  141. sensor.GetData(&data);
  142. if (data.Error != St_OK) {
  143. Serial.println("Sensor: Data error!");
  144. return;
  145. }
  146. SensorH = data.Humidity / 10;
  147. SensorT = data.Temperature / 10;
  148. Serial.print("Humidity: ");
  149. Serial.print(SensorH);
  150. Serial.print("%. Temperature: ");
  151. Serial.print(SensorT);
  152. Serial.println("*C");
  153. }
  154. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel)
  155. {
  156. debugf("connected");
  157. WifiAccessPoint.enable(false);
  158. }
  159. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
  160. {
  161. Serial.print("Got IP address: ");
  162. Serial.println(ip);
  163. // Restart main screen output
  164. procTimer.restart();
  165. displayTimer.restart();
  166. // start NTP Client there?
  167. startWebServer();
  168. }
  169. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
  170. {
  171. debugf("connection FAILED: %s", WifiEvents.getDisconnectReasonDesc(reason).c_str());
  172. WifiAccessPoint.config("ClockConfig", "", AUTH_OPEN);
  173. WifiAccessPoint.enable(true);
  174. // Stop main screen output
  175. procTimer.stop();
  176. displayTimer.stop();
  177. Serial.println("WiFi ClockConfig");
  178. Serial.println(WifiAccessPoint.getIP());
  179. startWebServer();
  180. WifiStation.disconnect();
  181. WifiStation.connect();
  182. }
  183. /**
  184. * @brief NTP Client
  185. */
  186. void onNtpReceive(NtpClient& client, time_t timestamp)
  187. {
  188. SystemClock.setTime(timestamp, eTZ_UTC);
  189. NTPLastUpdate = SystemClock.now();
  190. Serial.println("*** Time synchronized OK! ***"); // DEBUG
  191. }