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.

240 lines
6.3 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 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. {
  121. char buffer[200];
  122. snprintf_P(
  123. buffer,
  124. sizeof(buffer) - 1,
  125. PSTR("{\"time\": \"%s\", \"uptime\": %lu, \"heap\": %lu}"),
  126. ntpDateTime().c_str(), uptime_seconds, free_heap
  127. );
  128. wsSend(buffer);
  129. }
  130. }
  131. // -----------------------------------------------------------------------------
  132. unsigned char resetReason() {
  133. static unsigned char status = 255;
  134. if (status == 255) {
  135. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  136. if (status > 0) resetReason(0);
  137. if (status > CUSTOM_RESET_MAX) status = 0;
  138. }
  139. return status;
  140. }
  141. void resetReason(unsigned char reason) {
  142. EEPROM.write(EEPROM_CUSTOM_RESET, reason);
  143. EEPROM.commit();
  144. }
  145. void reset(unsigned char reason) {
  146. resetReason(reason);
  147. ESP.restart();
  148. }
  149. void deferredReset(unsigned long delay, unsigned char reason) {
  150. _defer_reset.once_ms(delay, reset, reason);
  151. }
  152. // -----------------------------------------------------------------------------
  153. #if SYSTEM_CHECK_ENABLED
  154. // Call this method on boot with start=true to increase the crash counter
  155. // Call it again once the system is stable to decrease the counter
  156. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  157. // setting _systemOK = false;
  158. //
  159. // An unstable system will only have serial access, WiFi in AP mode and OTA
  160. bool _systemStable = true;
  161. void systemCheck(bool stable) {
  162. unsigned char value = EEPROM.read(EEPROM_CRASH_COUNTER);
  163. if (stable) {
  164. value = 0;
  165. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  166. } else {
  167. if (++value > SYSTEM_CHECK_MAX) {
  168. _systemStable = false;
  169. value = 0;
  170. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  171. }
  172. }
  173. EEPROM.write(EEPROM_CRASH_COUNTER, value);
  174. EEPROM.commit();
  175. }
  176. bool systemCheck() {
  177. return _systemStable;
  178. }
  179. void systemCheckLoop() {
  180. static bool checked = false;
  181. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  182. // Check system as stable
  183. systemCheck(true);
  184. checked = true;
  185. }
  186. }
  187. #endif
  188. // -----------------------------------------------------------------------------
  189. char * ltrim(char * s) {
  190. char *p = s;
  191. while ((unsigned char) *p == ' ') ++p;
  192. return p;
  193. }
  194. double roundTo(double num, unsigned char positions) {
  195. double multiplier = 1;
  196. while (positions-- > 0) multiplier *= 10;
  197. return round(num * multiplier) / multiplier;
  198. }