customise a Sonoff smart relay (esphome 3b)

main topic shortcuts > intro 1 > intro 2 > step 1 > step 2 > step3a > step 3b > step 4

Software which allows you to ‘own’ your devices

ESPhome can be used to reprogram a ESP chip so that Home Assistant can control it. I use it to re-program a Sonoff S20 smart plug or a Sonoff relay or a Sonoff light switch. There’s a good range of nicely priced devices including the Sonoff RF Bridge that will control legacy 433MHz devices. Here are some examples:

  • I control my hot water immersion heater with a Sonoff Pow2. I programmed the heater to turn off when the water was hot – instead of turn off at a certain time. I also programmed a ‘hot water boost’ feature where I could turn on the water and it would switch off as soon as the water was hot. With the help of graphs I had a much better idea of the cost of heating water in this way.
  • I plugged a printer upstairs into a Sonoff smart socket. The printer takes a few minutes to warm up so I’m able to print something without repeatedly going up and downstairs. Also, in case I forget, the smart socket is set to switch off every night.

How to program a Sonoff wifi smart plug and control it with Home Assistant and the ESPHome plug-in.

A Sonoff smart plug is a mains socket that you can turn on remotely like any smart plug. The procedure below replaces its firmware with new firmware so that you can use it in Home Assistant. The result is a huge hike in capability and convenience. The process is a bit scary the first time through – so watch others do it on the youtube below until you’re comfortable!

Compile the ESPhome code into firmware

What we’re trying to do is to add our code to the ESP chip and replace what’s currently there. We first copy and paste this code into the ESPhome interface and add our wifi password and network settings. We can also add a wifi signal meter. We can re-edit this code for future projects.

# Enter the name you want to use 
substitutions:
   devicename: sonoff_plug
  
 esphome:
   name: {devicename}
   platform: ESP8266
   board: esp01_1m
 
wifi:
   ssid: "YOUR_SSID"
   password: "YOUR WIFI PASSWORD"
   manual_ip:
     # Set this to the IP you want for the ESP
     static_ip: 192.168.1.111
     # Set this to the IP address of the router. Often ends with .1
     gateway: 192.168.1.1
     # The subnet of the network. 255.255.255.0 works for most home networks.
     subnet: 255.255.255.0

# Enable useful feedback
 logger:
 
# Enable a connection to the Home Assistant API 
api:
  password: "YOUR Home ASSISTANT API password"
 
ota:
  password: ""

# This esnsures that the button on the smart device works as a manual override 
binary_sensor:
  - platform: gpio pin:   
    number: GPIO0   
    mode: INPUT_PULLUP   
    inverted: True 
    name: ${devicename} 
    button on_press: switch.toggle: relay 

output:
  - platform: esp8266_pwm
   id: sonoff_led
   pin:
     number: GPIO13
     inverted: True 

# This turns on an LED when the plug is active 
light:
  - platform: monochromatic
    name: ${devicename} LED
    output: sonoff_led
    id: led 
 
switch:
  - platform: gpio
    pin: GPIO12
    id: relay
 
  - platform: template
    name: ${devicename} relay
    optimistic: true
    id: relayandled
    turn_on_action:
    switch.turn_on: relay
    light.turn_on: led
    turn_off_action:
    switch.turn_off: relay
    light.turn_off: led
 
# The following can be omitted 
  - platform: restart
    name: ${devicename} restart 

sensor:
  - platform: wifi_signal
    name: ${devicename} wifi signal
    update_interval: 600s
 
  - platform: uptime
    name: ${devicename} uptime 

How to ‘flash’ or put the above code into a Sonoff device

On the right is a 3.3v ftdi which connects to the computer. On the left are dupont leads to connect to the Sonoff. A video below better explains all this.
  1. Open the Sonoff and solder four male pin headers to VCC, GND, TX and RX. Don’t connect this to mains power until you’re away from the computer and the socket has been reassembled!
  2. Use female-to-male Dupont jumper cables to connect the Sonoff to a FTDI UART board set at 3.3v.
  3. Use the code in the ESPhome node for this plug. Validate and compile the code into a binary file on your PC.
  4. Hold down the Sonoff reset button as you plug the FTDI into a USB socket on your computer. You can now release the reset button.
  5. Use Esphome-flasher to upload the binary file to the Sonoff.

Here’s the project write up for the above steps – by Juan who deserves thanks for outlining the process efficiently: https://www.juanmtech.com/how-to-get-started-with-esphome-and-sonoff/

Leave a Reply

Your email address will not be published. Required fields are marked *