Browse Source

Ensure hlw8012 interrupts are not working while no wifi connection (either STA or AP)

fastled
Xose Pérez 8 years ago
parent
commit
f4a59468e0
3 changed files with 44 additions and 20 deletions
  1. +1
    -0
      code/espurna/config/sensors.h
  2. +29
    -18
      code/espurna/pow.ino
  3. +14
    -2
      code/espurna/wifi.ino

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

@ -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


+ 29
- 18
code/espurna/pow.ino View File

@ -11,9 +11,8 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#include <HLW8012.h>
#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();


+ 14
- 2
code/espurna/wifi.ino View File

@ -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
});
}


Loading…
Cancel
Save