Flash ESPHome on a Sonoff R2 Mini using OTA DIY mode

Buy SONOFF MiniR2

Sonoff devices can be used from their eWelink Cloud or can be also easily flashed with new firmware that allow local control on them.

For this purpose, new devices have a built in mode called DIY MODE:

The DIY Mode is designed for IoT home automation users and developers who would like to control the SONOFF device via existing home automation open-source platform or local HTTP client instead of eWeLink App. …The SONOFF devices can work in either eWeLink mode or DIY Mode, In eWeLink mode, the device is connected with eWeLink cloud and controlled by eWeLink APP, while in DIY Mode, device publishes its capability service and is controlled by HTTP Post request….

So, I want to replace ITEAD cloud with my own Home Assistant server and integrate my Sonoff Mini R2 on it using ESPHome Addon:

ESPHome is a system to control your ESP8266/ESP32 by simple yet powerful configuration files and control them remotely through Home Automation systems

The key is that Sonoff devices are mostly bases on ESP8266/ESP32 chip so it seems a great choice.

Sonoff Mini R2 is based on ESP8266 and support DIY mode with OTA (Over The Air) updates so it means we do not need to solder nothing on our device on hack it in anyway, we can just flash it with our custom ESPHome firmware using our Wi-Fi network!

Handling mains voltage can be very dangerous!!!

Enable OTA on Sonoff

By default OTA is not enabled, so we are going to enable it:

  • Connect device to the main power (ATTENTION THIS CAN BE DANGEROUS FOR YOU SAFETY)
  • Long press RESET button for 5 secs until blue led start blink
Sonoff reset
  • Scan available Wi-Fi networks from your Mobile/PC and connect to the one named “ITEAD-xxx” using defaut password: 12345678
  • Open your browser and navigate to http://10.10.7.1 to enter DIY UI
  • Click on WIFI SETTING
  • Type your Wi-Fi network Username and Password (pay attention, must be a 2.4 GHZ network or the device will not connect)
  • Whait until Success messagge will popout. This can take some seconds
  • Click on “Send a new HTTP REQUEST”:
  • Select POST mode, type following address http://192.168.xxx.xx:8081/zeroconf/switch replacing the IP address with your own SONOFF IP (use any IP sniffer APP or your router UI to find it out)
  • Select Body and RAW and type fowling text:
{
    "deviceid": "",
    "data": {
        "switch": "on"
    }
}
  • Now press SEND
  • As a result you should hear (a click) or see (if you have connected something to it) your relè switch to on, replace on with off to do viceversa
  • So now change otaUnlock setting to ON
  • Change the address to http://192.168.xxx.xx:8081/zeroconf/ota_unlock and type following Body – Raw:

{
    "deviceid": "",
    "data": {
        "otaUnlock": "on"
    }
}

  • Click SEND
  • To upload the firmware we need a Sonoff tool that ITEAD as developed for us:

    • Download Sonoff DIY tool from the following github repository:

    https://github.com/itead/Sonoff_Devices_DIY_Tools/tree/master/tool

    • Open the tool and your device should be listed automatically
    • Click on buttons Power -on-state-ON and Power -on-state-OFF, and double check everything is responding

    ESPHome Firmware

    For this purpose I choose ESPHome that will allow full integration with Home Assistant.

    • Create a new Project from your Home Assistant ESPHome page
    • Type a Name and your SSID and Password, and click NEXT
    • Click SKIP THIS TEST
    • Select ESP8266 and click NEXT
    • Click CLOSE
    • Click EDIT on the new project now available on the ESPHome page
    • Replace everything with the following code, remember the replace YOUR_SSID and YOUR_PASSWORD, and click SAVE
    esphome:
      name: sonoff-mini-r2-kitchen-led
      platform: ESP8266
      board: esp01_1m
    
    # Enable logging
    logger:
    
    # Enable Home Assistant API
    api:
    
    ota:
      password: "2e6b8w3282f509af8x914qee01f3c00f"
    
    wifi:
      ssid: YOUR_SSID
      password: YOUR_PASSWORD
    
    captive_portal: 
    
    status_led:
      pin:
        number: GPIO13
        inverted: true
    
    output:
      - platform: gpio
        id: relay_1
        pin: GPIO12
    
    light:
      - platform: binary
        id: light_1
        name: kitchen_LED_Light
        output: relay_1    
    
    binary_sensor:
      - platform: gpio
        pin: GPIO00
        id: reset
        internal: true
        filters:
          - invert:
          - delayed_off: 10ms
        on_press:
          - light.toggle: 
              id: light_1
    
      - platform: gpio
        name: kitchen_LED_Light
        pin: GPIO04
        id: switch_1
        on_press:
          then:
            - light.toggle: 
                id: light_1
        on_release:
          then:
            - light.toggle: 
                id: light_1
    
    • Click VALIDATE on your Project and wait for SUCCESS status result
    • Click INSTALL and select Manual Download, this will create and download a .bin file, wait till the end of the process, it can take a while
    • Once the file is ready go back to our Sonoff DIY tool and be ready to flash the new firmware. WARNING THIS IS NOT REVERSIBLE, stock firmware will be overwritten
    • Click Firmware flash, than Import firmware, select bin file from the previous step and click OK
    • Follow the instruction on the screen and do not restart or unplug manually the device
    • Once done the STATUS on out ESPHome page will became ONLINE so the device is ready
    • Navigate to Integrations page on your Home Assistant UI, you should find a new ESPHome node, click Setup to add it to your Home Assistant instance
    • Start your home automation!