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.

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