Skip to content

Instantly share code, notes, and snippets.

@vimagick
Last active February 22, 2024 06:27
Show Gist options
  • Save vimagick/3c0c4d079b881a89cc0e2fecc5e072bd to your computer and use it in GitHub Desktop.
Save vimagick/3c0c4d079b881a89cc0e2fecc5e072bd to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Simple script to test network connectivity, and send result to a Tasmota/ESP32 RGB Light
# !!! Please implement your own "check()" function !!!
#
HTTP_API="http://192.168.1.103/cm"
MQTT_PUB="mosquitto_pub -h broker.hivemq.com -p 8883 --capath /etc/ssl/certs/ -u username -P password -q 2"
DEV_NAME="tasmota_XXXXXX"
CMND="http" # http or mqtt
mqtt() {
local topic=cmnd/$DEV_NAME/${1}
local state=${2}
$MQTT_PUB -t $topic -m $state
}
http() {
curl -s -G $HTTP_API --data-urlencode cmnd="$1 $2" >/dev/null 2>&1
}
check() {
curl -m 5 -sf ipinfo.io | jq -e '.country=="HK"' >/dev/null 2>&1
return $?
}
led() {
local color=${1^^}
local state=${2}
declare -A LED_COLORS=([RED]=1 [GREEN]=2 [BLUE]=3)
$CMND POWER${LED_COLORS[$color]} $state
}
beep() {
# buzzer <count>,<beep>,<silence>,<tune>
local value=${1}
$CMND buzzer $value
}
while :; do
echo "[$(date +%FT%T)] network testing ..."
led BLUE blink
if check; then
echo "[$(date +%FT%T)] network is good"
led RED blinkoff
led GREEN ON
else
echo "[$(date +%FT%T)] network is bad"
led RED blink
led GREEN OFF
beep 5,2,1
fi
led BLUE blinkoff
sleep 60
done
@vimagick
Copy link
Author

vimagick commented Feb 4, 2024

BlinkCount - Number of relay toggles (blinks) (does not control the status LED)

  • 0 = blink many times before restoring power state
  • 1..32000 = set number of blinks (default = 10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment