Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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