Giter VIP home page Giter VIP logo

sonoff-ota-flash-cli's Introduction

sonoff-ota-flash-cli

A Bash script to perform an OTA (Over the Air) firmware update for Sonoff DIY device using the command line. The script will install Tasmota by default - unless you tell it to install something different.

Modules that support the DIY Mode Protocol v2.0 (firmware 3.5.0 or higher):

What this script does:

  • Uses multicast DNS to find the name of the module on the local network
  • Looks up the IP address for the module
  • Uses the info endpoint to display some JSON about the module
  • Uses the ota_unlock endpoint to unlock the module for Over The Air updates
  • Uses the ota_flash endpoint to flash the module with Tasmota.

Installation

Either download the whole repo as a zip file or just download the bash script using:

curl -O https://raw.githubusercontent.com/njh/sonoff-ota-flash-cli/main/sonoff-ota-flash.sh
chmod a+rx sonoff-ota-flash.sh

The script uses the following commands:

  • dns-sd (used to find the module's hostname on the network on Mac OS)
  • avahi-browse (used to find the module's hostname on the network on Linux)
  • expect (used on Mac OS to timeout if dns-sd doesn't find anything)
  • dscacheutil / getent (used to resolve a local hostname to an IP address)
  • curl (used to make HTTP requests)

All of these should be installed on Mac OS by default. But if you don't have curl on your system, then you might want to install Homebrew then run:

brew install curl

On Debian / Ubuntu you may need to install the dependencies using:

sudo apt install curl avahi-utils

Usage

Given no parameters, this script will find a Sonoff module (in DIY mode) on your network and flash it with the latest version of Tasmota.

A final confirmation prompt is displayed before going ahead with flashing.

Usage: ./sonoff-ota-flash.sh [options] [<filename or url>]
If just a filename is given, it is relative to http://sonoff-ota.aelius.com/

Options:
 -i, --ipaddress <ipaddress>      Specify the IP address of the Sonoff module
 -s, --sha256 <sha256>            Specify the SHA256 sum of the firmware
 -h, --help                       Display this message

Options:

  • If you have more than one module, or auto-discovery isn't working you can use -i to specify the IP address of the module to flash
  • If you give it a filename, it will try and use that file from http://sonoff-ota.aelius.com/
  • If you give it a full URL, it will try to use that but you must either create a .sha256 file or provide the SHA256 sum using the -s command-line options

Tested Operating Systems

The sonoff-ota-flash.sh script has been tested with the following operating systems:

OS Working Notes
Mac OS 12.3.1 dns-sd/1558.0.56 curl/7.79.1
Raspberry Pi OS 11.3 avahi-browse/0.8 curl/7.74.0
Windows 10 Likely problems with Multicast DNS for device discovery?

It is possible that it may work on other OS too. If you have success on another OS, please raise a Pull Request with the details, to let other people know.

Steps to flash a Sonoff Mini module

I bought two Sonoff Mini Modules in December 2020.

They came with Firmware version 3.5.0 pre-installed on them (which uses Protocol 2.0). This meant that I didn't even have to install the eWeLink software on my phone. The OTA header on the circuit board was not populated with pins - but this doesn't matter because the unlocking can be done using software instead now.

⚠️ Warning ⚠️ Once you flash the module with a new firmware there is no going back. There is no way of installing eWeLink back on the module unless you have some other way of making and restoring a backup of the original firmware.

The first thing you need to do is to put the module into DIY mode. These steps are also described on the Sonoff DIY page.

  1. Wire up the module to mains power. The blue LED will slowly flash once every 2 seconds.
  2. Hold down the button for 5 seconds. The blue LED then started flashing quickly 3 times per 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".

Once the module is in DIY mode and connected to your network, you can run the OTA flashing bash script:

$ ./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":2,
    "error":0,
    "data":
    {
        "switch":"off",
        "startup":"off",
        "pulse":"off",
        "pulseWidth":500,
        "ssid":"test",
        "otaUnlock":false,
        "fwVersion":"3.5.0",
        "deviceid":"1000e4c17c",
        "bssid":"b4:fb:de:ad:be:ef",
        "signalStrength":-52
    }
 }

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

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

Please wait for your device to finish flashing.

Unfortunately, there isn't a way of knowing if flashing still in progress with this script. But after a short while, the device should reset and start advertising a new Wifi network called something like tasmota_62E43F-1087.

You can then connect to the Tasmota Wifi network and configure it to use your main Wifi network. Jump to the Configure Wifi section in the Tasmota Getting Started Guide for details.

License

sonoff-ota-flash.sh is licensed under the terms of the MIT license. See the file LICENSE for details.

Contact

  • Author: Nicholas J Humfrey
  • Twitter: @njh

sonoff-ota-flash-cli's People

Contributors

njh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

sonoff-ota-flash-cli's Issues

manually call /zeroconf/ota_unlock

Hello,

If i run the script, i'm getting the following error: Failed to create client object: Daemon not running

I'm running the Ubuntu app on windows installation.

Could you help me to see why this is happening? Thanks!

Will script flash tasmota to mini r3 with s-mate for no neutral solution

I used your script to flash my mini r2 and it worked great. I then realised that I have no neutral behind some of my switches in the back boxes (old house wiring). I have seen that this can be overcome using diodes and fusible resistors as shown here https://www.youtube.com/watch?v=oBR5O391m_c
however sonoff has an out of the box no neutral "mini r3 + s-mate" solution that feels less error prone and tidier. The question would your script successfully tasmotise this ota and if it can has tasmota been updated to have a configuration for this model. (some of this is clearly googleable and I apologise for my laziness in asking some of this here).

I am tempted to buy the mini r3 and s-mate and just try it working on a bench. Great work btw and thanks for sharing it.

cannot flash a Sonoff Mini

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?

Need some help with mochad

Hi Nicholas, sorry for contacting you this way but I can't find any of your working contacts.

I need to add one more function to mochad: ability to toggle unit off then on with fixed or configurable delay. I'm running mochad on RPi with X10 to toggle my lights in house but really want to be able to restart cable modem and router remotely. So, I need (on server side) to turn specific unit off, wait for nn second, than turn it on. Number of seconds can be hardcoded or whatever 😊

Worked like a charm

Work where other failed on a MiniR2 (with no jumper).

Would it work on a Sonoff TH ?

Installs Tasmota-lite, so Domoticz support missing

Worked very well for me - Mac OS 13.0 - on MiniR2, but installs an 'old' Tasmota-lite version (9.5.0), without Domoticz support.
Once configured, I did OTA upgrade to latest Tasmota version (12.4.0).
Suggest change to script, please.

Window 10 users

Can you direct me to anyone that may have addressed Window in DIY Sononff Mini R2

Empty reply from server

Hi, I am getting this error. Do you have any idea what I should do to fix it?

Thanks.

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

Looking up sha256sum for firmware
OK

Searching for Sonoff module on network...
Found module on network.
Hostname: eWeLink_10011f2dfb
IPv4 Address: 192.168.0.121

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

Mini R2 cannot send REST commands or use this script

Hi,
I recently purchased the Mini R2 and upgraded the firmware to 3.7.6. REST POST commands do not work (get stuck in sending mode). Also, this script gets stuck at "GETTNG MODULE INFO". I can ping the device and can confirm it is connected to the router. I can also control it fine using the ewelink app. Any ideas?

Enhance for Sonoff Mini Extreme

Hi,
do you think, the script can be enhanced to also flash the new Sonoff Mini Extreme (ESP32), that also runs with Ewelink SW?

flash failed ???

have done everything as described here..and everything looked great...then the sonoff mini r2 always just blinked regularly but gives no ssid (ap) ..what am I doing wrong ?

3.7.6 - Mini - Needs change to work

For some reason, the script was not working on a Mini (I think R1? I got it a long time ago) running firmware 3.7.6.
I was able to get it working by completely removing the "deviceid" from the body variable in the sonoff_http_request function, and also from the ota_flash function.

That made the script start working and got Tasmota flashed.

Bricking MiniR2

I've now bricked 2 devices trying to flash with this. I run ./sonoff-ota-flash.sh and it detects my Sonoff, gets to the step of "Please wait for your device to finish flashing." and after restarting, it never comes back. I get no wifi network. The on off switch will still toggle the relay but I can't connect to it at all. The s1 / s2 connected switch doesn't toggle the relay either. I see a slow blink on the sonoff. can you advise?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.