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.

144 lines
3.9 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. void customReset(unsigned char status) {
  103. EEPROM.write(EEPROM_CUSTOM_RESET, status);
  104. EEPROM.commit();
  105. }
  106. unsigned char customReset() {
  107. static unsigned char status = 255;
  108. if (status == 255) {
  109. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  110. if (status > 0) customReset(0);
  111. if (status > CUSTOM_RESET_MAX) status = 0;
  112. }
  113. return status;
  114. }
  115. char * ltrim(char * s) {
  116. char *p = s;
  117. while ((unsigned char) *p == ' ') ++p;
  118. return p;
  119. }