application.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. Timer procTimer, procRTimer;
  9. Timer displayTimer, tmpTimer;
  10. Timer showHighTimer, showLowTimer;
  11. Timer brightTimer;
  12. // Sensors string values
  13. float SensorT, SensorH, SensorHI, SensorCR;
  14. String StrCF;
  15. // Time values
  16. time_t Time, NTPLastUpdate;
  17. DateTime dt;
  18. void process(void);
  19. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel);
  20. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason);
  21. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway);
  22. void showWatch(void);
  23. void showTime(void);
  24. void showTemperature(void);
  25. void showHumidity(void);
  26. void setBright(void);
  27. // NTP Client
  28. void onNtpReceive(NtpClient& client, time_t timestamp);
  29. NtpClient ntpClient("ntp.time.in.ua", 1500, onNtpReceive); // every 15 min
  30. void init()
  31. {
  32. spiffs_mount(); // Mount file system, in order to work with files
  33. Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
  34. Serial.systemDebugOutput(false); // Debug output to serial
  35. Serial.println("Wall Segment Clock");
  36. ActiveConfig = loadConfig();
  37. // set timezone hourly difference to UTC
  38. SystemClock.setTimeZone(ActiveConfig.AddTZ);
  39. WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword);
  40. WifiStation.enable(true);
  41. WifiAccessPoint.enable(false);
  42. WifiEvents.onStationConnect(connectOk);
  43. WifiEvents.onStationDisconnect(connectFail);
  44. WifiEvents.onStationGotIP(gotIP);
  45. // Sensors start
  46. //dht.begin();
  47. // polling sensors - once a minute?
  48. procTimer.initializeMs(60000, process).start();
  49. process();
  50. // Low LED output
  51. TM1650_Init();
  52. brightTimer.initializeMs(1000, setBright).start();
  53. // обновление экрана два раза в секунду
  54. displayTimer.initializeMs(500, showWatch).start();
  55. }
  56. void showWatch(void) {
  57. static time_t oldTime;
  58. Time = SystemClock.now();
  59. dt.setTime(Time);
  60. /*
  61. * теперь в dt у нас следующее:
  62. * int8_t Hour;
  63. * int8_t Minute;
  64. * int8_t Second;
  65. * int16_t Milliseconds;
  66. * int8_t Day;
  67. * int8_t DayofWeek; -- Sunday is day 0
  68. * int8_t Month; // Jan is month 0
  69. * int16_t Year; // Full Year numer
  70. */
  71. if (oldTime == Time) {
  72. // Старая секунда - нужно гасить точки
  73. TM1650_DotRes(Dig_2);
  74. } else {
  75. // Новая секунда
  76. oldTime = Time;
  77. TM1650_Out(dt.Hour >> 4, dt.Hour & 0xf, dt.Minute >>4, dt.Minute & 0xf);
  78. TM1650_DotSet(Dig_2);
  79. }
  80. }
  81. /*
  82. * Выводим текущее время [HH MM] на верхние индикаторы
  83. */
  84. void showTime(void) {
  85. static uint8_t oldHour = 0xFF, oldMinute = 0xFF;
  86. if (oldMinute != dt.Minute) {
  87. oldMinute = dt.Minute;
  88. // ...
  89. if (oldHour != dt.Hour) {
  90. oldHour = dt.Hour;
  91. // ...
  92. } // new hour
  93. } // new minute
  94. }
  95. /*
  96. * Выводим температуру на нижние индикаторы
  97. */
  98. void showTemperature(void) {
  99. // ...
  100. }
  101. /*
  102. * Выводим влажность на нижние индикаторы
  103. */
  104. void showHumidity(void) {
  105. // ...
  106. }
  107. /*
  108. * Выводим дату на верхние индикаторы [DD MM]
  109. */
  110. void showDate(void) {
  111. // ...
  112. }
  113. /*
  114. * Автоматическая регулировка яркости индикаторов
  115. * GY-49
  116. */
  117. void setBright(void) {
  118. // ...
  119. }
  120. /**
  121. * @brief Get data from Temperature/Humidity Sensor
  122. * Currently planed AHT10. Пока заглушка.
  123. */
  124. void process() {
  125. float t = 25.0;
  126. float h = 60.5;
  127. Serial.print("Humidity: ");
  128. Serial.print(h);
  129. Serial.print("%. Temperature: ");
  130. Serial.print(t);
  131. Serial.println("*C");
  132. }
  133. void connectOk(const String& SSID, MacAddress bssid, uint8_t channel)
  134. {
  135. debugf("connected");
  136. WifiAccessPoint.enable(false);
  137. }
  138. void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
  139. {
  140. Serial.print("Got IP address: ");
  141. Serial.println(ip);
  142. // Restart main screen output
  143. procTimer.restart();
  144. displayTimer.restart();
  145. // start NTP Client there?
  146. startWebServer();
  147. }
  148. void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
  149. {
  150. debugf("connection FAILED: %s", WifiEvents.getDisconnectReasonDesc(reason).c_str());
  151. WifiAccessPoint.config("ClockConfig", "", AUTH_OPEN);
  152. WifiAccessPoint.enable(true);
  153. // Stop main screen output
  154. procTimer.stop();
  155. displayTimer.stop();
  156. Serial.println("WiFi ClockConfig");
  157. Serial.println(WifiAccessPoint.getIP());
  158. startWebServer();
  159. WifiStation.disconnect();
  160. WifiStation.connect();
  161. }
  162. /**
  163. * @brief NTP Client
  164. */
  165. void onNtpReceive(NtpClient& client, time_t timestamp)
  166. {
  167. SystemClock.setTime(timestamp, eTZ_UTC);
  168. NTPLastUpdate = SystemClock.now();
  169. Serial.println("*** Time synchronized OK! ***"); // DEBUG
  170. }