From 635fcf9a0b6946fd772461be2c9b223bee973a57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 25 Feb 2017 16:01:11 +0100 Subject: [PATCH] Periodic MQTT reports configuration --- code/espurna/config/general.h | 23 ++++++++++++++++++++- code/espurna/config/sensors.h | 9 +++++++++ code/espurna/espurna.ino | 38 +++++++++++++++++++++++++++-------- code/espurna/mqtt.ino | 5 +++-- 4 files changed, 64 insertions(+), 11 deletions(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 255f76a3..837e204c 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -6,6 +6,7 @@ #define HOSTNAME DEVICE #define BUFFER_SIZE 1024 #define HEARTBEAT_INTERVAL 300000 +#define UPTIME_OVERFLOW 4294967295 //-------------------------------------------------------------------------------- // EEPROM @@ -15,6 +16,14 @@ #define EEPROM_ENERGY_COUNT 1 #define EEPROM_DATA_END 5 +//-------------------------------------------------------------------------------- +// POWER SUPPLY +//-------------------------------------------------------------------------------- + +#ifndef ENABLE_VCC_REPORT +#define ENABLE_VCC_REPORT 1 +#endif + //-------------------------------------------------------------------------------- // BUTTON //-------------------------------------------------------------------------------- @@ -155,7 +164,10 @@ #define MQTT_BUTTON_TOPIC "/button" #define MQTT_IP_TOPIC "/ip" #define MQTT_VERSION_TOPIC "/version" -#define MQTT_HEARTBEAT_TOPIC "/status" +#define MQTT_UPTIME_TOPIC "/uptime" +#define MQTT_FREEHEAP_TOPIC "/freeheap" +#define MQTT_VCC_TOPIC "/vcc" +#define MQTT_STATUS_TOPIC "/status" #define MQTT_ACTION_RESET "reset" @@ -168,6 +180,15 @@ #define MQTT_USE_GETTER "" #define MQTT_USE_SETTER "" +// Periodic reports +#define MQTT_STATUS_REPORT 1 +#define MQTT_IP_REPORT 2 +#define MQTT_UPTIME_REPORT 4 +#define MQTT_FREEHEAP_REPORT 8 +#define MQTT_VCC_REPORT 16 + +#define MQTT_REPORTS (MQTT_STATUS_REPORT | MQTT_UPTIME_REPORT | MQTT_FREEHEAP_REPORT) + // ----------------------------------------------------------------------------- // I2C // ----------------------------------------------------------------------------- diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 350d9a9e..db0248d9 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -1,3 +1,12 @@ +//-------------------------------------------------------------------------------- +// Internal popwer montior +// Enable support by passing ENABLE_VCC_REPORT=1 build flag +//-------------------------------------------------------------------------------- + +#if (MQTT_REPORTS | MQTT_VCC_REPORT) + ADC_MODE(ADC_VCC); +#endif + //-------------------------------------------------------------------------------- // Custom RF module // Check http://tinkerman.cat/adding-rf-to-a-non-rf-itead-sonoff/ diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 898105de..b8e3516f 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -40,13 +40,37 @@ void hardwareSetup() { void hardwareLoop() { + static unsigned long last_uptime = 0; + static unsigned char uptime_overflows = 0; + // Heartbeat - static unsigned long last_heartbeat = 0; - if ((millis() - last_heartbeat > HEARTBEAT_INTERVAL) || (last_heartbeat == 0)) { - last_heartbeat = millis(); - mqttSend(MQTT_HEARTBEAT_TOPIC, "1"); - DEBUG_MSG("[BEAT] Free heap: %d\n", ESP.getFreeHeap()); - DEBUG_MSG("[NTP] Time: %s\n", (char *) NTP.getTimeDateString().c_str()); + if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) { + + if (millis() < last_uptime) ++uptime_overflows; + last_uptime = millis(); + unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000); + + DEBUG_MSG("[MAIN] Time: %s\n", (char *) NTP.getTimeDateString().c_str()); + DEBUG_MSG("[MAIN] Uptime: %ld seconds\n", uptime_seconds); + DEBUG_MSG("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap()); + + #if (MQTT_REPORTS | MQTT_STATUS_REPORT) + mqttSend(MQTT_STATUS_TOPIC, "1"); + #endif + #if (MQTT_REPORTS | MQTT_IP_REPORT) + mqttSend(MQTT_IP_TOPIC, getIP().c_str()); + #endif + #if (MQTT_REPORTS | MQTT_UPTIME_REPORT) + mqttSend(MQTT_UPTIME_TOPIC, String(uptime_seconds).c_str()); + #endif + #if (MQTT_REPORTS | MQTT_FREEHEAP_REPORT) + mqttSend(MQTT_FREEHEAP_TOPIC, String(ESP.getFreeHeap()).c_str()); + #endif + #if (MQTT_REPORTS | MQTT_VCC_REPORT) + DEBUG_MSG("[BEAT] Power: %d mV\n", ESP.getVcc()); + mqttSend(MQTT_VCC_TOPIC, String(ESP.getVcc()).c_str()); + #endif + } } @@ -57,8 +81,6 @@ void hardwareLoop() { void welcome() { - delay(2000); - DEBUG_MSG("%s %s\n", (char *) APP_NAME, (char *) APP_VERSION); DEBUG_MSG("%s\n%s\n\n", (char *) APP_AUTHOR, (char *) APP_WEBSITE); DEBUG_MSG("ChipID: %06X\n", ESP.getChipId()); diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 1f633ed6..51c6f229 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -98,6 +98,7 @@ void _mqttOnConnect() { // Say hello and report our IP and VERSION mqttSend(MQTT_IP_TOPIC, getIP().c_str()); mqttSend(MQTT_VERSION_TOPIC, APP_VERSION); + mqttSend(MQTT_STATUS_TOPIC, "1"); // Subscribe to system topics mqttSubscribe(MQTT_ACTION_TOPIC); @@ -200,10 +201,10 @@ void mqttConnect() { if ((strlen(user) > 0) && (strlen(pass) > 0)) { DEBUG_MSG(" as user '%s'\n", user); - response = mqtt.connect(getIdentifier().c_str(), user, pass, (mqttTopic + MQTT_HEARTBEAT_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, "0"); + response = mqtt.connect(getIdentifier().c_str(), user, pass, (mqttTopic + MQTT_STATUS_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, "0"); } else { DEBUG_MSG("\n"); - response = mqtt.connect(getIdentifier().c_str(), (mqttTopic + MQTT_HEARTBEAT_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, "0"); + response = mqtt.connect(getIdentifier().c_str(), (mqttTopic + MQTT_STATUS_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, "0"); } if (response) {