DIY Stand-by Killer Via Home Automation and BL0937 Explanation

3,128

6

5

Introduction: DIY Stand-by Killer Via Home Automation and BL0937 Explanation

About: I like to combine electronics like Arduino's, ESP8266 etc with 3D designing and 3D printing.

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

Step 1: 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.

Step 2: 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:

  • BSD34 based switch from Girier: link
  • NEO-coolcam (NAS-WR01W) based switch: link

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.

Step 3: 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.

Step 4: 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.

Step 5: 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.

Step 6: 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:

  1. Relay - control the Relay ON/OFF via Openhab or other controllers.
  2. RelayState - the actual state of the relay which is communicated from the switch
  3. PowerW - the power in Watt
  4. 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:

  1. 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.
  2. 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.

Automation Contest

Participated in the
Automation Contest

Be the First to Share

    Recommendations

    • First Time Author Contest

      First Time Author Contest
    • Remote Control Contest

      Remote Control Contest
    • Build a Tool Contest

      Build a Tool Contest

    5 Comments

    0
    Raviteja224
    Raviteja224

    Question 7 months ago

    Hi,

    I tried using standby killer idea for 800w power inverter backup supply. I'm using wipro smart plug which is using same power monitoring chip(BL0937).I've connected the plug at input end of inverter thinking I can measure the power consumed for charging , standby and loads (fans and lights) which are connected to inverter . But am getting strange results , such as the plug is only measuring power consumption of charging and standby but not the loads connected to it. My inverter bypasses the main supply directly to the connected load. But if I'm understanding correctly it should not stop the measurement , because I've connected at input of inverter which used for charging and bypasses to load when fully charged .

    Can someone help me to understand the behaviour.
    Thanks in advance

    0
    dickshaydle
    dickshaydle

    Question 1 year ago on Step 4

    Did you measure the power consumption of the smart plug?

    I also reflashed the firmware and found the power consumption to increase compared to the original firmware. I used the squarish plug with the BL0937 power meter chip. I measured the AC current with a multi-meter.

    original firmware:
    3.5mA => 800mW (relay off)
    5.3mA => 1200mW (relay on)

    custom firmware:
    7.8mA =>1800mW (relay off)
    8.8mA => 2000mW (relay on)
    0.3mA => 70mW (deep sleep)

    I found that when configuring esp8285 GPIO15 as an input pin, the power consumption decreased (I don't know why this works):
    4.5mA => 1000mW (relay off)
    6.5mA => 1500mW (relay on)

    This is better but still worse than the original firmware. Maybe they used some sleep modes to save power. You can feel that the smart plug gets warm after a while.

    Maybe the power consumption can be reduced even beyond the original firmware.

    0
    delphino-999
    delphino-999

    Answer 10 months ago

    Yes, my first thought was also how useful it is to replace the standby current of a device with the standby current of the smart plug...
    Your calculations however aren't quite right. You can't just measure the current and multiply it with the voltage. This is AC, so current and voltage are constantly changing and aren't necessarily in phase. By multiplying current and voltage, you get apparent power, which in the case of a purely resistive load is also the real power. But these smart plugs usually use cheap capacitor based power supplies, which causes a phase shift between the current and voltage. Long story short, your measurements are meaningless without measuring the actual real power. Also, the capacitor power supplies used by such smart plugs aren't really well suited for very low currents. So in some smart plugs, deep sleeping the ESP, while saving power, may eventually fry the power supply and/or the ESP itself.
    Anyway ... since this one of the examples of this project was to turn off phone chargers once the phone is full. Let's take that as an example. Modern phone chargers typically use less than 0.5 Watts in standby. The ESP itself consumes about 70 mA on avergae at 3.3 V, so about 0.23 W. But the power supply for the ESP isn't gonna be 100 % efficient. Since these smart plugs are built to be cheap. Let's assume a 50 % efficiency. Meaning, we need about 0.46 W to power the ESP alone, not to mention the power measuring electronics etc. which probably also need a little bit of power. This would technically be less than 0.5 W. However the ESP is also running while the phone is charging, plus the relay. A common holding current for such a relay would be 20 mA at 5 V, so about 0.1 W. That means, while the phone is charging, this setup uses about 0.66 W more than charging the phone normally. And when the phone is done charging, it uses 0.04 W less but also doesn't keep your phone full (since as soon as the charger is off, the phone is running on battery again and slowly discharging).
    I tried measuring (the real power) one of my smart plugs and got about 0.6 W when the really is off and 0.9 W when the really is on, with a power factor of around 0.4.
    In any case, it's pretty safe to say, that in most cases this setup is probably gonna use more power than not using it.
    If you can deep sleep the ESP without frying it. You could probably get this setup to actually save power. That said, saving at best 0.5 W isn't gonna pay off the cost of the smart plug any time soon. So there are probably better ways to save power and money.

    0
    Wim3d
    Wim3d

    Reply 9 months ago

    Thanks for adding your measurements to my hobby project. My estimate of about 0.5W is quite right, since you measure 0.6W. You are right that this setup will not easily save power, it is best to take the plug and adapter from the outlet. In my home my kids like to charge their phones at night, so shutting the chargers down is also desired in view of fire safety.
    I use a similar setup to shut down my 3D printer which uses about 10W in standby.
    I have not tried to put the ESP8285 or ESP8266 in deep sleep, since GPIO16 is not connected to Reset, which is needed to wake it up again.

    0
    Wim3d
    Wim3d

    Answer 1 year ago

    Thanks for your comment. I did not measure the power of the switch itself. I would expect that most of the power in the switch it used by the ESP8285 (about 80 mA @ 3.3V = 250mW) when the relay is off. Of course the transformer and other components will use some power, but your results are really high.
    My switches do not feel really warm, just a little.
    Which firmware did you use, mine?
    Can you explain what you meanin your comment on GPIO15?
    Deep sleep will reduce the power used by the ESP8285, however that is only applicable when the switch it not used, since you loose WiFi connection and the relay will be off. It is a good suggestion to put the ESP asleep after the switch had switched off due to a standby killing, when it uses > 1mW.