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.

210 lines
5.7 KiB

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