From 95459567d9cd96f26a289e46b8cd5a30a76d12a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 22 Sep 2017 09:50:39 +0200 Subject: [PATCH] Small changes, caching some values --- code/espurna/debug.ino | 3 ++- code/espurna/domoticz.ino | 14 +++++++++----- code/espurna/espurna.ino | 4 ++++ code/espurna/influxdb.ino | 27 +++++++++++++++------------ code/espurna/mqtt.ino | 4 +++- code/espurna/settings.ino | 10 ++++++++++ 6 files changed, 43 insertions(+), 19 deletions(-) diff --git a/code/espurna/debug.ino b/code/espurna/debug.ino index d4d14e9c..0e2c90ef 100644 --- a/code/espurna/debug.ino +++ b/code/espurna/debug.ino @@ -194,7 +194,8 @@ void debugDumpCrashInfo() { EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, epc3); EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, excvaddr); EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, depc); - DEBUG_MSG_P(PSTR("[DEBUG] epc1=0x%08x epc2=0x%08x epc3=0x%08x excvaddr=0x%08x depc=0x%08x\n"), epc1, epc2, epc3, excvaddr, depc); + DEBUG_MSG_P(PSTR("[DEBUG] epc1=0x%08x epc2=0x%08x epc3=0x%08x\n"), epc1, epc2, epc3); + DEBUG_MSG_P(PSTR("[DEBUG] excvaddr=0x%08x depc=0x%08x\n"), excvaddr, depc); uint32_t stack_start, stack_end; EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start); diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index 38e90c11..773ff9bf 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -10,7 +10,7 @@ Copyright (C) 2016-2017 by Xose Pérez #include -bool _dczEnabled = false; +bool _dcz_enabled = false; //------------------------------------------------------------------------------ // Private methods @@ -35,7 +35,7 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) if (type == MQTT_MESSAGE_EVENT) { - if (!_dczEnabled) return; + if (!_dcz_enabled) return; // Check topic if (dczTopicOut.equals(topic)) { @@ -68,7 +68,7 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) //------------------------------------------------------------------------------ template void domoticzSend(const char * key, T nvalue, const char * svalue) { - if (!_dczEnabled) return; + if (!_dcz_enabled) return; unsigned int idx = getSetting(key).toInt(); if (idx > 0) { char payload[128]; @@ -82,7 +82,7 @@ template void domoticzSend(const char * key, T nvalue) { } void domoticzSendRelay(unsigned int relayID) { - if (!_dczEnabled) return; + if (!_dcz_enabled) return; char buffer[15]; snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%d"), relayID); domoticzSend(buffer, relayStatus(relayID) ? "1" : "0"); @@ -95,7 +95,7 @@ int domoticzIdx(unsigned int relayID) { } void domoticzConfigure() { - _dczEnabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; + _dcz_enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; } void domoticzSetup() { @@ -103,4 +103,8 @@ void domoticzSetup() { mqttRegister(_domoticzMqtt); } +bool domoticzEnabled() { + return _dcz_enabled; +} + #endif diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index ffb5730a..149497f4 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -204,7 +204,11 @@ void welcome() { } else { DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str()); } + DEBUG_MSG_P(PSTR("[INIT] Free heap: %u bytes\n"), ESP.getFreeHeap()); + #if ADC_VCC_ENABLED + DEBUG_MSG_P(PSTR("[INIT] Power: %d mV\n"), ESP.getVcc()); + #endif DEBUG_MSG_P(PSTR("\n")); } diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index caf79c2e..2eb797bd 100644 --- a/code/espurna/influxdb.ino +++ b/code/espurna/influxdb.ino @@ -11,18 +11,18 @@ Copyright (C) 2017 by Xose Pérez #include "ESPAsyncTCP.h" #include "SyncClient.h" -bool influxDBEnabled = false; -SyncClient _influxClient; +bool _influxdb_enabled = false; +SyncClient _influx_client; template bool influxDBSend(const char * topic, T payload) { - if (!influxDBEnabled) return true; + if (!_influxdb_enabled) return true; if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true; DEBUG_MSG("[INFLUXDB] Sending\n"); - _influxClient.setTimeout(2); - if (!_influxClient.connect(getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt())) { + _influx_client.setTimeout(2); + if (!_influx_client.connect(getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt())) { DEBUG_MSG("[INFLUXDB] Connection failed\n"); return false; } @@ -37,22 +37,25 @@ template bool influxDBSend(const char * topic, T payload) { getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt(), strlen(data), data); - if (_influxClient.printf(request) > 0) { - while (_influxClient.connected() && _influxClient.available() == 0) delay(1); - while (_influxClient.available()) _influxClient.read(); - if (_influxClient.connected()) _influxClient.stop(); + if (_influx_client.printf(request) > 0) { + while (_influx_client.connected() && _influx_client.available() == 0) delay(1); + while (_influx_client.available()) _influx_client.read(); + if (_influx_client.connected()) _influx_client.stop(); return true; } - _influxClient.stop(); + _influx_client.stop(); DEBUG_MSG("[INFLUXDB] Sent failed\n"); - while (_influxClient.connected()) delay(0); + while (_influx_client.connected()) delay(0); return false; } +bool influxdbEnabled() { + return _influxdb_enabled; +} void influxDBConfigure() { - influxDBEnabled = getSetting("idbHost").length() > 0; + _influxdb_enabled = getSetting("idbHost").length() > 0; } void influxDBSetup() { diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 77aa237f..4b6424d1 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -31,6 +31,7 @@ WiFiClientSecure _mqtt_client_secure; #endif // MQTT_USE_ASYNC bool _mqtt_enabled = MQTT_ENABLED; +bool _mqtt_use_json = false; unsigned long _mqtt_reconnect_delay = MQTT_RECONNECT_DELAY_MIN; String _mqtt_topic; String _mqtt_setter; @@ -135,7 +136,7 @@ void _mqttFlush() { } void mqttSend(const char * topic, const char * message, bool force) { - bool useJson = force ? false : getSetting("mqttUseJson", MQTT_USE_JSON).toInt() == 1; + bool useJson = force ? false : _mqtt_use_json; if (useJson) { mqtt_message_t element; element.topic = strdup(topic); @@ -466,6 +467,7 @@ void mqttConfigure() { } else { _mqtt_enabled = getSetting("mqttEnabled", MQTT_ENABLED).toInt() == 1; } + _mqtt_use_json = (getSetting("mqttUseJson", MQTT_USE_JSON).toInt() == 1); } diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index fb6353c8..e24d0f80 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -135,6 +135,16 @@ void settingsSetup() { e->response(Embedis::OK); }); + Embedis::command( F("INFO"), [](Embedis* e) { + welcome(); + e->response(Embedis::OK); + }); + + Embedis::command( F("UPTIME"), [](Embedis* e) { + e->stream->printf("Uptime: %d seconds\n", getUptime()); + e->response(Embedis::OK); + }); + Embedis::command( F("RESET"), [](Embedis* e) { e->response(Embedis::OK); customReset(CUSTOM_RESET_TERMINAL);