DIY Stand-by Killer Via Home Automation and BL0937 Explanation
by Wim3d in Circuits > Assistive Tech
8828 Views, 6 Favorites, 0 Comments
DIY Stand-by Killer Via Home Automation and BL0937 Explanation
In this Instructable I show you how I hacked a smart switch with power meter function and transformed it to a stand-by killer. Great for charging a phone and killing the power when the phone battery is full.
In my previous setup, I used a timer in my home automation to switch of the load after the pre-set time, but when I was charging a phone or comparable, I wanted to fully charge the battery and then shut off the power to the charger.
If you do not have a home automation or Node Red setup, you can also alter the code of the switch program to do the standby killing in the switch. However, if you want to tweak the settings, this requires a change in firmware. While in a home automation setup you can change the parameters easily.
Disclaimer: The stand-by killer switches of the connected load, but does not switch of itself and remains connected to the WiFi network.
Supplies
- Smart switch with power meter function, based on ESP8266 or ESP8285
- Arduino IDE and FTDI or other UART serial programmer
- Home automation setup, for example Openhab or Node-Red
The Principle
In this scheme you can see the principle of the standby killer. The smart switch measures the power and publishes the value to my home automation.
When the power is above the defined threshold, nothing happens.
When the power is below the defined threshold, my home automation wait for a defined debounce time for the power to transcend the threshold. This debounce time also sees that, when charging a device, your device gets a full battery since the power drops and the end of the charging cycle.
If the power remains below the threshold, the switches is switched of by the home automation via an 'OFF' command.
The Hardware
I made the standby killer from two different smart plugs with power metering function. Important that the switch is based on an ESP8266 or ESP8285 chip, so you can easily flash the microcontroller with your custom firmware.
The plugs I used where bought on Aliexpress:
The BSD34 can be pried open, see my other instructable. For more information on the pinout of this switch, see this website. It is ESP8285 based, see the pictures for the way I connected it to my FTDI adapter. The RX en TX pins are used by the switch and are connected to the main PCB of the switch. If you connect your wires to the solder pads of the ESP8285 PCB, make sure that the connections with the main PCB are inhibited. You can also desolder the whole ESP8285 PCB.
The NEO-coolcam can be opened by removing the allen screw (see picture). See this website for an explanation how to flash the ESP8266 based switch. I did not completely desolder the parts, but I did make a picture of my connections.
Power Measuring
The switches I used measure the voltage, current and power via the BL0937. This IC is somewhat similar to the HLW8012 which is used in several Sonoff-devices.
The BL0937 is from a Chinese manufacturer (Belling) and most information is in Chinese, however I found a English datasheet.
Information I used:
At first I investigated in depth how the IC works and compared it to the HLW8012.
What does the BL0937 do?
On the PCB, the BL0937 is connected to mains via a set of resistors which act as a voltage divider to measure the mains voltage via the VP-pin. The IP and IN pins of the BL0937 are connected to a shunt resistor, in my case of 1m Ohm, and measure the voltage over the resistor which are a measure for the current.
The chip sends pulses on the both CF and CF1 pins as an output. The pulse frequency on the CF pin is a measure for the power. The pulse frequency on the CF1 pin is a measure for the current if the SEL pin is low (ground). The pulse frequency on the CF1 pin is a measure for the voltage if the SEL pin is high (VCC).
According to the datasheet, the pulses are approximately 38 us, which was confirmed with my oscilloscope.
A difference with the HLW8012 chip is that the HLW8012 sends pulses with a duty cycle of 50% where the BL0937 sends pulses with a fixed length.
Converting pulses to power
In a first attempt, I planned to convert the pulse frequency to power by using the datasheet with a complex formula based on the values of the resistors and capacitors. This is possible, but needs a calibration step.
However, why bother with complex calculations if we can use the calibration step to determine the conversion parameter? I measured the pulse frequency while measuring the power used by glowing bulbs of 75W, 40W and 25W and a heater which draws about 1100W (measured via commercial power meter).
I found out that the pulse frequency was quite lineair to the power consumed, as expected, only one bulb differed. From this measurements I could determine the conversion factor. For my switches, this factor was 1.47 (Power in W = pulse frequency * 1.47).
In my setup, measuring the constant mains voltage is not interesting for me, I only use the CF pulses for measuring the power and not the CF1 pulses. If the mains is stable (in my case 230 V AC), you can calculate the current from the power. and the voltage, the CF1 reading is not required.
In my code, the measurement of the pulses starts when the relay is switched on. When the relay is switched off, the device itself consumes some energy for the ESP which is connected to WiFi and also by the other components in the switch. I estimate the quiescent current will be about 130 mA at 3.3V, which results in < 0.5W, I neglect this.
Measuring energy
The measured power in W is integrated to calculate the energy consumed in kWh. In my code the measuring interval is set to 5 seconds, for my application, this is sufficient.
While the smart switch is connected to mains, it keeps adding the consumed energy. There is no reset function implemented, other than unplugging the switch.
The Software/firmware
The code is in my Github.
In the software a simple webinterface is implemented, see attached picture.
The code is backwards compatible with switches with do not have power metering function, just select the correct type in the lines:
- #define BRAND BSD34
- #define DEVICE_TYPE4 4
The pulses of the BL0937 are measured via a ISR (interrupt service routine). A large advantage of an ISR is that it runs in the background and is very fast so that no pulse is missed. However, an ISR is quite special. After a lot of errors and crashed of the ESP, I found out that in the newer ESP8266 definitions in the Arduino IDE (I am on 2.7.4 now), you must define in which part of the memory the ISR should run, by using this line before the setup() function, where CF_impulse() is my ISR:
void ICACHE_RAM_ATTR CF_impulse();
I found this solution on this page of the Arduino forum.
If you want to use my code, please define the WiFi credentials and the mqtt server. You can change the code to read the CF1 pulses, however, I did not work that out completely.
OTA update of the firmware is supprted via the ESP8266HTTPUpdateServer library.
The Automation in Node Red
See the attached picture for the Node Red flows to use the smart switch as a standby killer.
The flow is published here. Just copy the text to your clipboard and import the flow in Node Red.
When the switch is switched on, the flow variable "switch10_on" is set to 'true'. If the power is below the defined threshold of 2W, a delay is started and the flow variable "timer" is set to 'true'. The threshold is coded in the switch node.
If the delay has ended and the power was below the threshold during the running of the timer, the switch is switched of via the 'OFF' command.
Automation Via Openhab
The parts concerning a standby killer switch in my Openhab3 files are published on my Github.
See this Instructable how I set up Openhab and MQTT.
My 'Switch10' has a corresponding 'Thing' in Openhab: mqtt:topic:mosquitto:Switch10.
This 'Thing' has 4 channels which are linked to the MQTT topics:
- Relay - control the Relay ON/OFF via Openhab or other controllers.
- RelayState - the actual state of the relay which is communicated from the switch
- PowerW - the power in Watt
- kWh - the amount of energy consumed
The 4 channels are linked to 'items' in the items file. These items are also shown in the sitemap
In the Openhab rules files, there are 2 rules:
- If the power received an update the first rule checks whether the power is below the defined threshold. If the debouncetimer is not running, it starts to run and if it ends, the switch is switched off. If the debouncetimer is running and the power is above the threshold, the debouncetimer is cancelled.
- If the switch is switched of manually or via another process, the debouncetimer is cancel. This is necessary to prevent that the timer is started twice or ends when it should not.