Skip to content

Instantly share code, notes, and snippets.

@kevinkub
Last active September 15, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinkub/20d08786845fc5400d41e29026a785a5 to your computer and use it in GitHub Desktop.
Save kevinkub/20d08786845fc5400d41e29026a785a5 to your computer and use it in GitHub Desktop.
Sets macOS to continuously scan for new wifi networks. When a shell* wifi network appears, it tries to connect automatically. Afterwards the script checks for versioning information and performs multiple http requests to configure the shellys accordingly.
#!/bin/bash
#
# Automatically connects to Shelly-Wifis and performs the initial setup (on macOS).
while true; do
# Connect to Shelly wifi
echo "Started scanning for shelly Wifi networks"
shellySsid=""
while [ -z "$shellySsid" ]; do
shellySsid=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s | grep shelly | awk '{$1=$1};1' | grep -io '[a-zA-Z0-9\-]\{10,\}' | head -n 1)
done
echo "Joining network $shellySsid"
if [[ -z $(networksetup -setairportnetwork en0 $shellySsid) ]]; then
# Check Shelly device information
deviceInfo=""
while [ -z "$deviceInfo" ]; do
deviceInfo=$(curl http://192.168.33.1/shelly -m 5 -s)
done
echo "Got device info $deviceInfo"
# Apply configuration based on shelly type
function ensure_requests () {
for url in "$@"; do
until $(curl --output /dev/null --silent --head --fail "$url"); do
echo "Request to url '$url' failed. Retrying."
done
done
}
if [[ $deviceInfo == *\"type\":\"SHSW-25\"* ]]; then
urls=(
"http://192.168.33.1/settings/?mode=roller"
"http://192.168.33.1/settings/?max_power=2300"
"http://192.168.33.1/settings/sta?enabled=1&ssid=[PUT YOUR SSID HERE]&key=[PUT YOUR WIFI KEY HERE]"
)
ensure_requests "${urls[@]}"
echo "Setup for Shelly 2.5 completed successfully."
fi
if [[ $deviceInfo == *\"type\":\"SHDM-2\"* ]]; then
urls=(
"http://192.168.33.1/settings/light/0?default_state=last"
"http://192.168.33.1/light/0?turn=off&brightness=100"
"http://192.168.33.1/settings/light/0?btn_type=toggle"
"http://192.168.33.1/settings/?transition=0"
"http://192.168.33.1/settings/sta?enabled=1&ssid=[PUT YOUR SSID HERE]&key=[PUT YOUR WIFI KEY HERE]"
)
ensure_requests "${urls[@]}"
echo "Setup for Shelly Dimmer 2 completed successfully."
fi
else
echo "Could not connect to $shellySsid"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment