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.

183 lines
5.4 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. #define DUMP_SETTING(X, Y) DEBUG_MSG_P(PSTR(" %s: %s\n"), X, String(Y).c_str())
  42. void initDump() {
  43. DEBUG_MSG_P(PSTR("\n[INIT] Current settings:\n\n"));
  44. DUMP_SETTING("MANUFACTURER", MANUFACTURER);
  45. DUMP_SETTING("DEVICE", DEVICE);
  46. DEBUG_MSG_P(PSTR(" ---------------------------\n"));
  47. DUMP_SETTING("ALEXA_SUPPORT", ALEXA_SUPPORT);
  48. DUMP_SETTING("ANALOG_SUPPORT", ANALOG_SUPPORT);
  49. DUMP_SETTING("DEBUG_SERIAL_SUPPORT", DEBUG_SERIAL_SUPPORT);
  50. DUMP_SETTING("DEBUG_UDP_SUPPORT", DEBUG_UDP_SUPPORT);
  51. DUMP_SETTING("DHT_SUPPORT", DHT_SUPPORT);
  52. DUMP_SETTING("DOMOTICZ_SUPPORT", DOMOTICZ_SUPPORT);
  53. DUMP_SETTING("DS18B20_SUPPORT", DS18B20_SUPPORT);
  54. DUMP_SETTING("EMON_SUPPORT", EMON_SUPPORT);
  55. DUMP_SETTING("HOMEASSISTANT_SUPPORT", HOMEASSISTANT_SUPPORT);
  56. DUMP_SETTING("I2C_SUPPORT", I2C_SUPPORT);
  57. DUMP_SETTING("INFLUXDB_SUPPORT", INFLUXDB_SUPPORT);
  58. DUMP_SETTING("MDNS_SUPPORT", MDNS_SUPPORT);
  59. DUMP_SETTING("NOFUSS_SUPPORT", NOFUSS_SUPPORT);
  60. DUMP_SETTING("NTP_SUPPORT", NTP_SUPPORT);
  61. DUMP_SETTING("RF_SUPPORT", RF_SUPPORT);
  62. DUMP_SETTING("SPIFFS_SUPPORT", SPIFFS_SUPPORT);
  63. DUMP_SETTING("TERMINAL_SUPPORT", TERMINAL_SUPPORT);
  64. DUMP_SETTING("WEB_SUPPORT", WEB_SUPPORT);
  65. DEBUG_MSG_P(PSTR(" ---------------------------\n"));
  66. DUMP_SETTING("LIGHT_PROVIDER", LIGHT_PROVIDER);
  67. DUMP_SETTING("MQTT_AUTOCONNECT", MQTT_AUTOCONNECT);
  68. DUMP_SETTING("MQTT_USE_ASYNC", MQTT_USE_ASYNC);
  69. }
  70. void heartbeat() {
  71. unsigned long uptime_seconds = getUptime();
  72. unsigned int free_heap = ESP.getFreeHeap();
  73. #if NTP_SUPPORT
  74. DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
  75. #endif
  76. if (!mqttConnected()) {
  77. DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
  78. DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
  79. #if ADC_VCC_ENABLED
  80. DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc());
  81. #endif
  82. }
  83. #if (HEARTBEAT_REPORT_INTERVAL)
  84. mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
  85. #endif
  86. #if (HEARTBEAT_REPORT_APP)
  87. mqttSend(MQTT_TOPIC_APP, APP_NAME);
  88. #endif
  89. #if (HEARTBEAT_REPORT_VERSION)
  90. mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
  91. #endif
  92. #if (HEARTBEAT_REPORT_HOSTNAME)
  93. mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
  94. #endif
  95. #if (HEARTBEAT_REPORT_IP)
  96. mqttSend(MQTT_TOPIC_IP, getIP().c_str());
  97. #endif
  98. #if (HEARTBEAT_REPORT_MAC)
  99. mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
  100. #endif
  101. #if (HEARTBEAT_REPORT_RSSI)
  102. mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
  103. #endif
  104. #if (HEARTBEAT_REPORT_UPTIME)
  105. mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  106. #if INFLUXDB_SUPPORT
  107. influxDBSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  108. #endif
  109. #endif
  110. #if (HEARTBEAT_REPORT_FREEHEAP)
  111. mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  112. #if INFLUXDB_SUPPORT
  113. influxDBSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  114. #endif
  115. #endif
  116. #if (HEARTBEAT_REPORT_RELAY)
  117. relayMQTT();
  118. #endif
  119. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
  120. lightMQTT();
  121. #endif
  122. #if (HEARTBEAT_REPORT_VCC)
  123. #if ADC_VCC_ENABLED
  124. mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
  125. #endif
  126. #endif
  127. #if (HEARTBEAT_REPORT_STATUS)
  128. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  129. #endif
  130. }
  131. void customReset(unsigned char status) {
  132. EEPROM.write(EEPROM_CUSTOM_RESET, status);
  133. EEPROM.commit();
  134. }
  135. unsigned char customReset() {
  136. static unsigned char status = 255;
  137. if (status == 255) {
  138. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  139. if (status > 0) customReset(0);
  140. if (status > CUSTOM_RESET_MAX) status = 0;
  141. }
  142. return status;
  143. }
  144. char * ltrim(char * s) {
  145. char *p = s;
  146. while ((unsigned char) *p == ' ') ++p;
  147. return p;
  148. }