From cb63e2659bf80ef943dee1ac4019e52f029056f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Wed, 20 Jun 2018 21:56:24 +0200 Subject: [PATCH] Support for BL0937 power monitoring chip with unmodified HLW8012 library (#737) --- code/espurna/config/arduino.h | 1 + code/espurna/config/hardware.h | 46 ++++++++++++++++++++++++++++ code/espurna/config/sensors.h | 20 ++++++++++++ code/espurna/sensor.ino | 6 ++-- code/espurna/sensors/HLW8012Sensor.h | 4 +-- code/platformio.ini | 26 ++++++++++++++++ 6 files changed, 98 insertions(+), 5 deletions(-) diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 5a978961..c06eec38 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -85,6 +85,7 @@ //#define NEO_COOLCAM_POWER_PLUG_WIFI //#define ESTINK_WIFI_POWER_STRIP //#define PILOTAK_ESP_DIN_V1 +//#define BLITZWOLF_BWSHP2 //-------------------------------------------------------------------------------- // Features (values below are non-default values) diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 396c366f..2845423c 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -2240,6 +2240,52 @@ #define LED3_RELAY 3 #define LED4_RELAY 4 +// ----------------------------------------------------------------------------- +// Several boards under different names uing a power chip labelled BL0937 or HJL-01 +// * Blitzwolf (https://www.amazon.es/Inteligente-Temporización-Dispositivos-Cualquier-BlitzWolf/dp/B07BMQP142) +// * HomeCube (https://www.amazon.de/Steckdose-Homecube-intelligente-Verbrauchsanzeige-funktioniert/dp/B076Q2LKHG) +// * Coosa (https://www.amazon.com/COOSA-Monitoring-Function-Campatible-Assiatant/dp/B0788W9TDR) +// * Goosund (http://www.gosund.com/?m=content&c=index&a=show&catid=6&id=5) +// ----------------------------------------------------------------------------- + +#elif defined(BLITZWOLF_BWSHP2) + + // Info + #define MANUFACTURER "BLITZWOLF" + #define DEVICE "BWSHP2" + + // Buttons + #define BUTTON1_PIN 13 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH + #define BUTTON1_RELAY 1 + + // Relays + #define RELAY1_PIN 15 + #define RELAY1_TYPE RELAY_TYPE_NORMAL + + // LEDs + #define LED1_PIN 2 + #define LED1_PIN_INVERSE 1 + #define LED2_PIN 0 + #define LED2_PIN_INVERSE 1 + #define LED2_MODE LED_MODE_FINDME + #define LED2_RELAY 1 + + // HJL01 / BL0937 + #ifndef HLW8012_SUPPORT + #define HLW8012_SUPPORT 1 + #endif + #define HLW8012_SEL_PIN 12 + #define HLW8012_CF1_PIN 14 + #define HLW8012_CF_PIN 5 + + #define HLW8012_SEL_CURRENT LOW + #define HLW8012_CURRENT_RATIO 25740 + #define HLW8012_VOLTAGE_RATIO 313400 + #define HLW8012_POWER_RATIO 3414290 + #define HLW8012_INTERRUPT_ON FALLING + + // ----------------------------------------------------------------------------- // TEST boards (do not use!!) // ----------------------------------------------------------------------------- diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index da8da1f0..771b7c50 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -383,7 +383,27 @@ #define HLW8012_VOLTAGE_R_DOWN ( 1000 ) // Downstream voltage resistor #endif +#ifndef HLW8012_CURRENT_RATIO +#define HLW8012_CURRENT_RATIO 0 // Set to 0 to use factory defaults +#endif + +#ifndef HLW8012_VOLTAGE_RATIO +#define HLW8012_VOLTAGE_RATIO 0 // Set to 0 to use factory defaults +#endif + +#ifndef HLW8012_POWER_RATIO +#define HLW8012_POWER_RATIO 0 // Set to 0 to use factory defaults +#endif + +#ifndef HLW8012_USE_INTERRUPTS #define HLW8012_USE_INTERRUPTS 1 // Use interrupts to trap HLW8012 signals +#endif + +#ifndef HLW8012_INTERRUPT_ON +#define HLW8012_INTERRUPT_ON CHANGE // When to trigger the interrupt + // Use CHANGE for HLW8012 + // Use FALLING for BL0937 / HJL0 +#endif //------------------------------------------------------------------------------ // MHZ19 CO2 sensor diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 0177590a..e20e89b4 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -644,13 +644,13 @@ void _sensorInit() { double value; - value = getSetting("pwrRatioC", 0).toFloat(); + value = getSetting("pwrRatioC", HLW8012_CURRENT_RATIO).toFloat(); if (value > 0) sensor->setCurrentRatio(value); - value = getSetting("pwrRatioV", 0).toFloat(); + value = getSetting("pwrRatioV", HLW8012_VOLTAGE_RATIO).toFloat(); if (value > 0) sensor->setVoltageRatio(value); - value = getSetting("pwrRatioP", 0).toFloat(); + value = getSetting("pwrRatioP", HLW8012_POWER_RATIO).toFloat(); if (value > 0) sensor->setPowerRatio(value); } diff --git a/code/espurna/sensors/HLW8012Sensor.h b/code/espurna/sensors/HLW8012Sensor.h index be21118f..a65e74b1 100644 --- a/code/espurna/sensors/HLW8012Sensor.h +++ b/code/espurna/sensors/HLW8012Sensor.h @@ -234,13 +234,13 @@ class HLW8012Sensor : public BaseSensor { if (_interrupt_cf != _cf) { if (_interrupt_cf != GPIO_NONE) _detach(_interrupt_cf); - _attach(this, _cf, CHANGE); + _attach(this, _cf, HLW8012_INTERRUPT_ON); _interrupt_cf = _cf; } if (_interrupt_cf1 != _cf1) { if (_interrupt_cf1 != GPIO_NONE) _detach(_interrupt_cf1); - _attach(this, _cf1, CHANGE); + _attach(this, _cf1, HLW8012_INTERRUPT_ON); _interrupt_cf1 = _cf1; } diff --git a/code/platformio.ini b/code/platformio.ini index 348e5413..8a928708 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -2423,3 +2423,29 @@ upload_port = "${env.ESPURNA_IP}" upload_flags = --auth=${env.ESPURNA_AUTH} --port 8266 extra_scripts = ${common.extra_scripts} modules = ${common.modules} sensor + +[env:blitzwolf-bwshp2] +platform = ${common.platform} +framework = arduino +board = esp01_1m +lib_deps = ${common.lib_deps} +lib_ignore = ${common.lib_ignore} +build_flags = ${common.build_flags_1m0m} -DBLITZWOLF_BWSHP2 +upload_speed = 115200 +monitor_speed = 115200 +extra_scripts = ${common.extra_scripts} +modules = ${common.modules} sensor + +[env:blitzwolf-bwshp2-ota] +platform = ${common.platform} +framework = arduino +board = esp01_1m +lib_deps = ${common.lib_deps} +lib_ignore = ${common.lib_ignore} +build_flags = ${common.build_flags_1m0m} -DBLITZWOLF_BWSHP2 +upload_speed = 115200 +monitor_speed = 115200 +upload_port = "${env.ESPURNA_IP}" +upload_flags = --auth=${env.ESPURNA_AUTH} --port 8266 +extra_scripts = ${common.extra_scripts} +modules = ${common.modules} sensor