Browse Source

Periodic MQTT reports configuration

fastled
Xose Pérez 7 years ago
parent
commit
635fcf9a0b
4 changed files with 64 additions and 11 deletions
  1. +22
    -1
      code/espurna/config/general.h
  2. +9
    -0
      code/espurna/config/sensors.h
  3. +30
    -8
      code/espurna/espurna.ino
  4. +3
    -2
      code/espurna/mqtt.ino

+ 22
- 1
code/espurna/config/general.h View File

@ -6,6 +6,7 @@
#define HOSTNAME DEVICE #define HOSTNAME DEVICE
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
#define HEARTBEAT_INTERVAL 300000 #define HEARTBEAT_INTERVAL 300000
#define UPTIME_OVERFLOW 4294967295
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// EEPROM // EEPROM
@ -15,6 +16,14 @@
#define EEPROM_ENERGY_COUNT 1 #define EEPROM_ENERGY_COUNT 1
#define EEPROM_DATA_END 5 #define EEPROM_DATA_END 5
//--------------------------------------------------------------------------------
// POWER SUPPLY
//--------------------------------------------------------------------------------
#ifndef ENABLE_VCC_REPORT
#define ENABLE_VCC_REPORT 1
#endif
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// BUTTON // BUTTON
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
@ -155,7 +164,10 @@
#define MQTT_BUTTON_TOPIC "/button" #define MQTT_BUTTON_TOPIC "/button"
#define MQTT_IP_TOPIC "/ip" #define MQTT_IP_TOPIC "/ip"
#define MQTT_VERSION_TOPIC "/version" #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" #define MQTT_ACTION_RESET "reset"
@ -168,6 +180,15 @@
#define MQTT_USE_GETTER "" #define MQTT_USE_GETTER ""
#define MQTT_USE_SETTER "" #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 // I2C
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------


+ 9
- 0
code/espurna/config/sensors.h View File

@ -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 // Custom RF module
// Check http://tinkerman.cat/adding-rf-to-a-non-rf-itead-sonoff/ // Check http://tinkerman.cat/adding-rf-to-a-non-rf-itead-sonoff/


+ 30
- 8
code/espurna/espurna.ino View File

@ -40,13 +40,37 @@ void hardwareSetup() {
void hardwareLoop() { void hardwareLoop() {
static unsigned long last_uptime = 0;
static unsigned char uptime_overflows = 0;
// Heartbeat // 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() { void welcome() {
delay(2000);
DEBUG_MSG("%s %s\n", (char *) APP_NAME, (char *) APP_VERSION); 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("%s\n%s\n\n", (char *) APP_AUTHOR, (char *) APP_WEBSITE);
DEBUG_MSG("ChipID: %06X\n", ESP.getChipId()); DEBUG_MSG("ChipID: %06X\n", ESP.getChipId());


+ 3
- 2
code/espurna/mqtt.ino View File

@ -98,6 +98,7 @@ void _mqttOnConnect() {
// Say hello and report our IP and VERSION // Say hello and report our IP and VERSION
mqttSend(MQTT_IP_TOPIC, getIP().c_str()); mqttSend(MQTT_IP_TOPIC, getIP().c_str());
mqttSend(MQTT_VERSION_TOPIC, APP_VERSION); mqttSend(MQTT_VERSION_TOPIC, APP_VERSION);
mqttSend(MQTT_STATUS_TOPIC, "1");
// Subscribe to system topics // Subscribe to system topics
mqttSubscribe(MQTT_ACTION_TOPIC); mqttSubscribe(MQTT_ACTION_TOPIC);
@ -200,10 +201,10 @@ void mqttConnect() {
if ((strlen(user) > 0) && (strlen(pass) > 0)) { if ((strlen(user) > 0) && (strlen(pass) > 0)) {
DEBUG_MSG(" as user '%s'\n", user); 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 { } else {
DEBUG_MSG("\n"); 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) { if (response) {


Loading…
Cancel
Save