Flashing Sonoff Mini R2 running firmware 3.7.6 instructions

Step One, Prepare the Sonoff Mini R2:

Disconnect power.
Connect the Sonoff Mini R2 to power.
(Neutral, White or Return to “N In” and Hot, Black or Line to “L In” terminals.)
Reconnect power.
Observe three blue flashes, pause, three blue flashes, etc.
(If you do not see this flash sequence, press and hold the button for 5 seconds.)

Step Two, Pair in Ewelink App:

Open Ewelink App.
Press blue “+” at the bottom.
Select “Quick Pairing”
Select “Add one device”
Enter your home WiFi name and password at the bottom and choose “Next”
Wait for the device to be found (up to 3 minutes)
When you see “Device added successfully” give it a throwaway but unique name.
Select the device in the App. Toggle it.
Go to “Device Settings”, “Basic Info”, “Current Version. Confirm firmware 3.7.6 or upgrade it.
It may take awhile for the App to offer you a firmware upgrade.
Scroll down to the bottom of “Device Settings” and make a note of the Mac address. Write it down.

Step Three, find the IP Address:

Use your router or network scanner software to find the IP address of the MAC address you just wrote down.
(I can’t really walk you through this part, every router is different.)
Write down the device IP Address.

Step Four, reset the Sonoff Mini R2:

Hold down the button for 5 seconds until you see 3 blue flashes, pause, three blue flashes.
Hold down the button for 5 seconds again, until you see steady blinking.

Step Five, Delete the device from the Ewelink App:

Open the Ewelink App.
Select the device. It will be offline.
Select “…” in the top right corner.
Scroll to the very bottom and select “Delete device” then “Confirm”.
Step six, Sonoff Mini R2 hotspot:
Connect to “ITEAD-numberandletters” with the password “12345678”
In your browser, visit “10.10.7.1”
Choose “WIFI SETTING” and enter your home WiFi name and Password and select save.

Step Six, Script:

Follow the instructions at GitHub - njh/sonoff-ota-flash-cli: Bash script to perform an OTA (Over the Air) firmware update for Sonoff 'DIY Mode' device on the command line
The script that worked best for me was:
./sonoff-ota-flash.sh -i 192.168.x.xxx
(replace the IP address with the IP address of the device you found in step three)
Be patient for the script to find, flash and let the device reboot. Give it several minutes.

Step Seven, Tasmota hotspot:

Connect to “tasmota_lettersandnumbers” no password required.
In your browser, visit “192.168.4.1”
Under “Wifi parameters” enter your home WiFi name and Password and select save.
Give it time to reboot and connect to your home WiFi.

Step Eight, Tasmota:

Enter the IP address of the device you found in Step Three.
If you cannot connect, repeat Step Three to find the new IP Address of the same MAC address you wrote down.
In your browser, visit that IP address. You should see Tasmota 9.5.0.
You could stop here if you like. Tasmota is pretty cool.
Leave this browser window open.

Step Nine, ESPHome:

In Home Assistant, go to your ESPHome Add-On Web UI.
Select “+ NEW DEVICE”, “CONTINUE” and select a name for your ESPHome flashed Sonoff Mini R2.
(You can change this name, but it’s a pain, so choose wisely now.)
Choose “ESP8266” and “SKIP”.
Choose “EDIT” on the new device.

substitutions:
  device_name: "the-name-you-selected-in-step-nine"

esphome:
  name: ${device_name}
  friendly_name: AnyName YouLike

esp8266:
  board: esp8285

# Enable logging
logger:

# Enable Home Assistant API
api:
  reboot_timeout: 15min  
  encryption:
    key: "yourencryptionkey"

ota:
  password: "yourpassword"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "any fallback hotspot name"
    password: "apassword"

captive_portal:

Choose “SAVE” then “X”.
Select “…” then “VALIDATE”
Select “…” then “INSTALL”, “Manual Download”, “Modern Format”.
Wait for it to compile, and download. This can take awhile.
When you see a line of green text you can select “CLOSE”.

Step Ten, Upload ESPHome Firmware in Tasmota:

Go back to the browser window you left open.
Go to “Console” and type “SetOption78 1” then press enter.
Go to “Main Menu”
Go to “Firmware Upgrade”
Select “Choose File” and navigate to wherever you saved the firmware.
When you see “Upload Successful” go to Home Assistant , ESPHome.

Step Eleven, ESPHome:

In Home Assistant, go to your ESPHome Add-On Web UI.
Wait for the device to reboot and show as ONLINE.
Choose “EDIT” on the new device.
Add the rest of the yaml code below “captive_portal:” to fully enable the device:

web_server:
  port: 80

sensor:
  - platform: wifi_signal
    name: ${device_name} Wifi Signal Strength
    update_interval: 60s
  - platform: uptime
    name: ${device_name} Uptime

#######################################
# Device specific Config Begins Below #
#######################################

binary_sensor:
  - platform: gpio
    pin: GPIO00
    id: reset
    internal: true
    filters:
      - invert:
      - delayed_off: 10ms
    on_press:
      - switch.toggle:
          id: relay_1

  - platform: gpio
    name: ${device_name}_status
    pin: GPIO04
    id: switch_1
    on_press:
      then:
        - switch.toggle:
            id: relay_1
    on_release:
      then:
        - switch.toggle:
            id: relay_1

switch:
  - platform: gpio
    name: ${device_name}_switch
    icon: "mdi:lightbulb_outline"
    pin: GPIO12
    id: relay_1
    restore_mode: restore_default_off

status_led:
  pin:
    number: GPIO13
    inverted: true

output:
  - platform: esp8266_pwm
    id: blue_led
    pin: GPIO13
    inverted: True

light:
  # the 4 lines below define the Blue LED light on Sonoff Mini, to expose in HomeAssistant remove line "internal: true"
  - platform: monochromatic
    name: ${device_name}_blueled
    output: blue_led
    internal: true # hides the Blue LED from HomeAssistant

Save.
Validate.
Install → Wirelessly.
Wait for it to compile, upload and reboot. This can take awhile.
When you see a bunch of purple text you can select “STOP”.

Step Twelve, Sonoff Mini R2:

Disconnect power.
Disconnect the device from wires.
Ideally mark it “ESPHome” or something with a permanent marker.
Install it for real.

2 Likes

Hi!
I’m having an issue (that’s actually happening in 3.6.0 as well as after updating to 3.7.6) in which I can get the response on the Module Info but it hangs with an empty reply whoen trying to do the ota_unlock…
Have you run into some simmilar issue? I’ve now tried with two brand new MINI R2.

Thanks a lot!

I think this happens when the device isn’t first paired in the App.

You’ve got to pair it,
Reset it,
Delete it from the eWeLink App,
Then connect to DIY mode,
Connect it to your router
Then run the scrypt.

Hi! Thanks for such useful article! Is there a way to do so on Windows?

If /zeroconf/ota_unlock gets stuck then the Sonoff’s servers are broken probably.

To bypass OTA unlock servers, redirect apid.coolkit.cn from DNS server (at least with EU Mini R2) to your local server and make http://your_server/v2/d/otaflash return json response of { "error": 422 }.

After flashing, if you care, success/error code gets POST’ed to /api/device/otaFlash as {"error":0} etc.

Or if any api calls get stuck, set also correct deviceid in post data excluding /zeroconf/info.

This does’t seem to work for me, though it looks like I’ve spoofed the response correctly:

siftah@fozzie ~ % curl -v -X POST http://apid.coolkit.cn/v2/d/otaflash --data-raw '{"deviceid":"10009be24e","data":{} }'

Note: Unnecessary use of -X or --request, POST is already inferred.

* Trying 192.168.1.253:80...

* Connected to apid.coolkit.cn (192.168.1.253) port 80 (#0)

> POST /v2/d/otaflash HTTP/1.1

> Host: apid.coolkit.cn

> User-Agent: curl/8.1.2

> Accept: */*

> Content-Length: 36

> Content-Type: application/x-www-form-urlencoded

>

< HTTP/1.1 200 OK

< Date: Wed, 29 Nov 2023 13:14:05 GMT

< Server: Apache/2.4.52 (Ubuntu)

< Transfer-Encoding: chunked

< Content-Type: application/json

<

* Connection #0 to host apid.coolkit.cn left intact

{ "error": 422 }**%**

Times out on this every time, regardless of using the spoofed address or hitting the ITead endpoint directly :frowning: it looks like a firmware bug.

Anyone found a fix for this yet? I had a bag of 6 mini’s that I bought a while back and hadn’t used. Stupidly I connected one to eWeLink and updated it to the latest firmware. Now I cannot get it to Ota_unlock. The other 5 I just connected straight to my network (after establishing you no longer need SonoffDiy to do this and that “ota_unlock” needs an internet connection to work) I flashed them all literally within minutes with no issues. I can info the device and it comes back with the info but the ota command just hangs. Although 5 successfully flashed and one stuck on so off firmware isn’t bad it’s really annoying that I can’t flash the last one.

Thanks for sharing @johno40.

I can run all the commands mentioned in the link except for the crux one to unlock the OTA. I send the command and it gets no response. All other commands return a successful response.

I noticed the FW I have out of the box is 3.7.3 so that may have changed things. I’ll continue to search for a solution but thought I’d share my findings here in case someone else experiences the same.

Thanks

Gave up in the end and did a serial flash using tasmotizer Releases · tasmota/tasmotizer (github.com), worked fine and I have to say what an amazing little app. I have flashed quite a few sonoff’s over the years and this little app is absolute genius and soooooo easy!! I also had to replace a T1 wall socket as the old one broke. Using this app and a few bent header pins it literally took 5 minutes.

It was a little fiddly but managed to hold the pins onto the little pads on the bottom of the device and power up whilst holding the switch, then got my son to press the tazmotize button and held my breath :smile:

Don’t update the firmware on the D1 Mini. I had the same problem

I ended up flashing them through a serial connection. Really fiddly but with some help from my son to click the buttons while Dad held things together - it was done.

Overall, yes, it seems updating to the latest firmware removes the ability to ota_unlock. I was even spoofing the server it queries but nothing seemed to respond.

Fortunately, they’re off the cloud now and in my control with Tasmota.

I’m having some mixed results with this.
Bought 4 R2 minis.
1st one connected and updated to 3.7.6 and was struggling to get it into DIY mode. And once i got it into the DIY mode. When I ran the scripts it was stuck at Getting Module info.

The other three I never connected to the ewelink at all.
Did the DIY mode directly and it just worked.

So I went back to the first one again with the knowledge of the three successfull flashing sessions.

Managed after a lot of back and forth ( seems like my devices want 10x2 seconds and not 5x2 seconds that is written everywhere to enter DIY + some timing) to get it into DIY mode again.
Tried by re-connecting it first to the eWLink app again.
Then into DIY to change network.
It shows up in the correct network.
But still the same issue, script is stuck at Getting Module Info… =(
So I assume it’s also struggling with the OTA unlock?
Or should it be able to fetch the module info not matter what?

1 Like

Managed to solve this! ( note, me and the formatting on HA forum is not agreeing with each other so i hope this is readable enough)

Answer lay in this thread: Sonoff_Devices_DIY_Tools/issues/45
All appreciation goes to all the great people in that thread that figured this out!

The TL;DR is
There are two issues when running the 3.7.6 firmware.

  1. The R2 does not like it when one adds the device to the json request.
  2. It’s trying to call to this server: apid.coolkit.cn when performing the OTA Unlock. And it’s no longer accessible.

How-To

  1. There is a modded version of njh/sonoff-ota-flash-cli that comments out the device parts. It can be found here: IBims1NicerTobi/sonoff-ota-flash-cli-devid-fix
    Note I did not use this one, i modded the original script on my own.

  2. A bit more complicated and requires access to docker and a DNS server.
    This is what we want to achive: R2 → DNS Server → (Reverse Proxy) → Docker Container.

Docker container:

Run this docker container: DasRed/sonoff-ota_unlock
It will start up a webserver that will mock the reqespons from apid.coolkit.cn.
Instructions on how to run it is documented on the github page. :slight_smile:

The DNS.
I am running a pihole as my networks DNS server.
Hence In the Local DNS → DNS Records section i added the address: apid.coolkit.cn and pointed to the server where i am hosting my the docker container.

Reverse proxy
This step is optional and is not needed if you for example have nothing running on your dockers hosts port 80.

On my host where I started the container I am already hosting things on port 80 ( which is the default external port on the docker container). I changed that to a free port on the host.

After I did all this , I ran the script and and everything worked just like the OP described. :slight_smile:

So back to OP Step Seven, Tasmota hotspot:

1 Like

Dear all!

I have the follow error:

Getting Module Info…
curl: (52) Empty reply from server
Error posting to: http://192.168.1.58:8081/zeroconf/info

I had follow all the steps but seems not work the script, any solution?

Thanks

1 Like

Having the same issue here… Was able to flash 3 of them by not making the update to 3.7.6 first but can’t do the last one. (I broke a pad on the serial connection… :frowning: )

Does anyone if this will work with this device?

I have the UK equivalent so it’s KS-501-1, running 3.7.6 firmware, however I’m unable to get it to work with Sonoff DIY mode, as I’d imagine the device is too old.

Model: KESEN KS-501-1 (Sonoff ITEAD)
Device ID:1000102187
App: eWelink

I prefer to use a ota method rather opening the device up if possible, thanks.

My very recent experience is that most of the guides online are missing the recent change due to firmware updates. I just flashed two Mini R2s using this procedure and it worked flawlessly. All you have to do is add device id to the post request and it will work. Most guides have deviceid:“” leaving it blank. If you put in the numbers after the ITEAD- wifi network name that is created when you put it in DIY mode, that’s the device id. So the four commands in terminal:

SONOFF_IP=“192.168.1.whatever”

curl -XPOST --header “Content-Type: application/json” --data-raw ‘{“deviceid”: “1002155229”, “data”: {}}’ http://$SONOFF_IP:8081/zeroconf/info

curl -XPOST --header “Content-Type: application/json” --data-raw ‘{“deviceid”: “1002155229”, “data”: {}}’ http://$SONOFF_IP:8081/zeroconf/ota_unlock

curl -XPOST --header “Content-Type: application/json” --data-raw ‘{“deviceid”: “1002155229”, “data”: {“downloadUrl”: “http://sonoff-ota.aelius.com/tasmota-latest-lite.bin","sha256sum”: “5c1aecd2a19a49ae1bec0c863f69b83ef40812145c8392eebe5fd2677a6250cc”}}’ http://$SONOFF_IP:8081/zeroconf/ota_flash

1 Like

Confirmed, it is working for me too. It is also working if you just delete the deviceid completely from the request body.

1 Like