diff --git a/code/espurna/alexa.ino b/code/espurna/alexa.ino index c6920976..e12b5fe1 100644 --- a/code/espurna/alexa.ino +++ b/code/espurna/alexa.ino @@ -20,6 +20,8 @@ bool _alexa_change = false; unsigned int _alexa_device_id = 0; bool _alexa_state = false; +// ----------------------------------------------------------------------------- + void alexaConfigure() { alexa.enable(getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1); } @@ -29,6 +31,7 @@ void alexaSetup() { // Backwards compatibility moveSetting("fauxmoEnabled", "alexaEnabled"); + // Load & cache settings alexaConfigure(); unsigned int relays = relayCount(); diff --git a/code/espurna/analog.ino b/code/espurna/analog.ino index 3a2e822c..d2bcef5d 100644 --- a/code/espurna/analog.ino +++ b/code/espurna/analog.ino @@ -51,7 +51,7 @@ void analogLoop() { // Send to InfluxDB #if INFLUXDB_SUPPORT - influxDBSend(MQTT_TOPIC_ANALOG, analog); + idbSend(MQTT_TOPIC_ANALOG, analog); #endif // Update websocket clients diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 620ae2a9..bd819a17 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -20,6 +20,6 @@ template String getSetting(const String& key, unsigned int index, T template void domoticzSend(const char * key, T value); template void domoticzSend(const char * key, T nvalue, const char * svalue); -template bool influxDBSend(const char * topic, T payload); +template bool idbSend(const char * topic, T payload); char * ltrim(char * s); diff --git a/code/espurna/counter.ino b/code/espurna/counter.ino index 9831e0c0..878b8b3b 100644 --- a/code/espurna/counter.ino +++ b/code/espurna/counter.ino @@ -80,7 +80,7 @@ void counterLoop() { // Send to InfluxDB #if INFLUXDB_SUPPORT - influxDBSend(COUNTER_TOPIC, _counterValue); + idbSend(COUNTER_TOPIC, _counterValue); #endif } diff --git a/code/espurna/dht.ino b/code/espurna/dht.ino index b7bc6610..5be5f113 100644 --- a/code/espurna/dht.ino +++ b/code/espurna/dht.ino @@ -208,8 +208,8 @@ void dhtLoop() { #endif #if INFLUXDB_SUPPORT - influxDBSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature); - influxDBSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity); + idbSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature); + idbSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity); #endif // Update websocket clients diff --git a/code/espurna/ds18b20.ino b/code/espurna/ds18b20.ino index a93b5f5c..c4374f03 100644 --- a/code/espurna/ds18b20.ino +++ b/code/espurna/ds18b20.ino @@ -115,7 +115,7 @@ void dsLoop() { #endif #if INFLUXDB_SUPPORT - influxDBSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr); + idbSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr); #endif } diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index e8b26718..3cca1518 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -287,7 +287,7 @@ void setup() { nofussSetup(); #endif #if INFLUXDB_SUPPORT - influxDBSetup(); + idbSetup(); #endif #if DS18B20_SUPPORT dsSetup(); diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index 2eb797bd..5dc25835 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 _influxdb_enabled = false; -SyncClient _influx_client; +bool _idb_enabled = false; +SyncClient _idb_client; -template bool influxDBSend(const char * topic, T payload) { +template bool idbSend(const char * topic, T payload) { - if (!_influxdb_enabled) return true; + if (!_idb_enabled) return true; if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true; DEBUG_MSG("[INFLUXDB] Sending\n"); - _influx_client.setTimeout(2); - if (!_influx_client.connect(getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt())) { + _idb_client.setTimeout(2); + if (!_idb_client.connect(getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt())) { DEBUG_MSG("[INFLUXDB] Connection failed\n"); return false; } @@ -37,29 +37,30 @@ template bool influxDBSend(const char * topic, T payload) { getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt(), strlen(data), data); - 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(); + if (_idb_client.printf(request) > 0) { + while (_idb_client.connected() && _idb_client.available() == 0) delay(1); + while (_idb_client.available()) _idb_client.read(); + if (_idb_client.connected()) _idb_client.stop(); return true; } - _influx_client.stop(); + _idb_client.stop(); DEBUG_MSG("[INFLUXDB] Sent failed\n"); - while (_influx_client.connected()) delay(0); + while (_idb_client.connected()) delay(0); return false; } -bool influxdbEnabled() { - return _influxdb_enabled; + +bool idbEnabled() { + return _idb_enabled; } -void influxDBConfigure() { - _influxdb_enabled = getSetting("idbHost").length() > 0; +void idbConfigure() { + _idb_enabled = getSetting("idbHost").length() > 0; } -void influxDBSetup() { - influxDBConfigure(); +void idbSetup() { + idbConfigure(); } #endif diff --git a/code/espurna/power.ino b/code/espurna/power.ino index 9c94e6d7..980f4daa 100644 --- a/code/espurna/power.ino +++ b/code/espurna/power.ino @@ -234,16 +234,16 @@ void _powerReport() { #endif #if INFLUXDB_SUPPORT - if (influxdbEnabled()) { - influxDBSend(MQTT_TOPIC_CURRENT, buf_current); - influxDBSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str()); - influxDBSend(MQTT_TOPIC_ENERGY_DELTA, buf_energy_delta); - influxDBSend(MQTT_TOPIC_ENERGY_TOTAL, buf_energy_total); + if (idbEnabled()) { + idbSend(MQTT_TOPIC_CURRENT, buf_current); + idbSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str()); + idbSend(MQTT_TOPIC_ENERGY_DELTA, buf_energy_delta); + idbSend(MQTT_TOPIC_ENERGY_TOTAL, buf_energy_total); #if POWER_HAS_ACTIVE - influxDBSend(MQTT_TOPIC_POWER_ACTIVE, String((int) _power_active).c_str()); - influxDBSend(MQTT_TOPIC_POWER_REACTIVE, String((int) _power_reactive).c_str()); - influxDBSend(MQTT_TOPIC_VOLTAGE, String((int) _power_voltage).c_str()); - influxDBSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str()); + idbSend(MQTT_TOPIC_POWER_ACTIVE, String((int) _power_active).c_str()); + idbSend(MQTT_TOPIC_POWER_REACTIVE, String((int) _power_reactive).c_str()); + idbSend(MQTT_TOPIC_VOLTAGE, String((int) _power_voltage).c_str()); + idbSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str()); #endif } #endif diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index ce437387..3fb633ee 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -493,7 +493,7 @@ void relayInfluxDB(unsigned char id) { if (id >= _relays.size()) return; char buffer[10]; snprintf_P(buffer, sizeof(buffer), PSTR("%s,id=%d"), MQTT_TOPIC_RELAY, id); - influxDBSend(buffer, relayStatus(id) ? "1" : "0"); + idbSend(buffer, relayStatus(id) ? "1" : "0"); } #endif diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index 6b96d5ed..acc503e1 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -96,13 +96,13 @@ void heartbeat() { #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()); + idbSend(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()); + idbSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); #endif #endif #if (HEARTBEAT_REPORT_RELAY) diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index 24ed8923..408e1e11 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -367,7 +367,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { alexaConfigure(); #endif #if INFLUXDB_SUPPORT - influxDBConfigure(); + idbConfigure(); #endif #if DOMOTICZ_SUPPORT domoticzConfigure();