I am trying to get the Raspberry Pi to shut down when a GPIO input is triggered. I have a c version of a small program and a python version which both do what I want to do but both cause high CPU load.
The c example:
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main (void)
{
wiringPiSetup ();
pinMode (5, INPUT);
for(;;)
{
if(digitalRead (5) == HIGH)
{
printf("Sleepy Pi requesting shutdown on pin 24\n");
system("sudo shutdown -h now");
break;
}
}
usleep(50000);
return 0;
}
Here the example in python:
#!/usr/bin/python
import RPi.GPIO as GPIO
import os, time
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.OUT)
GPIO.output(25, GPIO.HIGH)
print ("[Info] Telling Sleepy Pi we are running pin 25")
while True:
if (GPIO.input(24)):
print ("Sleepy Pi requesting shutdown on pin 24")
os.system("sudo shutdown -h now")
break
time.sleep(5)
Is this the right approach to what I am trying to do or is there some other way getting this done without causing high CPU load ?
Kind Regards Jan
gpio-shutdown
which should do this (although I have not tried it).