From 18c69f0a1ec2e58a3c3f5622c7a3ce8230edaa01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 2 Jan 2018 12:55:06 +0100 Subject: [PATCH] Several fixes related to dependencies between modules --- code/espurna/config/arduino.h | 4 +- code/espurna/config/general.h | 10 +++++ code/espurna/espurna.ino | 17 ++------ code/espurna/influxdb.ino | 2 +- code/espurna/mqtt.ino | 4 ++ code/espurna/relay.ino | 18 ++++---- code/espurna/sensors/EmonADC121Sensor.h | 55 +++++++++++++++++++++++-- 7 files changed, 81 insertions(+), 29 deletions(-) diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 2d9d27c1..2a2b9ac5 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -73,7 +73,9 @@ //#define IR_SUPPORT 1 //#define LLMNR_SUPPORT 1 // Only with Arduino Core 2.4.0 //#define MDNS_SUPPORT 0 -//#define NOFUSS_SUPPORT 1 // Only with Arduino Core 2.4.0 +//#define MQTT_SUPPORT 0 +//#define NETBIOS_SUPPORT 1 // Only with Arduino Core 2.4.0 +//#define NOFUSS_SUPPORT 1 //#define NTP_SUPPORT 0 //#define RF_SUPPORT 1 //#define SPIFFS_SUPPORT 1 diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 7bf7e084..4399fc47 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -66,6 +66,11 @@ #define DEBUG_TELNET_SUPPORT TELNET_SUPPORT // Enable telnet debug log if telnet is enabled too #endif +#if DEBUG_TELNET_SUPPORT +#undef TELNET_SUPPORT +#define TELNET_SUPPORT 1 +#endif + //------------------------------------------------------------------------------ // General debug options and macros @@ -565,6 +570,11 @@ PROGMEM const char* const custom_reset_string[] = { #define DOMOTICZ_SUPPORT MQTT_SUPPORT // Build with domoticz (if MQTT) support (1.72Kb) #endif +#if DOMOTICZ_SUPPORT +#undef MQTT_SUPPORT +#define MQTT_SUPPORT 1 // If Domoticz enabled enable MQTT +#endif + #define DOMOTICZ_ENABLED 0 // Disable domoticz by default #define DOMOTICZ_IN_TOPIC "domoticz/in" // Default subscription topic #define DOMOTICZ_OUT_TOPIC "domoticz/out" // Default publication topic diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 24a35b8e..0ba5c7b5 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -56,20 +56,9 @@ void hardwareSetup() { void hardwareLoop() { // Heartbeat - static unsigned long last_uptime = 0; - static bool on_connect = true; - - bool send = (last_uptime == 0); - send = send || (millis() - last_uptime > HEARTBEAT_INTERVAL); - if (mqttConnected()) { - send = send || on_connect; - } else { - on_connect = true; - } - - if (send) { - last_uptime = millis(); - if (mqttConnected()) on_connect = false; + static unsigned long last = 0; + if ((last == 0) || (millis() - last > HEARTBEAT_INTERVAL)) { + last = millis(); heartbeat(); } diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index c5a795d1..22b36c67 100644 --- a/code/espurna/influxdb.ino +++ b/code/espurna/influxdb.ino @@ -78,7 +78,7 @@ template bool idbSend(const char * topic, T payload) { template bool idbSend(const char * topic, unsigned char id, T payload) { char measurement[64]; - snprintf_P(measurement, sizeof(measurement), PSTR("%s,id=%d"), topic, id); + snprintf(measurement, sizeof(measurement), "%s,id=%d", topic, id); return idbSend(topic, payload); } diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 05a71958..f7ab5784 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -233,8 +233,12 @@ void _mqttCallback(unsigned int type, const char * topic, const char * payload) if (type == MQTT_CONNECT_EVENT) { + // Subscribe to internal action topics mqttSubscribe(MQTT_TOPIC_ACTION); + // Send heartbeat messages + heartbeat(); + } if (type == MQTT_MESSAGE_EVENT) { diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 5ed1bc0f..5834f353 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -374,6 +374,15 @@ void _relayBoot() { } +void _relayConfigure() { + for (unsigned int i=0; i<_relays.size(); i++) { + pinMode(_relays[i].pin, OUTPUT); + if (_relays[i].type == RELAY_TYPE_LATCHED) pinMode(_relays[i].reset_pin, OUTPUT); + _relays[i].pulse = getSetting("relayPulse", i, RELAY_PULSE_MODE).toInt(); + _relays[i].pulse_ms = 1000 * getSetting("relayTime", i, RELAY_PULSE_MODE).toFloat(); + } +} + //------------------------------------------------------------------------------ // WEBSOCKETS //------------------------------------------------------------------------------ @@ -454,15 +463,6 @@ void _relayWebSocketOnAction(const char * action, JsonObject& data) { } -void _relayConfigure() { - for (unsigned int i=0; i<_relays.size(); i++) { - pinMode(_relays[i].pin, OUTPUT); - if (_relays[i].type == RELAY_TYPE_LATCHED) pinMode(_relays[i].reset_pin, OUTPUT); - _relays[i].pulse = getSetting("relayPulse", i, RELAY_PULSE_MODE).toInt(); - _relays[i].pulse_ms = 1000 * getSetting("relayTime", i, RELAY_PULSE_MODE).toFloat(); - } -} - void relaySetupWS() { wsOnSendRegister(_relayWebSocketOnStart); wsOnActionRegister(_relayWebSocketOnAction); diff --git a/code/espurna/sensors/EmonADC121Sensor.h b/code/espurna/sensors/EmonADC121Sensor.h index 4bf9deaf..a8335ce7 100644 --- a/code/espurna/sensors/EmonADC121Sensor.h +++ b/code/espurna/sensors/EmonADC121Sensor.h @@ -8,7 +8,7 @@ #pragma once #include "Arduino.h" -#include "EmonAnalogSensor.h" +#include "EmonSensor.h" #if I2C_USE_BRZO #include @@ -29,7 +29,7 @@ #define ADC121_RESOLUTION 12 #define ADC121_CHANNELS 1 -class EmonADC121Sensor : public EmonAnalogSensor { +class EmonADC121Sensor : public EmonSensor { public: @@ -37,7 +37,7 @@ class EmonADC121Sensor : public EmonAnalogSensor { // Public // --------------------------------------------------------------------- - EmonADC121Sensor(): EmonAnalogSensor() { + EmonADC121Sensor(): EmonSensor() { _channels = ADC121_CHANNELS; _sensor_id = SENSOR_EMON_ADC121_ID; init(); @@ -102,7 +102,54 @@ class EmonADC121Sensor : public EmonAnalogSensor { return; } - EmonAnalogSensor:pre(); + _current[0] = read(0); + + #if EMON_REPORT_ENERGY + static unsigned long last = 0; + if (last > 0) { + _energy[0] += (_current[0] * _voltage * (millis() - last) / 1000); + } + last = millis(); + #endif + + } + + // Type for slot # index + unsigned char type(unsigned char index) { + _error = SENSOR_ERROR_OK; + unsigned char i=0; + #if EMON_REPORT_CURRENT + if (index == i++) return MAGNITUDE_CURRENT; + #endif + #if EMON_REPORT_POWER + if (index == i++) return MAGNITUDE_POWER_APPARENT; + #endif + #if EMON_REPORT_ENERGY + if (index == i) return MAGNITUDE_ENERGY; + #endif + _error = SENSOR_ERROR_OUT_OF_RANGE; + return MAGNITUDE_NONE; + } + + // Current value for slot # index + double value(unsigned char index) { + + _error = SENSOR_ERROR_OK; + unsigned char channel = index / _magnitudes; + + unsigned char i=0; + #if EMON_REPORT_CURRENT + if (index == i++) return _current[channel]; + #endif + #if EMON_REPORT_POWER + if (index == i++) return _current[channel] * _voltage; + #endif + #if EMON_REPORT_ENERGY + if (index == i) return _energy[channel]; + #endif + + _error = SENSOR_ERROR_OUT_OF_RANGE; + return 0; }