D-Star Startup with Internet check

I wrote this script to only start the d-star daemons on a DV hotspot when there is a viable internet connection.  The reason for this is to simplify “in-the-field” use and only allow the system to start when there is an available internet connection.  Without this script, it was often a problem to ensure that the daemons always started AFTER the internet became available.  This script makes it easy to use a portable DV hotspot on D-Star in a simple “plug & play” way. Copy and paste this script to any text editor of your choice.

PLEASE NOTE: This code was modeled on an ODROID C1 (same price as the raspberry pi) but should be adaptable for the raspberry pi as well.

 

Global Functions File: (copy to /scripts/functions & set executable)

 

#!/bin/bash
#################################################################
#                                                               #
# Global functions for DV Hotspot automated management          #
# release v1    3-13-2015       John Rogers     K1WIZ           #
#                                                               #
#                                                               #
#################################################################

################# Check for internet reachability               
netcheck() {
N1=`ping -c 1 8.8.8.8 | grep '1 received' | wc -l`
	echo $N1 > /tmp/.netflag
}

################# start the gateway IF it is not already running
gwstart() {
        if [ "$G1" -eq 0 ]; then
                if [ "$N1" -eq 1 ]; then
                        /usr/local/bin/dstarrepeaterd -confdir:/etc &
                        	sleep 10
                        /usr/local/bin/timeserverd -confdir:/etc &
                        	sleep 10
                        /usr/local/bin/ircddbgatewayd -confdir:/etc &
                else
                        echo "$(date) NOT STARTED - NO INTERNET CONNECTION" >> /tmp/hs-log.log
                fi
        else
                echo "GATEWAY IS UP - RETURN TO LOOP" > /dev/null 2>&1
        fi
}

################# Check status of daemons
daemon() {
G1=`ps -ef | grep [i]rcddbgate | wc -l`
	echo $G1 > /tmp/.gwflag
R1=`ps -ef | grep [d]star | wc -l`
	echo $R1 > /tmp/.rptrflag
}

################# Misc Checks
safeshut() {
pwroff=`/usr/local/bin/gpio read 27`
	echo $pwroff > /tmp/.shutflag
        if [ "$pwroff" -eq 0 ]; then
                /sbin/poweroff
        else
                return 1
        fi
}
reset() {
restart=`/usr/local/bin/gpio read 26`
	echo $restart > /tmp/.restartflag
        if [ "$restart" -eq 0 ]; then
                /sbin/reboot
        else
                return 1
        fi
}

################# Initialize GPIO Channels
gpioinit() {
	/usr/local/bin/gpio mode 21 out
		/usr/local/bin/gpio write 21 0
	/usr/local/bin/gpio mode 22 out
		/usr/local/bin/gpio write 22 0
	/usr/local/bin/gpio mode 23 out
		/usr/local/bin/gpio write 23 0
	/usr/local/bin/gpio mode 24 out
		/usr/local/bin/gpio write 24 0
}

################# Drive GPIO & LEDS
leddriver() {
	gwflag=`cat /tmp/.gwflag`
		/usr/local/bin/gpio write 21 $gwflag
	rptrflag=`cat /tmp/.rptrflag`
		/usr/local/bin/gpio write 22 $rptrflag
	net=`cat /tmp/.netflag`
		/usr/local/bin/gpio write 23 $net
	shut=`cat /tmp/.shutflag`
		/usr/local/bin/gpio write 24 $shut
}

 

Management Loop Script calling functions above: (copy to /scripts/dvstart.sh & make executable)

(call this script from /etc/rc.local and background it with a “&”)

#!/bin/bash

# THIS SCRIPT WAITS FOR VIABLE INTERNET CONNECTION BEFORE STARTING HOTSPOT
# Written by: John Rogers, K1WIZ
# Permission is granted for free use as long as this notice is intact.

. /scripts/functions


for (( ; ; ))
        do
                safeshut
                reset
                netcheck
                daemon
                leddriver
                gwstart
                sleep 10
        done

Leave a Reply

Your email address will not be published. Required fields are marked *