Last active
May 17, 2020 20:41
-
-
Save dennybaa/bfa002e5b3acd38b8f68e4555aaf8eac to your computer and use it in GitHub Desktop.
Flash sonoff with Tasmota #sonoff #tasmota
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## Hotspot should be created | |
# sonoffDiy 20170618sn | |
# ref: https://www.sigmdel.ca/michel/ha/sonoff/sonoff_mini_en.html | |
# ref: https://github.com/itead/Sonoff_Devices_DIY_Tools/blob/master/other/SONOFF%20DIY%20MODE%20Protocol%20Doc%20v1.4.md | |
download() { | |
# Download latest | |
# Get lite version (must be less 508kb!) | |
latest_url=$(curl -s https://api.github.com/repos/arendst/Tasmota/releases/latest | sed -n '/browser_download_url.*tasmota-lite.bin/ { s/.*https:\(.*\)"/https:\1/; p }') | |
latest_ver=$(echo $latest_url | sed 's/.*releases\/download\/\(v.*\)\/.*/\1/') | |
firmware_path="firmwares/$latest_ver/tasmota-lite.bin" | |
# overwrite the exising firmware | |
if [ ! -f "$firmware_path" ]; then | |
mkdir -p "$(dirname $firmware_path)" | |
echo "Downloading the latest firmware $url..." | |
curl -Lo $firmware_path "$latest_url" || exit 1 | |
echo | |
else | |
echo -e "The latest firmware has been download already...\n" | |
fi | |
} | |
checkDiy_wifi() { | |
if ( ! nmcli -t dev wifi | grep -q "*:sonoffDiy" ); then | |
echo "Create sonoffDiy hotspot with password 20170618sn and connect to it (must have Internet access!)." | |
echo "https://www.sigmdel.ca/michel/ha/sonoff/sonoff_mini_en.html#desktop_ap" | |
echo -n "Procced (press any key)" | |
read | |
echo | |
fi | |
nmcli -t dev wifi | grep -q "*:sonoffDiy" || { echo -e "Not connected. Aborting...\n"; exit 1; } | |
} | |
## ------- Script entry | |
# | |
## Check CLI | |
requires=() | |
which avahi-browse &> /dev/null || requires+=("avahi-browse") | |
which nmcli &> /dev/null || requires+=("nmcli") | |
which docker &> /dev/null || requires+=("nmcli") | |
reqs=$(printf ', %s' "${requires[@]}") | |
if [ "$reqs" != ", " ]; then | |
echo -e "Expecting ${reqs:1} to available. Aborting...\n" | |
exit 1 | |
fi | |
# 1. Check wifi | |
checkDiy_wifi | |
# 2. Download the latest firmware | |
export firmware_path | |
download | |
echo $firmware_path | |
# 3. Start nginx | |
## cleanup | |
function cleanup { | |
set +x | |
echo -n "Removing docker containers... " && docker rm -f sonoffdiy.$$ | |
} | |
trap cleanup EXIT | |
echo "Starting nginx docker container..." && docker run --rm --name sonoffdiy.$$ -v $(pwd)/$(dirname $firmware_path):/usr/share/nginx/html:ro -p 38181:80 -d nginx | |
echo | |
# 4. Flash | |
# -------- | |
shasum=$(sha256sum $firmware_path | cut -f1 -d' ') | |
# locate device_id and device_url | |
# read device_id device_url <<< $( avahi-browse -r _ewelink._tcp -t -p | sed -n '1d;s/.*eWeLink_\(.*\).local;\(.*\);.*/\1 \2/;s/;/:/p' ) | |
if [ -z "$device_id" -o -z "$device_url" ]; then | |
echo "Check sonoff mDNS device is avaiable..." | |
echo -e "avahi-browse -r _ewelink._tcp -t -p\n" | |
exit 1 | |
fi | |
# flash | |
server_ip=$(nmcli connection show sonoffDiy | grep IP4.ADDRESS | tr -s ' ' | cut -f2 -d' ' | sed 's/\/.*//') | |
if [ -z "$server_ip" ]; then | |
echo "Check connection sonoffDiy. Got empty address." | |
exit 1 | |
fi | |
read -r -d '' flash_payload <<-EHD | |
{"deviceid":"$device_id","data":{"downloadUrl":"http://$server_ip:38181/$(basename $firmware_path)","sha256sum":"$shasum"}} | |
EHD | |
echo "Unlock and start flasing..." | |
set -x | |
curl http://$device_url/zeroconf/ota_unlock -XPOST -d "{\"deviceid\":\"$device_id\",\"data\":{}}" && \ | |
curl http://$device_url/zeroconf/ota_flash -XPOST -d "${flash_payload}" | |
echo "Wait for firmware to be downloaded (for 45 sec)..." | |
sleep 45 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment