Skip to content

Instantly share code, notes, and snippets.

@Arnavion
Created November 18, 2019 19:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arnavion/32bf76c0ad35318c44041a6d1f1cdb39 to your computer and use it in GitHub Desktop.
Save Arnavion/32bf76c0ad35318c44041a6d1f1cdb39 to your computer and use it in GitHub Desktop.
Flash Raspberry Pi's IP address using its LED
#!/usr/bin/awk -f
BEGIN {
while (1) {
split(exec_line_match("ip addr show dev eth0", " inet "), ip_a_parts, " ")
split(ip_a_parts[2], ip_value_parts, "/")
split(ip_value_parts[1], ip_parts, ".")
reset();
for (i = 1; i <= 4; i++) {
for (j = 0; j <= 7; j++) {
divisor = 2 ^ j
bit = int(ip_parts[i] / divisor) % 2
if (bit == 1) {
one()
}
else {
zero()
}
}
}
}
}
function exec_line_match(command, substring, result) {
result = ""
while ((command | getline) > 0) {
if (index($0, substring) > 0) {
result = $0
break
}
}
close(command)
return result
}
function reset() {
system("echo none > /sys/class/leds/led0/trigger; echo 1 > /sys/class/leds/led0/brightness; sleep 1; echo 0 > /sys/class/leds/led0/brightness; sleep 1")
}
function zero() {
system("echo 1 > /sys/class/leds/led0/brightness; sleep 0.1; echo 0 > /sys/class/leds/led0/brightness; sleep 0.9")
}
function one() {
system("echo 1 > /sys/class/leds/led0/brightness; sleep 0.5; echo 0 > /sys/class/leds/led0/brightness; sleep 0.5")
}
[Unit]
Description=Flashes led0 in morse code to signal eth0 IP
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/home/pi/ip-flash.awk
Type=simple
Restart=always
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment