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.

222 lines
5.9 KiB

  1. /*
  2. UTILS MODULE
  3. Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <Ticker.h>
  6. Ticker _defer_reset;
  7. String getIdentifier() {
  8. char buffer[20];
  9. snprintf_P(buffer, sizeof(buffer), PSTR("%s_%06X"), DEVICE, ESP.getChipId());
  10. return String(buffer);
  11. }
  12. String buildTime() {
  13. const char time_now[] = __TIME__; // hh:mm:ss
  14. unsigned int hour = atoi(&time_now[0]);
  15. unsigned int minute = atoi(&time_now[3]);
  16. unsigned int second = atoi(&time_now[6]);
  17. const char date_now[] = __DATE__; // Mmm dd yyyy
  18. const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  19. unsigned int month = 0;
  20. for ( int i = 0; i < 12; i++ ) {
  21. if (strncmp(date_now, months[i], 3) == 0 ) {
  22. month = i + 1;
  23. break;
  24. }
  25. }
  26. unsigned int day = atoi(&date_now[3]);
  27. unsigned int year = atoi(&date_now[7]);
  28. char buffer[20];
  29. snprintf_P(
  30. buffer, sizeof(buffer), PSTR("%04d/%02d/%02d %02d:%02d:%02d"),
  31. year, month, day, hour, minute, second
  32. );
  33. return String(buffer);
  34. }
  35. unsigned long getUptime() {
  36. static unsigned long last_uptime = 0;
  37. static unsigned char uptime_overflows = 0;
  38. if (millis() < last_uptime) ++uptime_overflows;
  39. last_uptime = millis();
  40. unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000);
  41. return uptime_seconds;
  42. }
  43. void heartbeat() {
  44. unsigned long uptime_seconds = getUptime();
  45. unsigned int free_heap = ESP.getFreeHeap();
  46. #if NTP_SUPPORT
  47. DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
  48. #endif
  49. if (!mqttConnected()) {
  50. DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
  51. DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
  52. #if ADC_VCC_ENABLED
  53. DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc());
  54. #endif
  55. }
  56. #if (HEARTBEAT_REPORT_INTERVAL)
  57. mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
  58. #endif
  59. #if (HEARTBEAT_REPORT_APP)
  60. mqttSend(MQTT_TOPIC_APP, APP_NAME);
  61. #endif
  62. #if (HEARTBEAT_REPORT_VERSION)
  63. mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
  64. #endif
  65. #if (HEARTBEAT_REPORT_HOSTNAME)
  66. mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
  67. #endif
  68. #if (HEARTBEAT_REPORT_IP)
  69. mqttSend(MQTT_TOPIC_IP, getIP().c_str());
  70. #endif
  71. #if (HEARTBEAT_REPORT_MAC)
  72. mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
  73. #endif
  74. #if (HEARTBEAT_REPORT_RSSI)
  75. mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
  76. #endif
  77. #if (HEARTBEAT_REPORT_UPTIME)
  78. mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  79. #if INFLUXDB_SUPPORT
  80. idbSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  81. #endif
  82. #endif
  83. #if (HEARTBEAT_REPORT_FREEHEAP)
  84. mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  85. #if INFLUXDB_SUPPORT
  86. idbSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  87. #endif
  88. #endif
  89. #if (HEARTBEAT_REPORT_RELAY)
  90. relayMQTT();
  91. #endif
  92. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
  93. lightMQTT();
  94. #endif
  95. #if (HEARTBEAT_REPORT_VCC)
  96. #if ADC_VCC_ENABLED
  97. mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
  98. #endif
  99. #endif
  100. #if (HEARTBEAT_REPORT_STATUS)
  101. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  102. #endif
  103. // Send info to websocket clients
  104. {
  105. char buffer[200];
  106. snprintf_P(
  107. buffer,
  108. sizeof(buffer) - 1,
  109. PSTR("{\"time\": \"%s\", \"uptime\": %lu, \"heap\": %lu}"),
  110. ntpDateTime().c_str(), uptime_seconds, free_heap
  111. );
  112. wsSend(buffer);
  113. }
  114. }
  115. // -----------------------------------------------------------------------------
  116. unsigned char resetReason() {
  117. static unsigned char status = 255;
  118. if (status == 255) {
  119. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  120. if (status > 0) resetReason(0);
  121. if (status > CUSTOM_RESET_MAX) status = 0;
  122. }
  123. return status;
  124. }
  125. void resetReason(unsigned char reason) {
  126. EEPROM.write(EEPROM_CUSTOM_RESET, reason);
  127. EEPROM.commit();
  128. }
  129. void reset(unsigned char reason) {
  130. resetReason(reason);
  131. ESP.restart();
  132. }
  133. void deferredReset(unsigned long delay, unsigned char reason) {
  134. _defer_reset.once_ms(delay, reset, reason);
  135. }
  136. // -----------------------------------------------------------------------------
  137. #if SYSTEM_CHECK_ENABLED
  138. // Call this method on boot with start=true to increase the crash counter
  139. // Call it again once the system is stable to decrease the counter
  140. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  141. // setting _systemOK = false;
  142. //
  143. // An unstable system will only have serial access, WiFi in AP mode and OTA
  144. bool _systemStable = true;
  145. void systemCheck(bool stable) {
  146. unsigned char value = EEPROM.read(EEPROM_CRASH_COUNTER);
  147. if (stable) {
  148. value = 0;
  149. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  150. } else {
  151. if (++value > SYSTEM_CHECK_MAX) {
  152. _systemStable = false;
  153. value = 0;
  154. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  155. }
  156. }
  157. EEPROM.write(EEPROM_CRASH_COUNTER, value);
  158. EEPROM.commit();
  159. }
  160. bool systemCheck() {
  161. return _systemStable;
  162. }
  163. void systemCheckLoop() {
  164. static bool checked = false;
  165. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  166. // Check system as stable
  167. systemCheck(true);
  168. checked = true;
  169. }
  170. }
  171. #endif
  172. // -----------------------------------------------------------------------------
  173. char * ltrim(char * s) {
  174. char *p = s;
  175. while ((unsigned char) *p == ' ') ++p;
  176. return p;
  177. }
  178. double roundTo(double num, unsigned char positions) {
  179. double multiplier = 1;
  180. while (positions-- > 0) multiplier *= 10;
  181. return round(num * multiplier) / multiplier;
  182. }