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.

265 lines
7.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. // -------------------------------------------------------------------------
  63. // MQTT
  64. // -------------------------------------------------------------------------
  65. #if MQTT_SUPPORT
  66. #if (HEARTBEAT_REPORT_INTERVAL)
  67. mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
  68. #endif
  69. #if (HEARTBEAT_REPORT_APP)
  70. mqttSend(MQTT_TOPIC_APP, APP_NAME);
  71. #endif
  72. #if (HEARTBEAT_REPORT_VERSION)
  73. mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
  74. #endif
  75. #if (HEARTBEAT_REPORT_HOSTNAME)
  76. mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
  77. #endif
  78. #if (HEARTBEAT_REPORT_IP)
  79. mqttSend(MQTT_TOPIC_IP, getIP().c_str());
  80. #endif
  81. #if (HEARTBEAT_REPORT_MAC)
  82. mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
  83. #endif
  84. #if (HEARTBEAT_REPORT_RSSI)
  85. mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
  86. #endif
  87. #if (HEARTBEAT_REPORT_UPTIME)
  88. mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  89. #endif
  90. #if (HEARTBEAT_REPORT_FREEHEAP)
  91. mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  92. #endif
  93. #if (HEARTBEAT_REPORT_RELAY)
  94. relayMQTT();
  95. #endif
  96. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
  97. lightMQTT();
  98. #endif
  99. #if (HEARTBEAT_REPORT_VCC)
  100. #if ADC_VCC_ENABLED
  101. mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
  102. #endif
  103. #endif
  104. #if (HEARTBEAT_REPORT_STATUS)
  105. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  106. #endif
  107. bool serial = !mqttConnected();
  108. #else
  109. bool serial = true;
  110. #endif
  111. // -------------------------------------------------------------------------
  112. // Serial
  113. // -------------------------------------------------------------------------
  114. if (serial) {
  115. DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
  116. DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
  117. #if ADC_VCC_ENABLED
  118. DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc());
  119. #endif
  120. }
  121. #if NTP_SUPPORT
  122. DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
  123. #endif
  124. // -------------------------------------------------------------------------
  125. // InfluxDB
  126. // -------------------------------------------------------------------------
  127. #if INFLUXDB_SUPPORT
  128. #if (HEARTBEAT_REPORT_UPTIME)
  129. idbSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  130. #endif
  131. #if (HEARTBEAT_REPORT_FREEHEAP)
  132. idbSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  133. #endif
  134. #endif
  135. // -------------------------------------------------------------------------
  136. // WebSockets
  137. // -------------------------------------------------------------------------
  138. #if WEB_SUPPORT
  139. #if NTP_SUPPORT
  140. {
  141. char buffer[200];
  142. snprintf_P(
  143. buffer,
  144. sizeof(buffer) - 1,
  145. PSTR("{\"time\": \"%s\", \"uptime\": %lu, \"heap\": %lu}"),
  146. ntpDateTime().c_str(), uptime_seconds, free_heap
  147. );
  148. wsSend(buffer);
  149. }
  150. #endif
  151. #endif
  152. }
  153. // -----------------------------------------------------------------------------
  154. unsigned char resetReason() {
  155. static unsigned char status = 255;
  156. if (status == 255) {
  157. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  158. if (status > 0) resetReason(0);
  159. if (status > CUSTOM_RESET_MAX) status = 0;
  160. }
  161. return status;
  162. }
  163. void resetReason(unsigned char reason) {
  164. EEPROM.write(EEPROM_CUSTOM_RESET, reason);
  165. EEPROM.commit();
  166. }
  167. void reset(unsigned char reason) {
  168. resetReason(reason);
  169. ESP.restart();
  170. }
  171. void deferredReset(unsigned long delay, unsigned char reason) {
  172. _defer_reset.once_ms(delay, reset, reason);
  173. }
  174. // -----------------------------------------------------------------------------
  175. #if SYSTEM_CHECK_ENABLED
  176. // Call this method on boot with start=true to increase the crash counter
  177. // Call it again once the system is stable to decrease the counter
  178. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  179. // setting _systemOK = false;
  180. //
  181. // An unstable system will only have serial access, WiFi in AP mode and OTA
  182. bool _systemStable = true;
  183. void systemCheck(bool stable) {
  184. unsigned char value = EEPROM.read(EEPROM_CRASH_COUNTER);
  185. if (stable) {
  186. value = 0;
  187. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  188. } else {
  189. if (++value > SYSTEM_CHECK_MAX) {
  190. _systemStable = false;
  191. value = 0;
  192. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  193. }
  194. }
  195. EEPROM.write(EEPROM_CRASH_COUNTER, value);
  196. EEPROM.commit();
  197. }
  198. bool systemCheck() {
  199. return _systemStable;
  200. }
  201. void systemCheckLoop() {
  202. static bool checked = false;
  203. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  204. // Check system as stable
  205. systemCheck(true);
  206. checked = true;
  207. }
  208. }
  209. #endif
  210. // -----------------------------------------------------------------------------
  211. char * ltrim(char * s) {
  212. char *p = s;
  213. while ((unsigned char) *p == ' ') ++p;
  214. return p;
  215. }
  216. double roundTo(double num, unsigned char positions) {
  217. double multiplier = 1;
  218. while (positions-- > 0) multiplier *= 10;
  219. return round(num * multiplier) / multiplier;
  220. }