From f4a59468e05a527fc656687709c5c2c07c20e201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 20 Jan 2017 23:31:02 +0100 Subject: [PATCH] Ensure hlw8012 interrupts are not working while no wifi connection (either STA or AP) --- code/espurna/config/sensors.h | 1 + code/espurna/pow.ino | 47 +++++++++++++++++++++-------------- code/espurna/wifi.ino | 16 ++++++++++-- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 18d67bc9..d6cbcd80 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -56,6 +56,7 @@ #define POW_SEL_PIN 5 #define POW_CF1_PIN 13 #define POW_CF_PIN 14 +#define POW_USE_INTERRUPTS 1 #define POW_SEL_CURRENT HIGH #define POW_CURRENT_R 0.001 #define POW_VOLTAGE_R_UP ( 5 * 470000 ) // Real: 2280k diff --git a/code/espurna/pow.ino b/code/espurna/pow.ino index 327ad08f..09258c4f 100644 --- a/code/espurna/pow.ino +++ b/code/espurna/pow.ino @@ -11,9 +11,8 @@ Copyright (C) 2016-2017 by Xose PĂ©rez #include -#define POW_USE_INTERRUPTS 1 - HLW8012 hlw8012; +bool _powEnabled = false; // ----------------------------------------------------------------------------- // POW @@ -29,16 +28,21 @@ void hlw8012_cf_interrupt() { hlw8012.cf_interrupt(); } -void powAttachInterrupts() { - attachInterrupt(POW_CF1_PIN, hlw8012_cf1_interrupt, CHANGE); - attachInterrupt(POW_CF_PIN, hlw8012_cf_interrupt, CHANGE); - DEBUG_MSG("[POW] Enabled\n"); -} - -void powDettachInterrupts() { - detachInterrupt(POW_CF1_PIN); - detachInterrupt(POW_CF_PIN); - DEBUG_MSG("[POW] Disabled\n"); +void powEnable(bool status) { + _powEnabled = status; + if (_powEnabled) { + #if POW_USE_INTERRUPTS == 1 + attachInterrupt(POW_CF1_PIN, hlw8012_cf1_interrupt, CHANGE); + attachInterrupt(POW_CF_PIN, hlw8012_cf_interrupt, CHANGE); + #endif + DEBUG_MSG("[POW] Enabled\n"); + } else { + #if POW_USE_INTERRUPTS == 1 + detachInterrupt(POW_CF1_PIN); + detachInterrupt(POW_CF_PIN); + #endif + DEBUG_MSG("[POW] Disabled\n"); + } } // ----------------------------------------------------------------------------- @@ -137,11 +141,6 @@ void powSetup() { // Retrieve calibration values powRetrieveCalibration(); - // Attach interrupts - #if POW_USE_INTERRUPTS - powAttachInterrupts(); - #endif - } void powLoop() { @@ -152,8 +151,20 @@ void powLoop() { static unsigned long power_sum = 0; static double current_sum = 0; static unsigned long voltage_sum = 0; + static bool powWasEnabled = false; + + // POW is disabled while there is no internet connection + // When the HLW8012 measurements are enabled back we reset the timer + if (!_powEnabled) { + powWasEnabled = false; + return; + } + if (!powWasEnabled) { + last_update = millis(); + powWasEnabled = true; + } - if ((millis() - last_update > POW_UPDATE_INTERVAL) || (last_update == 0 )){ + if (millis() - last_update > POW_UPDATE_INTERVAL) { last_update = millis(); diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index 9f65520d..70335e32 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -30,6 +30,9 @@ String getNetwork() { } void wifiDisconnect() { + #if ENABLE_POW + powEnable(false); + #endif jw.disconnect(); } @@ -149,14 +152,12 @@ void wifiSetup() { // Configure mDNS if (code == MESSAGE_CONNECTED) { - if (MDNS.begin((char *) WiFi.hostname().c_str())) { MDNS.addService("http", "tcp", 80); DEBUG_MSG("[MDNS] OK\n"); } else { DEBUG_MSG("[MDNS] FAIL\n"); } - } // Configure captive portal @@ -167,6 +168,17 @@ void wifiSetup() { dnsServer.stop(); } + // Manage POW + #if ENABLE_POW + if (code == MESSAGE_CONNECTED) { + powEnable(true); + } + if (code == MESSAGE_DISCONNECTED) { + powEnable(false); + } + #endif + + }); }