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
http://sonoff-ota.aelius.com/
MIT License
129 stars 19 forks source link

cannot flash a Sonoff Mini #2

Closed simonckenyon closed 3 years ago

simonckenyon commented 3 years ago

when i run your most excellent script it just sits there:

./sonoff-ota-flash.sh -i 192.168.20.13 tasmota-9.2.0-lite.bin Checking new firmware file exists OK

Looking up sha256sum for firmware OK

Getting Module Info... { "seq":2, "error":0, "data": { "switch":"off", "startup":"off", "pulse":"off", "pulseWidth":500, "ssid":"koala-iot", "otaUnlock":false, "fwVersion":"3.6.0", "deviceid":"1000a05bf6", "bssid":"46:d9:e7:49:83:a8", "signalStrength":-43 } }

Unlocking for OTA flashing...

and then after a couple of minutes it says:

curl: (52) Empty reply from server Error posting to: http://192.168.20.13:8081/zeroconf/ota_unlock

i have been trying to post ota_unlock to the mini for several hours now.

am i doing something wrong?

njh commented 3 years ago

Hm! The modules aren't very good at proving feedback about what is going on.

I have experienced "Empty reply from server" but I have had some problems with the virus / web protection software on my laptop, so I wasn't sure if it was related to that (it intercepts all TCP/HTTP connections). However resetting the module/starting again did fix it. The OTA Unlock step is very fast - takes less than a second - so you shouldn't have to wait.

Have you put the module into DIY mode?

  1. Wire up the module to mains power. The blue LED will flash once every 2 seconds.
  2. Hold down the button for 5 seconds. The blue LED then started flashing quickly 3 times every second.
  3. I then had to hold down the button again for 5 seconds and it then flashed on and off quickly.
  4. Connect to the Wifi network called ITEAD-xxxxxx using your computer. The password is 12345678.
  5. Open http://10.10.7.1/ in your browser and enter your main Wifi network name and password. This is only used during the flashing process.
  6. The module will then try and connect to your Wifi network. The blue LED will then flash twice per second. My router reported that the hostname of the device is "ESP_XXXXXX".
  7. Then run my bash script...

There are a couple of things I should do:

simonckenyon commented 3 years ago

thank you for your quick response. i really regret updating the firmware to 3.6.0. the device had been sitting in the box for about a year. my bad.

what was/is not clear to me was:

njh commented 3 years ago

I think I need to go through this again to double-check, but I think once you have configured the module with your home Wifi, it stays in DIY mode.

I did have to keep having to reset it while I was experimenting/writing the script. Some HTTP requests seem to cause it to crash and I had to power-cycle it to get it back. Pressing the button for 5 seconds is described as "Factory Reset"- so I would have thought it should be possible to do it multiple times.

I am not sure if it calls home or not during the ota_unlock request. When I try doing it again, I will do a packet capture and see if there are any requests out to the Internet.

I have just tried installing the eWeLink software on my phone for the first time but I can't get it to setup my module :-( It is possible I have done something to to the module so that original firmware won't work on it anymore 😞

njh commented 3 years ago

I think the backup of my Sonoff Mini is from just after I configured it to access my home Wifi. When I do a restore, it goes back to that state just before I called ota_unlock.

I managed to do a packet capture during unlocking and it is indeed calling home:

Screenshot 2021-02-02 at 23 57 44

This is what the HTTP request looks like:

POST /api/device/otaFlash HTTP/1.1
Host: api.coolkit.cn:8081
Content-Type: application/json
Authorization: Sign 8a62a48e34cfe4e4f5f0a8810267b2af34574396f227b575839310b14ec9a67d
Connection: close
Content-Length: 52

{"deviceid":"1000e4c17c","sequence":"4058031471168"}

HTTP/1.1 200 OK
Server: openresty
Date: Tue, 02 Feb 2021 00:31:26 GMT
Content-Type: application/json
Content-Length: 13
Connection: close

{"error":422}

Weirdly unlocking worked fine, despite the error code in the response.

When I lookup api.coolkit.cn using my local resolver in the UK:

$ host api.coolkit.cn
api.coolkit.cn is an alias for api.coolkit.cc.
api.coolkit.cc is an alias for eu-api.coolkit.cc.
eu-api.coolkit.cc has address 52.57.118.192

I get the same result when resolving using using 8.8.8.8 and 1.1.1.1.

It looks like Coolkit is the people who make eWeLink.

njh commented 3 years ago

Good news, I have successfully managed to OTA a flash a Sonoff Mini running firmware version 3.6.0. Although I had real trouble getting it into DIY mode. I quit everything on my Mac apart from Safari. Went to Wifi settings and told it to forget about the ITEAD wireless network. Then after holding down the button for 5 seconds to factory reset, then holding it down for 5 seconds again to put it into "Compatibility AP" mode, I was able to connect to the Wifi network, then connect to 10.10.7.1 and set my home Wifi network name and password. It then successfully joined my home network.

Once in DIY mode, it seemed to stay in DIY mode between power cycles.

Running the bash script then worked as expected:

./sonoff-ota-flash.sh 
Checking new firmware file exists
OK

Looking up sha256sum for firmware
OK

Searching for Sonoff module on network...
Found module on network.
Hostname: eWeLink_1000e4c17c
IPv4 Address: 192.168.1.104

Getting Module Info...
 {
    "seq":3,
    "error":0,
    "data":
    {
        "switch":"off",
        "startup":"off",
        "pulse":"off",
        "pulseWidth":500,
        "ssid":"test",
        "otaUnlock":true,
        "fwVersion":"3.6.0",
        "deviceid":"1000e4c17c",
        "bssid":"b4:fb:e4:77:7f:aa",
        "signalStrength":-47
    }
 }

Unlocking for OTA flashing...
 {
    "seq":3,
    "error":0
 }

Proceed with flashing? [N/y] y
Requesting OTA flashing...
 {
    "seq":4,
    "error":0
 }

Please wait for your device to finish flashing.

Packet capture on my router showed it phoning home again. Again it got an 422 error response back in the JSON endpoint, which it didn't seem to mind.

njh commented 3 years ago

@simonckenyon did you have any success with this?

matfiz commented 3 years ago

I can just say I have managed to flash 5 Sonoff Minis flawlessly.

surenz commented 3 years ago

Just for the record I've managed to flash Sonoff Basic R3 (newer version without jumper for DIY mode) with firmware v3.6.0. The only issue was that avahi was not able to find the IP of the device but I put it with the -i parameter.

Thanks a lot for this script.

njh commented 3 years ago

I recently bought some more Sonoff Mini R2s, that came with firmware v3.6.0.

I have updated the README with more detailed instructions.

All worked fine, so closing this issue now.

phiter commented 2 years ago

I keep getting "connection reset by peer". Any idea why?

edit: I disabled the router firewall and it worked. I got it flashed with v3.6.0