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.

185 lines
5.2 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. influxDBSend(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. influxDBSend(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. }
  102. // -----------------------------------------------------------------------------
  103. void customReset(unsigned char status) {
  104. EEPROM.write(EEPROM_CUSTOM_RESET, status);
  105. EEPROM.commit();
  106. }
  107. unsigned char customReset() {
  108. static unsigned char status = 255;
  109. if (status == 255) {
  110. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  111. if (status > 0) customReset(0);
  112. if (status > CUSTOM_RESET_MAX) status = 0;
  113. }
  114. return status;
  115. }
  116. // -----------------------------------------------------------------------------
  117. // Call this method on boot with start=true to increase the crash counter
  118. // Call it again once the system is stable to decrease the counter
  119. // If the counter reaches CRASH_COUNT_MAX then the system is flagged as unstable
  120. // setting _systemOK = false;
  121. //
  122. // An unstable system will only have serial access, WiFi in AP mode and OTA
  123. bool _systemStable = true;
  124. void systemCheck(bool stable) {
  125. unsigned char value = EEPROM.read(EEPROM_CRASH_COUNTER);
  126. if (stable) {
  127. value = 0;
  128. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  129. } else {
  130. if (++value > CRASH_COUNT_MAX) {
  131. _systemStable = false;
  132. value = 0;
  133. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  134. }
  135. }
  136. EEPROM.write(EEPROM_CRASH_COUNTER, value);
  137. EEPROM.commit();
  138. }
  139. bool systemCheck() {
  140. return _systemStable;
  141. }
  142. // -----------------------------------------------------------------------------
  143. char * ltrim(char * s) {
  144. char *p = s;
  145. while ((unsigned char) *p == ' ') ++p;
  146. return p;
  147. }
  148. double roundTo(double num, unsigned char positions) {
  149. double multiplier = 1;
  150. while (positions-- > 0) multiplier *= 10;
  151. return round(num * multiplier) / multiplier;
  152. }