Link

Sinilink XY-WFUSB USB Switch Relay

This device can be found on Aliexpress for about 5 USD.

alt text alt text

Flashing

The case can be pried open without too much difficulty. Be careful to keep both halves as parallel as possible to not break the pins that hold them together.

Flashing must be done by connecting an FTDI adapter to the 1mm pads close to the USB female port. Here is the pinout:

alt text

The pads are small, so this is a bit tricky. I did it with some sewing pins stuck into dupont wires. It could be done as well connecting alternating pins from either side of the board.

GPIO0 has to be connected to GND to enter programming mode.

GPIO Pinout

Pin Function
GPIO00 ?
GPIO01 ?
GPIO03 ?
GPIO04 Toggle Button
GPIO05 Relay and Red LED
GPIO14 Green LED
GPIO16 Blue LED

Basic Config

The following configuration mimics this Tasmota template string.

The green LED is turned on when the relay is off and vice versa. The blue LED is used as the status LED. The button toggles the state of the relay.

I had to increase the ap_timeout, this board seems to take some time to connect to the WiFi.

substitutions:
  devicename: usb_relay
  upper_devicename: "USB Relay"

esphome:
  name: $devicename
  platform: ESP8266
  board: esp01_1m

wifi:
  ...

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ...
    ap_timeout: 3min

...

status_led:
  pin:
    number: GPIO16 # Blue LED

switch:
  # Relay
  - platform: gpio
    id: relay
    pin: GPIO5

  # Green LED
  - platform: gpio
    pin: GPIO14
    id: green_led
    inverted: true # start on

  # Switch template to link relay and green LED states
  # LED is on when relay is off
  - platform: template
    id: "${devicename}_relay"
    name: $upper_devicename
    lambda: |-
      if (id(relay).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
        - switch.turn_on:
            id: green_led
        - switch.turn_on:
            id: relay
    turn_off_action:
        - switch.turn_off:
            id: green_led
        - switch.turn_off:
            id: relay

# Button
binary_sensor:
  - platform: gpio
    id: button
    pin:
      number: GPIO04
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      - switch.toggle: "${devicename}_relay"

If the USB Relay controls a light, it can also be implemented like this:

output:
  # Relay
  - platform: gpio
    id: relay
    pin: GPIO5

  # Green LED
  - platform: gpio
    pin: GPIO14
    id: green_led
    inverted: true # start on

  # Binary Output template to link relay and green LED states
  # LED is on when relay is off
  - platform: template
    type: binary
    id: "${devicename}_relay"
    write_action:
      if:
        condition:
          lambda: return state;
        then:
          - output.turn_on: relay
          - output.turn_on: green_led
        else:
          - output.turn_off: relay
          - output.turn_off: green_led

# Button
binary_sensor:
  - platform: gpio
    id: button
    pin:
      number: GPIO04
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      light.toggle: $devicename

light:
  - platform: binary
    name: $upper_devicename
    id: $devicename
    output: "${devicename}_relay"