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.

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