Skip to content

Instantly share code, notes, and snippets.

@devilmonastery
Last active May 4, 2022 14:12
Show Gist options
  • Save devilmonastery/448f8d858f7c0250b3c8cdee1f7528a7 to your computer and use it in GitHub Desktop.
Save devilmonastery/448f8d858f7c0250b3c8cdee1f7528a7 to your computer and use it in GitHub Desktop.
bootstrap shelly h&t
#!/bin/bash
SSID=$1
PW=$2
if [[ "${SSID}" == "" ]]; then
echo "$0 ssid password"
exit
fi
if [[ "${PW}" == "" ]]; then
echo "$0 ssid password"
exit
fi
LOGFILE=""
LOG () {
when=$(date '+%Y-%m-%d %H:%M:%S')
echo "${when}] $1"
if [[ "${LOGFILE}" != "" ]]; then
echo "${when}] $1" >> "${LOGFILE}"
fi
}
echo
echo "turn on or factory reset the shelly h&t"
echo "searching..."
while true; do
ssid=$(airport -s | grep shelly | awk '{print $1}')
if [[ ${ssid} != "" ]]; then
LOGFILE="logs/${ssid}.log"
LOG "found ${ssid}"
networksetup -setairportnetwork en0 "${ssid}" && break
break
fi
sleep 1
done
LOG "joined network '${ssid}'"
MAC=$(echo "${ssid}" | sed 's/shellyht-\(..\)\(..\)\(..\)/\1:\2:\3/g')
TARGET="192.168.33.1"
LOG "waiting for ping..."
while true; do
ping -t 1 -c 1 ${TARGET} > /dev/null 2> /dev/null && break
done
LOG "got ping..."
LOG "Setting up networking..."
WIFI=$(python -c "import urllib; print urllib.urlencode({'ssid':'${SSID}', 'key':'${PW}'})")
curl -s -o - "http://${TARGET}/settings?discoverable=1" >> "${LOGFILE}"
curl -s -o - "http://${TARGET}/settings/sta?enabled=1&${WIFI}" >> "${LOGFILE}"
echo >> "${LOGFILE}"
LOG "Joining ${SSID}..."
if [[ "${SSID}" != "fake" ]]; then
until networksetup -setairportnetwork en0 ${SSID} ${PW}; do sleep 1; done
fi
LOG "joined network '${SSID}' with '${PW}'"
LOG "Waiting for reboot..."
TARGET="${ssid}.local."
ip=""
while true; do
until ip=$(ping -t 1 -c 1 ${TARGET} | head -1 | sed 's/PING .*(\(.*\)).*/\1/g'); do sleep 1; done
if [[ "${ip}" != "" ]]; then
break
fi
done
LOG "Found at http://${ip}"
LOG "Now, update firmware via the UI."
LOG "When updated, run ./configure.sh ${ssid}"
open http://${ip}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment