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.

281 lines
7.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("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_DATETIME) & (NTP_SUPPORT)
  98. mqttSend(MQTT_TOPIC_DATETIME, String(ntpDateTime()).c_str());
  99. #endif
  100. #if (HEARTBEAT_REPORT_FREEHEAP)
  101. mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  102. #endif
  103. #if (HEARTBEAT_REPORT_RELAY)
  104. relayMQTT();
  105. #endif
  106. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
  107. lightMQTT();
  108. #endif
  109. #if (HEARTBEAT_REPORT_VCC)
  110. #if ADC_VCC_ENABLED
  111. mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
  112. #endif
  113. #endif
  114. #if (HEARTBEAT_REPORT_STATUS)
  115. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  116. #endif
  117. bool serial = !mqttConnected();
  118. #else
  119. bool serial = true;
  120. #endif
  121. // -------------------------------------------------------------------------
  122. // Serial
  123. // -------------------------------------------------------------------------
  124. if (serial) {
  125. DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
  126. DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
  127. #if ADC_VCC_ENABLED
  128. DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc());
  129. #endif
  130. }
  131. #if NTP_SUPPORT
  132. DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
  133. #endif
  134. // -------------------------------------------------------------------------
  135. // InfluxDB
  136. // -------------------------------------------------------------------------
  137. #if INFLUXDB_SUPPORT
  138. #if (HEARTBEAT_REPORT_UPTIME)
  139. idbSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  140. #endif
  141. #if (HEARTBEAT_REPORT_FREEHEAP)
  142. idbSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  143. #endif
  144. #endif
  145. // -------------------------------------------------------------------------
  146. // WebSockets
  147. // -------------------------------------------------------------------------
  148. #if WEB_SUPPORT
  149. #if NTP_SUPPORT
  150. {
  151. char buffer[200];
  152. snprintf_P(
  153. buffer,
  154. sizeof(buffer) - 1,
  155. PSTR("{\"time\": \"%s\", \"uptime\": %lu, \"heap\": %lu}"),
  156. ntpDateTime().c_str(), uptime_seconds, free_heap
  157. );
  158. wsSend(buffer);
  159. }
  160. #endif
  161. #endif
  162. }
  163. // -----------------------------------------------------------------------------
  164. unsigned char resetReason() {
  165. static unsigned char status = 255;
  166. if (status == 255) {
  167. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  168. if (status > 0) resetReason(0);
  169. if (status > CUSTOM_RESET_MAX) status = 0;
  170. }
  171. return status;
  172. }
  173. void resetReason(unsigned char reason) {
  174. EEPROM.write(EEPROM_CUSTOM_RESET, reason);
  175. EEPROM.commit();
  176. }
  177. void reset(unsigned char reason) {
  178. resetReason(reason);
  179. ESP.restart();
  180. }
  181. void deferredReset(unsigned long delay, unsigned char reason) {
  182. _defer_reset.once_ms(delay, reset, reason);
  183. }
  184. // -----------------------------------------------------------------------------
  185. #if SYSTEM_CHECK_ENABLED
  186. // Call this method on boot with start=true to increase the crash counter
  187. // Call it again once the system is stable to decrease the counter
  188. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  189. // setting _systemOK = false;
  190. //
  191. // An unstable system will only have serial access, WiFi in AP mode and OTA
  192. bool _systemStable = true;
  193. void systemCheck(bool stable) {
  194. unsigned char value = EEPROM.read(EEPROM_CRASH_COUNTER);
  195. if (stable) {
  196. value = 0;
  197. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  198. } else {
  199. if (++value > SYSTEM_CHECK_MAX) {
  200. _systemStable = false;
  201. value = 0;
  202. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  203. }
  204. }
  205. EEPROM.write(EEPROM_CRASH_COUNTER, value);
  206. EEPROM.commit();
  207. }
  208. bool systemCheck() {
  209. return _systemStable;
  210. }
  211. void systemCheckLoop() {
  212. static bool checked = false;
  213. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  214. // Check system as stable
  215. systemCheck(true);
  216. checked = true;
  217. }
  218. }
  219. #endif
  220. // -----------------------------------------------------------------------------
  221. char * ltrim(char * s) {
  222. char *p = s;
  223. while ((unsigned char) *p == ' ') ++p;
  224. return p;
  225. }
  226. double roundTo(double num, unsigned char positions) {
  227. double multiplier = 1;
  228. while (positions-- > 0) multiplier *= 10;
  229. return round(num * multiplier) / multiplier;
  230. }
  231. void nice_delay(unsigned long ms) {
  232. unsigned long start = millis();
  233. while (millis() - start < ms) delay(1);
  234. }