20170327_233139s

The ESPurna board, a smart wall switch with power monitoring

If you have read me, you might know I have a firmware for ESP8266-based smart switches called ESPurna. The firmware integrates with Alexa, Domoticz, Home Assistant and about any other service that supports MQTT or HTTP REST APIs. It supports a variety of devices, including almost the whole Sonoff family by Itead Studio, but also some other commercially available boards and light bulbs, and open source hardware projects as well.

But sometimes you just don’t find the proper hardware for your specific case. Maybe it doesn’t expose enough GPIOs, maybe it’s short of analog ports, maybe you need a double-throw relay,… Sometimes we manage to work around these limitations of the hardware adding peripherals or using a thin iron tip. But other times the problem is that it just doesn’t fit.

And size was the main reason I started creating my own smart switch board.

The ESPurna-H board

So the ESPurna board was born, and it got the shape of my home wall switches: 50x50x20mm. Also, I added some features that I have missed in some boards or liked from others:

  • SPDT 10A relay with NO and NC connections brought out
  • Connections for external button and notification LED
  • Optical isolation between the logic circuit and the relay circuit
  • HLW8012 chip for power monitoring

20170327_233327s

The board uses the Hi-Link HLK-PM01 (Aliexpress) AC/DC that outputs 5V to up to 600mA from an 100-240VAC source. So the board is usable anywhere in the world. There are cheaper options but this one comes encapsulated and has very good reviews. Just before the transformer there is a crystal fuse in series and a varistor across input terminals. I’m also using the trustee Songle SRD-05VDC-SL-C (Aliexpress) single pole double throw relay that supports up to 10A current. The board brings out both normally open and normally closed terminals of the relay, which is very convenient when using it with multi-way switches. The relay is switched by a 2n7002 N-channel mosfet isolated from the controller by a PC817C optoisolator.

20170327_233154s

The controller can be any ESP12 module, in the pictures it is a ESP12E module by Ai-Thinker (Aliexpress). An AMS1117-3.3 (Aliexpress) does the step-down phase to power the ESP module, there is also a RESET button and a 2×5 header with the programming GPIOs and two connections for external button and LEDs.

20170327_233238s

Finally the board is ready to add power monitoring circuitry based on the HLW8012 (Aliexpress), the same IC the Sonoff POW uses. The schematic is the same as in the POW, with a 1 milliOhm current sensing resistor and a 2350:1 voltage divider to monitor the AC voltage.

The HLW8012 requires that the power line should be tied to the circuit ground. This means that you should never ever connect the board to your computer while connected to mains. Never ever flash the board while connected to mains!

Open source hardware

The ESPurna-H board is released under the Creative Commons Attribution-ShareAlike 3.0 Unported License (CC-BY-SA 3.0) and follows the terms of the OSHW (Open-source hardware) Statement of Principles 1.0. It can be checked out at my ESPurna board repository on Github.

espurna_h-schematic-v0-6

You can use the latest gerblers from the repository or open the project using Eagle +8.0. You can also order the board directly from OSH Park clicking on the link below:

Order from OSH Park

espurna_h-render-bottom-v0-6s

espurna_h-render-top-v0-6s

Please note that you will be using this board at your own risk. This product is meant to be plugged to mains and it requires a deep understanding of the perils of it. I disclaim any responsibility, risk, liability and damages arising out of death or personal injury resulting from assembly or operation of this product.

Interface

The board is ready to use an external switch or push button to trigger the relay. But I wanted to keep as much as possible of the original switch enclosure and I didn’t want to drill any holes on it.

So I started doing some tests with touch sensors like the one below. This one is a TTP223B Module Capacitive Touch Switch (Aliexpress) that sells for less than a euro shipping excluded. The sensor board fits tight in the back of the front plate of the switch and it “feels” the contact across the plastic just fine.

20170407_210704s

20170407_210716s

The switch is easy to configure from the ESPurna firmware, just define it as a BUTTON_PUSHBUTTON with no pull-up since it’s a normally low sensor.

// -----------------------------------------------------------------------------
// ESPurna
// -----------------------------------------------------------------------------

#elif defined(ESPURNA_H)

    #define MANUFACTURER        "TINKERMAN"
    #define DEVICE              "ESPURNA_H"
    #define RELAY1_PIN          12
    #define RELAY1_PIN_INVERSE  1
    #define LED1_PIN            5
    #define LED1_PIN_INVERSE    0
    #define BUTTON1_PIN         4
    #define BUTTON1_RELAY       1
    #define BUTTON1_MODE        BUTTON_PUSHBUTTON
    #define ENABLE_POW          1

Wall switch base

Another issue I had to solve was how to replace the inner mechanism of the switch with the ESPurna board so it stays put in place. The switches have a plate that is tighten to the wall switch enclosure and fixes the mechanism of the switch. The same plate has 4 pegs that clamp the switch bezel.

I decided to test doing a 3D printed plate with the same dimensions and functionality, holes to screw it to the enclosure, holes to screw the ESPurna board behind it and the pegs for the bezel. I also added a hole to reach the reset button and a window for the cables for the touch sensor and the LED.

espurna_h-base-openscads

I used OpenSCAD to design the board, code is in the “parts” folder in the repo

20170408_005204s

The 3D printed part and the original switch mechanism

20170406_110434s

The devil is in the details, but the 3D printed part did it’s job sucessfully

20170406_110257s

Installation

Once everything was ready the first installation was easy peasy. One important requirement is that you need to have both line and neutral wires on hand to power the board. Some switches only have one of them.

20170406_162459s

In this case I had a plug  and a switch on the same enclosure. I used the cables powering the plug to power the ESPurna board.20170406_163326s

The enclosure with the bezel in place. The 3D printed part keeps the hot zones out of reach and the ESPurna board safe and fixed behind it. The touch sensor is fixed tight in the back of the front plate. The two remaining cables are for the LED but I finally decided not to add it.20170406_164102s

This version of the 3D printed plate does not have the pegs to keep the front plate in place, so I decided to hot-glue the corners. It will be easy to remove and the final result is almost unnoticeable.20170406_171100s

The main difference between the original switch and the modified one (aside for it being “smart”, that is) is the user experience. Originally it was a normal two-position switch with the classical “click” feeling. Now it’s a touch switch. The touch sensor works great but it requires about 500ms contact to switch. It takes some time to get used to it.  But of course you can always ask Alexa to toggle it for you 😉

UPDATE: Check also another project based on this board, the ESPurna Smart Socket, same features, but “portable” 🙂

CC BY-SA 4.0 The ESPurna board, a smart wall switch with power monitoring by Tinkerman is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

37 thoughts on “The ESPurna board, a smart wall switch with power monitoring

  1. Sacha Telgenhof

    Great job! Especially the touch sensor part is cool. Unfortunately my house has old style wiring meaning all switches don’t have a neutral line :(. Otherwise I could definitely use your board.

    For that reason I am designing a touch switch as well using an ESP8266 (so a WiFi switch).

    Reply
    1. Xose Pérez Post author

      I thought about opening a tindie store. If there are more people interested I might do, but not sure of the kind of product. It could be the board only, a solder-it-yourself kit, a partially soldered kit (with the small SMD parts) or the complete board. At the moment I solder them manually which takes quite some time, maybe I should request a quote to get them populated at the fab…

      Reply
      1. RJ

        Great job! at that size, it should just fit behind a standard UK switch too, as long as the wall box isn’t a shallow one.

        I would definitely be interested in pre-soldered boards, I have a lot of switches i’d like to automate. soldering all those bits would take me forever

        Reply
        1. RJ

          Also.. if you are considering getting these prefabbed to sell, if you could fit another relay on there and keep the board size under 65mmx65mm it would be suitable for a standard 2-gang UK light switch. (and 4 relays for a double width switch..)

          I think there’s a market for these, in the UK at least, since the only real option for retro-fit is zwave modules, and zwave is a lot of extra hassle.

          Reply
      2. Héctor

        FYI: I would consider to get up to 8 or 10 units (finished product) that I could easily install to replace the standard switches (in a Spanish house). I feel comfortable with the software part (using home-assistant here in a raspberry pi with mosquitto MQTT) but I’m not used to solder things.
        Anyway, if you don’t manage to get a final product, thanks for sharing and kudos for this nice project!

        Reply
        1. Xose Pérez Post author

          Right now it’s not my intention to sell it a a finished product. Maybe as a kit or only the PCB with the small SMD parts soldered in place. I’m trying to get some quotes from PCBA services.

          Reply
  2. SuperToma

    That’s a wonderful job and thank you for sharing !
    Did you do all the weldings yourself ?
    I’m ok in development but I’m not used to do micro electronic 🙁

    Reply
    1. Xose Pérez Post author

      Thank you! The welding is far from perfect but now that I have some experience I’m confident with parts down to 0805. Sometimes when the legs of an IC are too close one to each other I try using solder paste and a heat gun. Anyway it’s really satisfying when everything works as expected 🙂

      Reply
  3. danimtb

    Great job!! I think it is something necessary in the IoT field and I haven’t found a sonoff like divice that suits my needs except from Itead Sonoff Touch. Have you tried that one? Is esp8285 based

    Reply
    1. Xose Pérez Post author

      I have not tried it myself. The main drawback it has (from my point of view) is that it does not support multiway switches since it features a simple SPST relay. Aside from that it’s a great product. Of course if fully replaces you current switches and I’m not sure it will fit my home wall holes…

      Reply
  4. Pingback: ESPurna-H is a Compact Open Source Hardware Board with ESP8266 WiSoC, a 10A Relay, HLW8012 Power Monitoring Chip

  5. Pingback: ESPurna smart socket - Tinkerman

    1. Xose Pérez Post author

      That’s a good question. I don’t have my computer with me right now but if I recall correctly those traces are 120 millis width. I think that’s ok for 10A if you use 2oz/ft2 copper thickness. Else you can always add extra tin (the traces are exposed).

      Reply
      1. LinAdmin

        I wrote about “wires” (20170406_162459s.jpg), coloured red, yellow etc. and not about the printed circuit.

        Reply
        1. Xose Pérez Post author

          That switch in particular controls an LED ceiling bulb so the board is switching a few watts load. But you are right, the blue wire there is maybe 1mm diameter so it can handle about 2A.

          Reply
  6. Giovanni C.

    not sure if this is the right place but, I’m trying to replicate the circuit with a board I ordered via OSHPark but I can’t find easily the 0.001ohm 2512 resistor (only from China which takes 45 days for me); any chance I can use a 0.01ohm 2512 (which I can source locally) adapting somehow the cricuit/sketch?

    Reply
    1. Xose Pérez Post author

      You can easily adapt the code to use a different shunt resistor value, just modify the POW_CURRENT_R setting in the ‘code/espurna/config/sensors.h’ file or use auto calibration if you are using ESPurna or my HLW8012 library.

      But changing the resistor has other consequences you have to be aware. First, the HLW8012 has a limit on the current measurement pins of 30.94mV RMS (https://tinkerman.cat/hlw8012-ic-new-sonoff-pow/). With a 10mOhm shunt you won’t be able to measure currents higher than 3A. On the other hand it also increases your precision.

      Also, a ten times bigger shunt dissipates ten times more power (P=I*I*R), but with a limit of 3A that’s 90mW.

      Reply
  7. Pingback: A Smart Switch Board For The ESP8266 | Hackaday

  8. threegerbilsinacoat

    The pour that extends under R42, R43, and R44 means you are effectively relying on the solder mask to provide isolation. It would probably be good to pull the pour back from those resistors.

    Reply
      1. cot.s.ton

        Maybe that could be used for the espurna sensor connection? Adding a PIR or Light sensor would round this out beautifully for replacing wall switches.

        Reply
  9. Christian Mai

    Maybe you could use the ACS712 to avoid having the connection between mains and MCU domains?

    Reply
    1. Xose Pérez Post author

      Actually that’s what I plan to do. The ESPurna-H board uses HLW8012 but I want to do another version (ESPurna-A) with the ACS712. It will probably only do current sensing but isolation could be a plus.

      Reply
      1. NdK

        Might be interesting to test using two ACS712, one for current and the other for voltage (just using a resistors ladder, like the one used for HLW8012, but no need for the last 1k). Probably the SNR will be quite low, but it’s probably worth testing.

        Reply
  10. Pingback: ESPurna-H, A Compact Open Source Hardware Wireless Power Wall Switch - Electronics-Lab

  11. Sandy

    Is this ready to sell kind of product? Did you tested it? If yes.. can you please let me know problems you are facing by using it.

    Reply
    1. Xose Pérez Post author

      I’m not planning to sell this in any way. It would probably not pass any certification in most countries. This is a “makers” project and anyone using it should know about what she’s doing. It’s ASIS.

      Reply
  12. Sandy

    When are you planning to update this? Are you planning to update espurna framework to 1.8 anytime soon?

    Reply

Leave a Reply (all comments are moderated, be patient)