Brain transplant on CALEX 4-way to ESPHome M0YNG.uk

Created 2022-04-18

Tagged

A while ago I spotted some "Smart" 4-way power plugs "Reduced to Clear" in Tesco from £30 to £7.50 - so I bought two.

They are named "CALEX Holland Smart Power Plug 4-way / 4 USB", and are still listed on the Tesco website at £30

A black box with an image of the device and various logos

They use tuya - as so many things these days do, and I excitedly began the process of using Tuya-Convert to convert them to use the ESPHome firmware. Sadly these devices don't work with Tuya-Convert (at time of writing) due to the known issue of a New PSK Format and/or that they use a "WB3S" microcontroller which is not an ESP device but a custom controller tuya have started using (possibly to thwart people like me, probably for cost reasons.)

I read (somewhere) that the WB3S and the ESP8266 have the same pinout so it is theoretically possible to perform a "brain transplant" by removing the WB3S and replacing it with an ESP8266.

Before we start I need to remind you that this device has MAINS VOLTAGE in it, and you should only do this if you are confident you know what you are doing. Don't come crying to me if you die in the process, etc.

TL;DR

Performing surgery

This is a fairly simple process, but has some nuances

  1. Unscrew case
  2. Identify WB3S
  3. Remove WB3S
  4. Replace with pre-installed ESP8266
  5. Close up case

Let's go through it all step by step!

You will need

Programme the ESP8266

Before I began this I installed a very basic ESPHome firmware on the ESP8266 using the USB development board it came on.

ESP8266 developlment board connected via USB

I was able to confirm the ESP was working, and booting up, connecting to the WiFi, etc. before I did anything nasty to it.

I'll put a full configuration example below for you to start from.

Unscrew the case

Two problems here, one is that the case uses tri-wing screws and the other is that the screws are sufficiently recessed that my bit wouldn't reach. NOTE: you do not need a precision bit, these are fairly chunky screws. I had to buy a set of "security" bits and use the smallest which is much bigger than my largest precision tri-wing.

Bottom of case with six screw holes highlighted

To gain access to the screws, remove the six rubber feet. I couldn't reach the screws so used a countersink bit to drill out the top a bit, enough to reach the screws but not enough to prevent me fitting everything back together. Go slowly, too fast and you will melt the plastic rather than cutting it away. I do want to use this after I'm done and as it has mains voltage inside I don't want the cover hanging off!

Close view of drilled out screw hole - like a small impact crater

Once you can undo the screws it is simply a case of unscrewing them all and separating the cover halves. The PCB is attached to the back.

The case is open and we can see the PCB, bus bars, etc.

Identify the WB3S

The WB3S is located on the back of the PCB, when we first see the board. Remove the four small screws holding it in place and free the earth and neutral bus bars, and the four live connections from the case. You may find it helpful to unscrew the cable retainer which is using the same tri-wing screws as the case.

Turn the PCB over and look for the white rectangle overhanding the main PCB on the opposite end to the USB sockets.

The pcb is out of the case and the WB3S is highlighted

Remove the WB3S

This is a little tricky and I don't know the best way. I DO know that prying it off whilst trying to heat each pad with a soldering iron will damage the traces (possibly tearing them off the board) and destroy the main PCB, so don't do that. From the factory these boards are mostly attached with solder UNDER, not to the side, so it is very hard to get it to all melt.

I was successful by using desoldering braid and a soldering iron to remove the majority of the solder, then a heat gun to simultaneously melt the solder on all the pads and lift the entire blue PCB off the main green PCB very carefully using tweezers.

The PCB is in a clamp and the WB3S is no longer attached

I then had to repeat the process on the ESP8266 as it seems to be much easier and cheaper to buy them on development boards than as just the ESP (at least in the UK, if you don't want to wait for shipping directly from China.) Mine didn't have an overlap so I used a spudger to gently get in between the ESP's PCB and the development board and extremely carefully lifted it off whilst applying hot air.

The PCB is in a clamp and the ESP8266 is no longer attached

Replace the WB3S with the ESP8266

Hopefully this is harder to get wrong!

I placed some solder onto the now empty pads and placed the ESP8266 roughly in position. I then did a mix of melting pads directly with the soldering iron, and heating with the heat gun and pressing down with the tweezers.

This seemed to work ok, but I also flowed a little extra solder onto each pad to be sure, looking for it to appear inside the little hole in the pad.

The PCB is in a clamp and the ESP8266 has replaced the WB3S

I then went around and checked for continuity from each pad to the main PCB - ideally to the next component or connection I could see.

Close up the case

I know it's bad to close the case up before testing, but as we have mains voltage and bus bars held in place using only flexible wires I STRONGLY recommend you put everything back, screw it up, and not plug anything in until you are SURE there is no risk of the live and neutral shorting out or shocking you.

Now is the time to plug it in! If it worked you should see the LED by the USB light up, and one or two of the sockets might also have a lit LED (depending on the configuration (or lack of) you installed). It should also show up in ESPHome / on your WiFi - just like it did before when you tested it (you did test it, right?)

The rubber feet should still fit, and you could probably file/sand the rough plastic to make a nice flat finish.

Don't forget to add labels to remind you this one has ESPHome in it, and to identify the socket numbers.

Back of the closed case, it now has a Dymo label reading "ESPHOME" Front of the closed case, each socket has a Dymo label number on from 4 to 1

ESPHome configuration

It worked! Now what?

I had a "spare" PCB so I was able to trace the relays back to the controller and identify which pins control which relay. I don't think it is possible to control the USB ports, but I didn't ever try the tuya firmware so maybe I'm missing something.

You will have noticed the ribbon cable, it has seven wires. Four are used to control relays, one is ground, one is VCC, one is possibly also (a different) VCC. So there doesn't seem to be be one available to control the USB, and there isn't a relay for it, and looking at the PCB the USB ports seem to get a direct feed which is shared with the relays.

Each socket has an LED, which is tied to the relay so we don't need to control these in addition (like the Sonoff switch) and the LEDs around the power button are all hard wired too, so we can't control that either.

Your basic configuration could look something like this:

esphome:
  name: calex-4gang

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "set this / auto generated"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Calex-4Gang Fallback Hotspot"
    password: "set this / auto generated"

captive_portal:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO16
      mode:
        input: true
      inverted: true
    name: "CALEX Button"
    on_press:
      - switch.toggle: CALEX_Relay_1
      - switch.toggle: CALEX_Relay_2
      - switch.toggle: CALEX_Relay_3
      - switch.toggle: CALEX_Relay_4

switch:
  - platform: gpio
    name: "CALEX Relay 1"
    pin: GPIO14
    id: CALEX_Relay_1
  - platform: gpio
    name: "CALEX Relay 2"
    pin: GPIO5
    id: CALEX_Relay_2
  - platform: gpio
    name: "CALEX Relay 3"
    pin: GPIO15
    id: CALEX_Relay_3
  - platform: gpio
    name: "CALEX Relay 4"
    pin: GPIO4
    id: CALEX_Relay_4

It showed up in Home Assistant looking like this (after it was discovered and "configured")

Home Assistant UI showing four CALEX Relays and a button

Having Lights

I'm going to use two of these sockets to control lights, so I'd like Home Assistant to see them as lights, not switches.

To do that you need to alter the configuration slightly, remove the sockets you want to have controlling lights from the "switch" section, create a new "outputs" section and put them in there, don't give them a name (so they don't show up individually in HA). Then add a "light" section which calls on the outputs by ID, and does have a name. Like this:

switch:
  - platform: gpio
    name: "CALEX Relay 1"
    pin: GPIO14
    id: CALEX_Relay_1
  - platform: gpio
    name: "CALEX Relay 2"
    pin: GPIO5
    id: CALEX_Relay_2

output:
  - platform: gpio
    pin: GPIO15
    id: CALEX_Relay_3
  - platform: gpio
    pin: GPIO4
    id: CALEX_Relay_4

light:
  - platform: binary
    output: CALEX_Relay_3
    id: cloud
    name: "Cloud lights"
  - platform: binary
    output: CALEX_Relay_4
    id: stars
    name: "Star Lights"

Now it looks like this in Home Assistant

Home Assistant UI showing two CALEX Relays and two lights named Cloud and Star

Conclusion

What have we learned from all of this?

Is it worth it? Only you can answer that for yourself really. For me, the device was £7.50 (ok, I killed one but I can possibly patch it back together) and I like the privacy and control of having everything on my own network and not talking to random servers just to turn lights on and off. Plus I've learned a lot, had success, and some fun, and now I have a working useful device to replace multiple individual remote sockets in one room and a USB charger. So yes, for me, it was worth it.

Share this post on Mastodon