Arduino auto-shut off from bash script

Hi everyone

I've made a simple bash script that checks the time and sets a variable to a number based on that time, which in turn is sent to the arduino
the arduino then listens on the serial port, and depending on what number it receives, turns on some combination of 3 lights. What I'd like is some sort of failsafe where, if the script doesn't send any output (say if my PC crashes) the arduino will default to turn all the lights off. I'll attach my sketch and bash script below.

Thanks in advance for any help

The bash script:

#!/bin/bash

#configure the serial connection to talk to the arduino
stty -F /dev/ttyUSB0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts

#define all variables to be used
TIMEMIN=0
TIMEHOUR=0
DATE=0
LIGHTSON=0
PUMPONEON=0
PUMPTWOON=0
ANYPUMPON=0
FANON=0
SERIALOUT=0
TEMPTHRESHOLD=27 #if it gets hotter than this in centigrade the fan will be activated
PICNUMBER=0
WHILELOOP=0

while [ WHILELOOP=0 ]

do

reset all conditional variables to 0 at the beginning of the loop

LIGHTSON=0
PUMPONEON=0
PUMPTWOON=0
ANYPUMPON=0
FANON=0
SERIALOUT=0
TAKEPIC=0

#update the time variables
TIMEHOUR=date +%H
TIMEMIN=date +%H%M
DATE=date +%y-%m-%d

#shouldn't need this after troubleshooting is done
TIMESEC=date +%S
let "MODULUSL = $TIMEMIN % 2"
let "MODULUSF = $TIMEMIN % 3"
let "MODULUSP = $TIMEMIN % 5"

#create a folder to put the pictures in
if [ -d /home/cornucrapia/growbox/$DATE ]
then sleep 0
else mkdir /home/cornucrapia/growbox/$DATE
fi

#test whether lights should be on
#actual setting
#if [ "$TIMEHOUR" -gt 06 ] && [ "$TIMEHOUR" -lt 18 ]
#troubleshooting setting
if [ $MODULUSL -eq 0 ]
then LIGHTSON=1
echo "Lights should be turned on right now"
fi

#test whether it's time for the first pumping
#actual setting
#if [ "$TIMEMIN" -gt 0600 ] && [ "$TIMEMIN" -lt 0615 ]
#troubleshooting setting
if [ $MODULUSP -eq 0 ]
then PUMPONEON=1
echo "pumpone time"
fi

#test whether it's time for the second pumping
#if [ "$TIMEMIN" -gt 1800 ] && [ "$TIMEMIN" -lt 1815 ]

then PUMPTWOON=1

echo "pumptwo time"

#fi

#test whether either of the pumpings should be happening
if [ "$PUMPONEON" == 1 ]
then ANYPUMPON=1
fi
if [ "$PUMPTWOON" == 1 ]
then ANYPUMPON=1
fi

#Add in the fan testing condition here once you know how to read from the temp sensor
#troubleshooting setting
if [ $MODULUSF -eq 0 ]
then FANON=1
echo "fan time"
fi

#Set the serial output
#0 = default, all off
#1= lights, pump, fan
#2= lights, pump
#3= lights, fan
#4 = pump, fan
#5 = pump
#6 = fan
#7=lights
if [ "$LIGHTSON" == 1 ] && [ "$ANYPUMPON" == 1 ] && [ "$FANON" == 1 ]
then SERIALOUT=1

echo "Serialout is set to: "$SERIALOUT", should be 1"

fi
if [ "$LIGHTSON" == 1 ] && [ "$ANYPUMPON" == 1 ] && [ "$FANON" == 0 ]
then SERIALOUT=2

echo "Serialout is set to: "$SERIALOUT", should be 2"

fi
if [ "$LIGHTSON" == 1 ] && [ "$ANYPUMPON" == 0 ] && [ "$FANON" == 1 ]
then SERIALOUT=3

echo "Serialout is set to: "$SERIALOUT", should be 3"

fi
if [ "$LIGHTSON" == 0 ] && [ "$ANYPUMPON" == 1 ] && [ "$FANON" == 1 ]
then SERIALOUT=4

echo "Serialout is set to: "$SERIALOUT", should be 4"

fi
if [ "$LIGHTSON" == 0 ] && [ "$ANYPUMPON" == 1 ] && [ "$FANON" == 0 ]
then SERIALOUT=5

echo "Serialout is set to: "$SERIALOUT", should be 5"

fi
if [ "$LIGHTSON" == 0 ] && [ "$ANYPUMPON" == 0 ] && [ "$FANON" == 1 ]
then SERIALOUT=6

echo "Serialout is set to: "$SERIALOUT", should be 6"

fi
if [ "$LIGHTSON" == 1 ] && [ "$ANYPUMPON" == 0 ] && [ "$FANON" == 0 ]
then SERIALOUT=7

echo "Serialout is set to: "$SERIALOUT", should be 7"

fi
echo $TIMEHOUR":"$TIMEMIN "Serial: "$SERIALOUT
#Time to take a picture?
MODULUS=1
let "MODULUS = $TIMEMIN % 10"
if [ $MODULUS -eq 0 ]
then echo "picture time!"
streamer -f jpeg -o ~/growbox/"$DATE"/video0pic"$PICNUMBER".jpeg -c /dev/video0
streamer -f jpeg -o ~/growbox/"$DATE"/video1pic"$PICNUMBER".jpeg -c /dev/video1
let "PICNUMBER += 1"
fi

#output to the arduino
echo -n $SERIALOUT > /dev/ttyUSB0

#wait 5 seconds
sleep 5

done
exit 0

the arduino sketch. The last else is commented out because if I leave it in none of the lights ever turn on. That was my first attempt at getting them to turn off if nothing was being sent on the serial port

int lightpin = 2;
int fanpin = 3;
int pumppin = 4;
int incomingByte = 0;
void setup()
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bp
pinMode(lightpin, OUTPUT);
pinMode(fanpin, OUTPUT);
pinMode(pumppin, OUTPUT);
digitalWrite(lightpin, LOW);
digitalWrite(fanpin, LOW);
digitalWrite(pumppin, LOW);
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
//Set the appropriate pins high based on what you got from serial
/*
0 = default, all off - 48
1= lights, pump, fan - 49
2= lights, pump - 50
3= lights, fan - 51
4 = pump, fan - 52
5 = pump - 53
6 = fan - 54
7=lights - 55
/
if (incomingByte == 49)
{
digitalWrite(lightpin, HIGH);
digitalWrite(fanpin, HIGH);
digitalWrite(pumppin, HIGH);
}
else if (incomingByte == 50)
{
digitalWrite(lightpin, HIGH);
digitalWrite(fanpin, LOW);
digitalWrite(pumppin, HIGH);
}
else if (incomingByte == 51)
{
digitalWrite(lightpin, HIGH);
digitalWrite(fanpin, HIGH);
digitalWrite(pumppin, LOW);
}
else if (incomingByte == 52)
{
digitalWrite(lightpin, LOW);
digitalWrite(fanpin, HIGH);
digitalWrite(pumppin, HIGH);
}
else if (incomingByte == 53)
{
digitalWrite(lightpin, LOW);
digitalWrite(fanpin, LOW);
digitalWrite(pumppin, HIGH);
}
else if (incomingByte == 54)
{
digitalWrite(lightpin, LOW);
digitalWrite(fanpin, HIGH);
digitalWrite(pumppin, LOW);
}
else if (incomingByte == 55)
{
digitalWrite(lightpin, HIGH);
digitalWrite(fanpin, LOW);
digitalWrite(pumppin, LOW);
}
else
{
digitalWrite(lightpin, LOW);
digitalWrite(fanpin, LOW);
digitalWrite(pumppin, LOW);
}
} // end of serial available loop
/

else
{
digitalWrite(lightpin, LOW);
digitalWrite(fanpin, LOW);
digitalWrite(pumppin, LOW);
}
*/
} //end of big loop

I would store the value from the milis() function every time i actually recieved something on the serial port, then in your commented out else block i would compare the stored milis value to some timeout value, say 10 minutes (converted to miliseconds of cours) or whatever is apropriate in your case and if more time had passed without recieving anything switch everything off.

Edit:

If your program runs for extended periods of time you should also take the rollover of milis into acount. I think this happens every 49 days, but check up on it :slight_smile:

That worked perfectly! Thanks for the suggestion. This was my first post on these forums and I'm impressed with both the speed and helpfulness of the reply. Thanks again! As for the rollover, the documentation says it's 50 days, but the stuff I'm planning to run can handle being off for a second or whatever until both variables rollover and comparisons are valid, but I appreciate the warning.