Tuya WiFi smart socket CB2S ESPHome configuration

Just did some tweaking with some manually flashed Tuya sockets I have laying around. Sharing the configuration I built up after some soldering/multimetering and calibration. YMMV of course!

The important parts:

substitutions:
  bl0937_voltage_divider: "750"
  bl0937_current_resistor: "0.0025 ohm"

# The main relay is connected to P26
switch:
  - platform: gpio
    name: Socket
    pin: P26
    id: socket1
    on_turn_on:
      then:
        light.turn_on: led1
    on_turn_off: 
      then:
        light.turn_off: led1

# The button on the side of the socket is wired to P11
binary_sensor:
  - platform: gpio
    pin:
      number: P11
      mode:
        input: true
        pullup: true
      inverted: true
    name: Button
    on_click:
      min_length: 50ms
      then:
        switch.toggle: socket1

# The LED is directly controlled via the state of the relay with the switch above
light:
  - platform: binary
    name: "LED"
    id: led1
    output: light_output
    internal: true

output:
  - id: light_output
    platform: gpio
    inverted: true
    pin: P8

# The pins were checked with a multimeter and the datasheet for the built in BL0937.
sensor:
  - platform: uptime
    name: Uptime
  - platform: internal_temperature
    name: "Internal Temperature"
  - platform: hlw8012
    model: BL0937
    cf_pin:
      number: P7
      inverted: true
    cf1_pin:
      number: P6
      inverted: true
    sel_pin:
      number: P24
      inverted: true
    current:
      name: BL0937 Current
    voltage:
      name: BL0937 Voltage
    power:
      name: BL0937 Power
    energy:
      name: BL0937 Energy
    voltage_divider: $bl0937_voltage_divider
    current_resistor: $bl0937_current_resistor
    update_interval: 10s

The full config I was using for flashing is available here: ESPHome config for a Tuya CB2S WiFi smartsocket · GitHub

Pre-reqs:

  • Make sure you understand what pins are what on CB2S - LibreTiny
  • Get a solid 3.3V power supply (I used a 5V 2A power supply connected through a LM2595 DC-DC step down converter, turned to 3.3V (checked with a multimeter)
  • Get a USB ↔ UART converter

Wiring:

  • Connect GND on the USB UART to the GND on the power supply + a loose ground (you’ll need to tickle the CEN pin on the CB2S later)
  • Tack Solder the above GND to network to the GND on the C2BS
  • Tack Solder 3.3V from your power supply to the 3.3V pin on CB2S
  • Tack Solder TX on the USB UART to RX1 on the CB2S (P10)
  • Tack Solder RS on the USB UART to TX1 on CB2S (P11)

You should have:

  • 2 wires from the power supply to the CB2S (GND + 3.3v)
  • 3 wires from UART to CB2S (GND + TX + RX)
  • 1 loose wire for ground

Flashing instructions:

  • Run ESPHome dashboard (I use docker run --rm -v "${PWD}":/config --device=/dev/ttyUSB0 -it --net=host ghcr.io/esphome/esphome from a machine with docker)
  • Setup a new device. If you don’t have the Libretiny options, you may still have luck with just setting the bk72xx directive.
  • Install button
  • When it compiles, it’ll eventually say it wants to upload the new firmware. It might even give you a wiring diagram telling you the above wiring diagram. You now want to short the CEN pin to GND. Touch the loose GND wire that is connected to the GND network to the CEN pin on the C2BS.
  • When this doesn’t work, try again. It’ll eventually work :crossed_fingers:

If you have no luck, try ltchiptool gui. I had some luck getting the device into a serial mode by using “Check Chip Info” and getting a valid display. At that point - assuming you don’t disconnect anything - flashing in the ESPHome dashboard should work.

Disclaimer:

  • You are soldering wires onto a PCB - you’re voiding warranties and have a chance at bricking the device.
  • You are flashing a custom firmware to a mains voltage smart socket. Don’t be ignorant with mains voltages, you might only get one chance in the remainder of your life to play with these things.
  • If your house burns down, I take no responsibility :sweat_smile:

If you did the above successfully, congratulations! You now own the device you brought :smiley:

2 Likes

Thanks Ben.

You didn’t mention which brand & model of CB2S sockets this applies to … though there’s a good chance that they all use the same GPIO pins for the same functions.

For anyone else wanting to do this, before getting out your soldering iron I suggest checking out whether the firmware can be updated OTA (Over The Air - by wi-fi) using cloudcutter.