From 9046878122b6ec6f0ed23ef35143de3d11344a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 29 Aug 2017 10:09:23 +0200 Subject: [PATCH 1/3] Moving general methods to utils.ino --- code/espurna/espurna.ino | 132 --------------------------------------- code/espurna/utils.ino | 131 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 132 deletions(-) diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index e0d9e2f1..73dc44f6 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -26,138 +26,6 @@ along with this program. If not, see . // METHODS // ----------------------------------------------------------------------------- -String getIdentifier() { - char buffer[20]; - snprintf_P(buffer, sizeof(buffer), PSTR("%s_%06X"), DEVICE, ESP.getChipId()); - return String(buffer); -} - -String buildTime() { - - const char time_now[] = __TIME__; // hh:mm:ss - unsigned int hour = atoi(&time_now[0]); - unsigned int minute = atoi(&time_now[3]); - unsigned int second = atoi(&time_now[6]); - - const char date_now[] = __DATE__; // Mmm dd yyyy - const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - unsigned int month = 0; - for ( int i = 0; i < 12; i++ ) { - if (strncmp(date_now, months[i], 3) == 0 ) { - month = i + 1; - break; - } - } - unsigned int day = atoi(&date_now[3]); - unsigned int year = atoi(&date_now[7]); - - char buffer[20]; - snprintf_P( - buffer, sizeof(buffer), PSTR("%04d/%02d/%02d %02d:%02d:%02d"), - year, month, day, hour, minute, second - ); - - return String(buffer); - -} - - -unsigned long getUptime() { - - static unsigned long last_uptime = 0; - static unsigned char uptime_overflows = 0; - - if (millis() < last_uptime) ++uptime_overflows; - last_uptime = millis(); - unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000); - - return uptime_seconds; - -} - -void heartbeat() { - - unsigned long uptime_seconds = getUptime(); - unsigned int free_heap = ESP.getFreeHeap(); - - #if NTP_SUPPORT - DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str()); - #endif - - if (!mqttConnected()) { - DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds); - DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap); - #if ADC_VCC_ENABLED - DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc()); - #endif - } - - - #if (HEARTBEAT_REPORT_INTERVAL) - mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000); - #endif - #if (HEARTBEAT_REPORT_APP) - mqttSend(MQTT_TOPIC_APP, APP_NAME); - #endif - #if (HEARTBEAT_REPORT_VERSION) - mqttSend(MQTT_TOPIC_VERSION, APP_VERSION); - #endif - #if (HEARTBEAT_REPORT_HOSTNAME) - mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str()); - #endif - #if (HEARTBEAT_REPORT_IP) - mqttSend(MQTT_TOPIC_IP, getIP().c_str()); - #endif - #if (HEARTBEAT_REPORT_MAC) - mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str()); - #endif - #if (HEARTBEAT_REPORT_RSSI) - mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str()); - #endif - #if (HEARTBEAT_REPORT_UPTIME) - mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); - #if INFLUXDB_SUPPORT - influxDBSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); - #endif - #endif - #if (HEARTBEAT_REPORT_FREEHEAP) - mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); - #if INFLUXDB_SUPPORT - influxDBSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); - #endif - #endif - #if (HEARTBEAT_REPORT_RELAY) - relayMQTT(); - #endif - #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT) - lightMQTT(); - #endif - #if (HEARTBEAT_REPORT_VCC) - #if ADC_VCC_ENABLED - mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str()); - #endif - #endif - #if (HEARTBEAT_REPORT_STATUS) - mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true); - #endif - -} - -void customReset(unsigned char status) { - EEPROM.write(EEPROM_CUSTOM_RESET, status); - EEPROM.commit(); -} - -unsigned char customReset() { - static unsigned char status = 255; - if (status == 255) { - status = EEPROM.read(EEPROM_CUSTOM_RESET); - if (status > 0) customReset(0); - if (status > CUSTOM_RESET_MAX) status = 0; - } - return status; -} - void hardwareSetup() { EEPROM.begin(EEPROM_SIZE); diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index 6e41bc12..c8428cb0 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -6,6 +6,137 @@ Copyright (C) 2017 by Xose Pérez */ +String getIdentifier() { + char buffer[20]; + snprintf_P(buffer, sizeof(buffer), PSTR("%s_%06X"), DEVICE, ESP.getChipId()); + return String(buffer); +} + +String buildTime() { + + const char time_now[] = __TIME__; // hh:mm:ss + unsigned int hour = atoi(&time_now[0]); + unsigned int minute = atoi(&time_now[3]); + unsigned int second = atoi(&time_now[6]); + + const char date_now[] = __DATE__; // Mmm dd yyyy + const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + unsigned int month = 0; + for ( int i = 0; i < 12; i++ ) { + if (strncmp(date_now, months[i], 3) == 0 ) { + month = i + 1; + break; + } + } + unsigned int day = atoi(&date_now[3]); + unsigned int year = atoi(&date_now[7]); + + char buffer[20]; + snprintf_P( + buffer, sizeof(buffer), PSTR("%04d/%02d/%02d %02d:%02d:%02d"), + year, month, day, hour, minute, second + ); + + return String(buffer); + +} + + +unsigned long getUptime() { + + static unsigned long last_uptime = 0; + static unsigned char uptime_overflows = 0; + + if (millis() < last_uptime) ++uptime_overflows; + last_uptime = millis(); + unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000); + + return uptime_seconds; + +} + +void heartbeat() { + + unsigned long uptime_seconds = getUptime(); + unsigned int free_heap = ESP.getFreeHeap(); + + #if NTP_SUPPORT + DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str()); + #endif + + if (!mqttConnected()) { + DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds); + DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap); + #if ADC_VCC_ENABLED + DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc()); + #endif + } + + #if (HEARTBEAT_REPORT_INTERVAL) + mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000); + #endif + #if (HEARTBEAT_REPORT_APP) + mqttSend(MQTT_TOPIC_APP, APP_NAME); + #endif + #if (HEARTBEAT_REPORT_VERSION) + mqttSend(MQTT_TOPIC_VERSION, APP_VERSION); + #endif + #if (HEARTBEAT_REPORT_HOSTNAME) + mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str()); + #endif + #if (HEARTBEAT_REPORT_IP) + mqttSend(MQTT_TOPIC_IP, getIP().c_str()); + #endif + #if (HEARTBEAT_REPORT_MAC) + mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str()); + #endif + #if (HEARTBEAT_REPORT_RSSI) + mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str()); + #endif + #if (HEARTBEAT_REPORT_UPTIME) + mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); + #if INFLUXDB_SUPPORT + influxDBSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); + #endif + #endif + #if (HEARTBEAT_REPORT_FREEHEAP) + mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); + #if INFLUXDB_SUPPORT + influxDBSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); + #endif + #endif + #if (HEARTBEAT_REPORT_RELAY) + relayMQTT(); + #endif + #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT) + lightMQTT(); + #endif + #if (HEARTBEAT_REPORT_VCC) + #if ADC_VCC_ENABLED + mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str()); + #endif + #endif + #if (HEARTBEAT_REPORT_STATUS) + mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true); + #endif + +} + +void customReset(unsigned char status) { + EEPROM.write(EEPROM_CUSTOM_RESET, status); + EEPROM.commit(); +} + +unsigned char customReset() { + static unsigned char status = 255; + if (status == 255) { + status = EEPROM.read(EEPROM_CUSTOM_RESET); + if (status > 0) customReset(0); + if (status > CUSTOM_RESET_MAX) status = 0; + } + return status; +} + char * ltrim(char * s) { char *p = s; while ((unsigned char) *p == ' ') ++p; From f175f4aeead41bc6eb6ff73ee574f78a440187dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 29 Aug 2017 10:09:40 +0200 Subject: [PATCH 2/3] Add dependencies for HA support --- code/espurna/config/arduino.h | 1 + code/espurna/homeassitant.ino | 2 ++ 2 files changed, 3 insertions(+) diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 8c7d1bb2..a03dc4e1 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -52,6 +52,7 @@ //#define DOMOTICZ_SUPPORT 0 //#define DS18B20_SUPPORT 1 //#define EMON_SUPPORT 1 +//#define HOMEASSISTANT_SUPPORT 0 //#define I2C_SUPPORT 1 //#define INFLUXDB_SUPPORT 0 //#define MDNS_SUPPORT 0 diff --git a/code/espurna/homeassitant.ino b/code/espurna/homeassitant.ino index ffca411e..49ecd6aa 100644 --- a/code/espurna/homeassitant.ino +++ b/code/espurna/homeassitant.ino @@ -8,6 +8,8 @@ Copyright (C) 2017 by Xose Pérez #if HOMEASSISTANT_SUPPORT +#include + void haSend() { DEBUG_MSG_P(PSTR("[HA] Sending autodiscovery MQTT message\n")); From ca12524db75c10b17c8f252d216b08e4d58d816e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 29 Aug 2017 10:10:10 +0200 Subject: [PATCH 3/3] Terminal commands: renamed RECONNECT to RESET.WIFI, added RESET.MQTT --- code/espurna/settings.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index 0167b1ab..0a8328b9 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -99,16 +99,16 @@ void settingsSetup() { e->response(s); }, 0); - Embedis::command( F("RECONNECT"), [](Embedis* e) { + Embedis::command( F("RESET.WIFI"), [](Embedis* e) { wifiConfigure(); wifiDisconnect(); e->response(Embedis::OK); }); - Embedis::command( F("RESTART"), [](Embedis* e) { + Embedis::command( F("RESET.MQTT"), [](Embedis* e) { + mqttConfigure(); + mqttDisconnect(); e->response(Embedis::OK); - customReset(CUSTOM_RESET_TERMINAL); - ESP.restart(); }); Embedis::command( F("RESET"), [](Embedis* e) {