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:

6 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.

great walkthrough.

I spend some 2-3 hours trying to implement it, but just can’t make the USB serial ↔ CB2S connection.
tickeling the CEN pin for many MANY times, the CB2S restarts, but ltchiptool gui just can’t make connection (or the CB2S doesn’t go into flashing mode).

this is the second time I’m trying to UART flash a CB2S.
this time I got a separate 12V → 3.3V (adjustable) buck, since that’s what I thought was missing in the previous attempt.
everything is wired just like you described, with triple checking RX,TX directions (even swapped for some dozens trials).

soldering to the CB2S is a pain in the ass really, but I don’t understand what I’m missing.

tried again with an lms1117 3.3v, still no go.
finally disconnected the CB2S chip from the its board, and it worked immediately.

I’ve just recently replaced (as a test) one of these cb2s with esp 02s, from aliexpress.
solder-wise it’s a drop in replacement, but the pins have different names, so it’s needed to first dump the original firmware (make sure to ground IO0), and then use some tool (ltchiptool has UPK2ESPHome plugin) to get a “baseline” yaml for esphome, to then consider the pins naming on both chips, and get a final yaml that’s adapted to the esp 02s (also, need to set board: esp8285).

but as a base rule, I will now always desolder the chip from the board, before trying to flash it

Just last week I added a CB2S based lamp holder to esphome. As mentioned by Don, add it to Tuya and check the version. Mine was V1.1.15. I did try but this version cannot be localised OTA.

Easy enough to solder 4 wires to GND, 3V3, TX, & RX and then use BK7231Flasher. I only had to cycle power and the flasher tool picked it up straight away and dumped the contents. Lood openbeken and it’s then visible to HA so you can upload your yaml.

Hello Ben, thanks for sharing this, you saved me a lot of time on identifying the pins :slight_smile: May I add, that in my case (maybe just a different board version) the button is connected to P23 (ADC) instead of P11 (TX1).