Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /*
  2. SYSTEM MODULE
  3. Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <EEPROM_Rotate.h>
  6. // -----------------------------------------------------------------------------
  7. #if WIFI_SLEEP_MODE != WIFI_NONE_SLEEP
  8. unsigned long _loop_delay = 0;
  9. #endif
  10. bool _system_send_heartbeat = false;
  11. unsigned char _heartbeat_mode = HEARTBEAT_MODE;
  12. unsigned long _heartbeat_interval = HEARTBEAT_INTERVAL;
  13. // Calculated load average 0 to 100;
  14. unsigned short int _load_average = 100;
  15. // -----------------------------------------------------------------------------
  16. #if SYSTEM_CHECK_ENABLED
  17. // Call this method on boot with start=true to increase the crash counter
  18. // Call it again once the system is stable to decrease the counter
  19. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  20. // setting _systemOK = false;
  21. //
  22. // An unstable system will only have serial access, WiFi in AP mode and OTA
  23. bool _systemStable = true;
  24. void systemCheck(bool stable) {
  25. unsigned char value = EEPROMr.read(EEPROM_CRASH_COUNTER);
  26. if (stable) {
  27. value = 0;
  28. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  29. } else {
  30. if (++value > SYSTEM_CHECK_MAX) {
  31. _systemStable = false;
  32. value = 0;
  33. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  34. }
  35. }
  36. EEPROMr.write(EEPROM_CRASH_COUNTER, value);
  37. eepromCommit();
  38. }
  39. bool systemCheck() {
  40. return _systemStable;
  41. }
  42. void systemCheckLoop() {
  43. static bool checked = false;
  44. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  45. // Check system as stable
  46. systemCheck(true);
  47. checked = true;
  48. }
  49. }
  50. #endif
  51. // -----------------------------------------------------------------------------
  52. void systemSendHeartbeat() {
  53. _system_send_heartbeat = true;
  54. }
  55. bool systemGetHeartbeat() {
  56. return _system_send_heartbeat;
  57. }
  58. #if WIFI_SLEEP_MODE != WIFI_NONE_SLEEP
  59. unsigned long systemLoopDelay() {
  60. return _loop_delay;
  61. }
  62. #endif
  63. unsigned long systemLoadAverage() {
  64. return _load_average;
  65. }
  66. void _systemSetupHeartbeat() {
  67. _heartbeat_mode = getSetting("hbMode", HEARTBEAT_MODE).toInt();
  68. _heartbeat_interval = getSetting("hbInterval", HEARTBEAT_INTERVAL).toInt();
  69. }
  70. #if WEB_SUPPORT
  71. bool _systemWebSocketOnReceive(const char * key, JsonVariant& value) {
  72. return (strncmp(key, "hb", 2) == 0);
  73. }
  74. #endif
  75. void systemLoop() {
  76. // -------------------------------------------------------------------------
  77. // User requested reset
  78. // -------------------------------------------------------------------------
  79. if (checkNeedsReset()) {
  80. reset();
  81. }
  82. // -------------------------------------------------------------------------
  83. // Check system stability
  84. // -------------------------------------------------------------------------
  85. #if SYSTEM_CHECK_ENABLED
  86. systemCheckLoop();
  87. #endif
  88. // -------------------------------------------------------------------------
  89. // Heartbeat
  90. // -------------------------------------------------------------------------
  91. if (_system_send_heartbeat && _heartbeat_mode == HEARTBEAT_ONCE) {
  92. heartbeat();
  93. _system_send_heartbeat = false;
  94. } else if (_heartbeat_mode == HEARTBEAT_REPEAT || _heartbeat_mode == HEARTBEAT_REPEAT_STATUS) {
  95. static unsigned long last_hbeat = 0;
  96. #if NTP_SUPPORT
  97. if ((_system_send_heartbeat && ntpSynced()) || (millis() - last_hbeat > _heartbeat_interval * 1000)) {
  98. #else
  99. if (_system_send_heartbeat || (millis() - last_hbeat > _heartbeat_interval * 1000)) {
  100. #endif
  101. last_hbeat = millis();
  102. heartbeat();
  103. _system_send_heartbeat = false;
  104. }
  105. }
  106. // -------------------------------------------------------------------------
  107. // Load Average calculation
  108. // -------------------------------------------------------------------------
  109. static unsigned long last_loadcheck = 0;
  110. static unsigned long load_counter_temp = 0;
  111. load_counter_temp++;
  112. if (millis() - last_loadcheck > LOADAVG_INTERVAL) {
  113. static unsigned long load_counter = 0;
  114. static unsigned long load_counter_max = 1;
  115. load_counter = load_counter_temp;
  116. load_counter_temp = 0;
  117. if (load_counter > load_counter_max) {
  118. load_counter_max = load_counter;
  119. }
  120. _load_average = 100 - (100 * load_counter / load_counter_max);
  121. last_loadcheck = millis();
  122. }
  123. // -------------------------------------------------------------------------
  124. // Power saving delay
  125. // -------------------------------------------------------------------------
  126. #if WIFI_SLEEP_MODE != WIFI_NONE_SLEEP
  127. delay(_loop_delay);
  128. #endif
  129. }
  130. void _systemSetupSpecificHardware() {
  131. //The ESPLive has an ADC MUX which needs to be configured.
  132. #if defined(MANCAVEMADE_ESPLIVE)
  133. pinMode(16, OUTPUT);
  134. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  135. #endif
  136. // These devices use the hardware UART
  137. // to communicate to secondary microcontrollers
  138. #if defined(ITEAD_SONOFF_RFBRIDGE) || defined(ITEAD_SONOFF_DUAL) || (RELAY_PROVIDER == RELAY_PROVIDER_STM)
  139. Serial.begin(SERIAL_BAUDRATE);
  140. #endif
  141. }
  142. void systemSetup() {
  143. #if SPIFFS_SUPPORT
  144. SPIFFS.begin();
  145. #endif
  146. // Question system stability
  147. #if SYSTEM_CHECK_ENABLED
  148. systemCheck(false);
  149. #endif
  150. #if WEB_SUPPORT
  151. wsOnReceiveRegister(_systemWebSocketOnReceive);
  152. #endif
  153. // Init device-specific hardware
  154. _systemSetupSpecificHardware();
  155. // Cache loop delay value to speed things (recommended max 250ms)
  156. #if WIFI_SLEEP_MODE != WIFI_NONE_SLEEP
  157. _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  158. _loop_delay = constrain(_loop_delay, 0, 300);
  159. #endif
  160. // Register Loop
  161. espurnaRegisterLoop(systemLoop);
  162. // Cache Heartbeat values
  163. _systemSetupHeartbeat();
  164. }