jason88252
Posts: 8
Joined: Sat Feb 07, 2015 9:27 am

Send Email Gpio Using shell

Tue Mar 10, 2015 4:14 pm

Hi there
i made now a lot of shell scripting on my raspberry.

now there is one script i am stuck, the plan is to check a GPIO input and if the state changed, it sends a email.
but as soon the script is running, it sends every second a email, no matter what the input state is.

btw, the echos shows only once up and seems to work.

thanks for helping me...

Code: Select all

#!/bin/bash

echo "input3.sh written by Jason, all rights reserved"
echo "this scripts reads the input 2 of PiFace and sends an email if needed"


#Pin1
# Den Zustand des Eingangs lesen
previous=$(gpio -p read 203)

# Endlose Schleife
while true
do
  # Den Zustand des Eingangs lesen
  pin=$(gpio -p read 203)

  # Wenn der Zustand geändert hat
  if [ $pin -ne $previous ]
  then
    if [ $pin -lt $previous ]
    then
      # Eingeschaltet
    	echo "Input 3 true"
    	sleep 0.1
    	echo "sending mail..."
    	sendmail -v mail@mail.com < /var/www/mails/error.mail 
   else
      # Ausgeschaltet
      echo "Input 3 false"
    fi
  fi

  previous=$pin
  sleep 1
  done


danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Send Email Gpio Using shell

Tue Mar 10, 2015 5:36 pm

I don't have a piface but this is the standard way of doing it using a pi


Just replace the echo with your mail application
test.sh

Code: Select all

#!/bin/bash


#set GPIO17  ON  INPUT

echo "17" > /sys/class/gpio/export 2>/dev/null
echo "in" > /sys/class/gpio/gpio17/direction

gpioValue[0]="False"
gpioValue[1]="True"

previous=$(cat /sys/class/gpio/gpio17/value)

while true
 do
  pin=$(cat /sys/class/gpio/gpio17/value)
   if [ $pin -ne $previous ]
    then
     echo "GPIO17 is "${gpioValue[$pin]}
     previous=$pin
   fi
  sleep 1
 done
if you do sudo ./test.sh it should tell you the status of gpio17 when you toggle it!


oops typo corrected it is 2>/dev/null . This prevent the error when gpio17 is already exported
Daniel

Return to “Troubleshooting”