markkulix
Posts: 8
Joined: Mon Jul 06, 2015 6:01 pm

My very first script

Mon Jul 06, 2015 6:27 pm

Hello!

I just started yesterday to learn how to send commands to my RPI. I made a script (in *nix sh) that checks if an other device is online and depending of that RPI lights green, yellow or red LEDs. It's quite simple and you can edit it easily to do other tests.

The LEDs are connected, as usual to, GND through a resistor and GPIO connectors are connected as below

Code: Select all

#!/bin/sh

# presets
GRN=2	# GPIO #2
YLW=3 	# GPIO #3
RED=17	# GPIO #17
TARGET=192.168.1.1
alias test='ping -c 1 -q -n -W 1 $TARGET > /dev/null'	# see 'man ping' for the parameter meanings
# presets

# start test of the LEDs
raspi-gpio set $GRN op dh ; sleep 1 ; raspi-gpio set $YLW op dh ; sleep 1 ; raspi-gpio set $RED op dh ; sleep 1
raspi-gpio set $GRN op dl ; sleep 1 ; raspi-gpio set $YLW op dl ; sleep 1 ; raspi-gpio set $RED op dl ; sleep 1
raspi-gpio set $RED op dh ; sleep 1 ; raspi-gpio set $RED op dl ; sleep 1
raspi-gpio set $YLW op dh ; sleep 1 ; raspi-gpio set $YLW op dl ; sleep 1
raspi-gpio set $GRN op dh ; sleep 1 ; raspi-gpio set $GRN op dl ; sleep 1 ; raspi-gpio set $GRN op dh
# end test the LEDs

while true ;  do					# loop forever
	if (test) 					# true if $TARGET responses
	then
		raspi-gpio set $YLW op dl
		raspi-gpio set $RED op dl
		raspi-gpio set $GRN op dh		# target is "alive", so turn only the GREEN LED on
		sleep 5					# don't react too fast
	else
		raspi-gpio set $GRN op dl
		raspi-gpio set $RED op dl
		raspi-gpio set $YLW op dh 		# target _might_ be "dying", so turn only the YELLOW LED on
		sleep 5					# don't go to "dead" too fast

		if (!(test)) ; then			# test if there is still no response
			raspi-gpio set $GRN op dl
			raspi-gpio set $YLW op dl
			raspi-gpio set $RED op dh	# still no reponse, so target must be "dead". Turn only the RED LED on
			while (!(test)) ; do		# wait for response
				sleep 5			# don't react too fast
			done				# and exit when response is received
		fi

		raspi-gpio set $GRN op dl
		raspi-gpio set $RED op dl
		raspi-gpio set $YLW op dh		# target _might_ be "waking up" so turn only the YELLOW LED on
		sleep 5					# wait, there is no need to react too fast
	fi
done							# repeat from the beginning of the loop

exit 0


markkulix
Posts: 8
Joined: Mon Jul 06, 2015 6:01 pm

Re: My very first script

Tue Jul 07, 2015 3:20 pm

Hi!

I'm still testing and learning to control the GPIO-pins ;) I made a simpler variation of this using only one LED connected to BCM2. This script responds faster (< 1 sec) if there is a network failure. The LED blinks if everything is OK and keeps being on if something is wrong.

Create a file named eg. "/usr/local/bin/simpler.sh" that is a sh-script. Make it executable with "chmod +x /usr/local/bin/simpler.sh". Then you can start it with "simpler.sh". You need to be root.

You can also start it automatically when the PI reboot like this: as root do "crontab -e" and add there a line "@reboot usr/local/bin/simpler.sh". You also need to have cron installed "apt-get install cron" and running "service cron start".

And here is the simpler script:

Code: Select all

#!/bin/sh

TARGET=192.168.1.1 # my router
alias test='ping -c 1 -q -n -W 1 $TARGET > /dev/null'	# send one ping quietly, only numeric, timeout 1 sec and send output to /dev/null

while true ; do					# repeat forever

	while (test) ; do			# if the $TARGET responses blink the LED
		raspi-gpio set 2 op dl		# turn the LED off
		sleep 1				# wait for 1 sec
		raspi-gpio set 2 op dh		# turn the LED on
		sleep 0.1			# wait for 0.1 secs
	done					# until no response

	raspi-gpio set 2 op dh			# no response, so turn the LED on and keep it on
	while (!(test)) ; do			# if the $TARGET does not respond
		sleep 0				# do nothing (ping timeout is 1 sec (parameter -W 1))
	done					# until the $TARGET responds

done						# start again from the beginning

exit 0

markkulix
Posts: 8
Joined: Mon Jul 06, 2015 6:01 pm

Re: My very first script

Thu Jul 09, 2015 7:14 pm

Ok! I've been reading and learning myself. I optimized my script to as small as possible:

Code: Select all

#!/bin/sh
TARGET=192.168.1.1			# My router
alias test='ping -A -c 1 -q -n -W 1 $TARGET > /dev/null'	# send one ping quietly, only numeric, timeout 1 sec and send output to /dev/hell
while true ; do					# Repeat forever!
	while (test) ; do			# If the $TARGET responds blink the LED
		raspi-gpio set 4 op dl	# Turn the LED off (...again)
		sleep 1				# Wait for 1 sec...
		raspi-gpio set 4 op dh	# ...and turn the LED back on.
	done					# Until no response
done						# Start again from the beginning
So the LED blinks if test succeeds. Else it leaves the LED on and starts blinking it again when test is OK :-)

Can this be done with a shorter script? ;-)

User avatar
rpdom
Posts: 16741
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: My very first script

Thu Jul 09, 2015 7:49 pm

markkulix wrote:Can this be done with a shorter script?
Possibly, but not much :)

I would have chosen something other than "test" for the name of your alias command, as there is already a command called test and it could cause confusion. Actually I wouldn't have bothered with an alias inside a script anyway, I would just put the actual command, which does make it slightly shorter ;)

Code: Select all

#!/bin/sh
TARGET=192.168.1.1         # My router
# send one ping quietly, only numeric, timeout 1 sec and send output to /dev/hell
while true ; do               # Repeat forever!
   while (ping -A -c 1 -q -n -W 1 $TARGET > /dev/null) ; do         # If the $TARGET responds blink the LED
      raspi-gpio set 4 op dl   # Turn the LED off (...again)
      sleep 1            # Wait for 1 sec...
      raspi-gpio set 4 op dh   # ...and turn the LED back on.
   done               # Until no response
done                  # Start again from the beginning

markkulix
Posts: 8
Joined: Mon Jul 06, 2015 6:01 pm

Re: My very first script

Tue Jul 14, 2015 3:48 pm

Thanks for your answers!

I dropped the A and W parameters from ping. This way the LED blinks so that it is longer ON if there is slower reply.
This is logical since the LED stays lit, if there is no answer :-)

Cheers,
Markku

markkulix
Posts: 8
Joined: Mon Jul 06, 2015 6:01 pm

Re: My very first script

Fri Aug 07, 2015 8:53 pm

Ok!

I'm still testing...
Since I like archlinux better than raspian, I had to find out how to make things to work in arch:

Code: Select all

#!/bin/sh

TARGET=192.168.1.1			# My router
alias pong='ping -c 1 -q -n $TARGET > /dev/null'

gpio -g mode 17 output

while true ; do				# Repeat forever!
	while (pong) ; do		# If the $TARGET responds blink the LED
		gpio -g write 17 0	# Turn the LED off (...again)
		sleep 1			# Wait for 1 sec...
		gpio -g write 17 1	# ...and turn the LED back on.
	done				# Until no response
done					# Start again from the beginning

You need wiringpi from AUR to get it working!

Return to “Beginners”