application.cpp 5.4 KB

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