C version of picofssd.py

Various Python Tools that helps handling of the UPS PIco

C version of picofssd.py

Postby mikelaurense » Mon Jun 22, 2015 2:58 pm

Lately I've been having some problems with picofssd.py. Sometimes it will not start, thus it causes the Pi to not shutdown when I turn off my car (it is being used as a GPS tracker/WiFi hotspot). I'm not exactly sure what causes this problem.

To reduce memory and cpu usage, and to eliminate this as a possible problem, I've decided to make a C version of picofssd.py. Below is the code. You will need the wiringPi library. To install it:
Code: Select all
$ cd /home/pi
$ mkdir cpico
$ cd cpico
$ git clone git://git.drogon.net/wiringPi
$ cd wiringPi
$ ./build

The C version of picofssd.py:
Code: Select all
#include <stdio.h>
#include <wiringPi.h> // Include WiringPi library!

// Pin number declarations.
const int inPin = 27;  // Setup pin 27 as an input
const int outPin = 22; // Setup pin 22 as an output

int main(void) {
   pinMode(inPin, INPUT);
   pinMode(outPin, OUTPUT);
   pullUpDnControl(inPin, PUD_UP); // Enable pull-up resistor
   pullUpDnControl(outPin, PUD_UP); // Enable pull-up resistor

   while(1) {
      digitalWrite(outPin, HIGH);
      digitalWrite(outPin, LOW);
      if(digitalRead(inPin) == 0) {
         system("shutdown -h now");
   return 0;

Compile it using this command:
Code: Select all
$ gcc -o picofssd picofssd.c -l wiringPi

Also, I've eliminated any possible problems with starting this via rc.local, or problems with picofssd.py crashing and causing file safe shutdown to malfunction, by moving it to /etc/inittab. Add this to the end of the file:
Code: Select all
#Keep picofssd running

Do not forget to remove picofssd.py from /etc/rc.local!

The C version uses less memory and CPU, as shown by the output of ps auxw:
Code: Select all
root      5382  0.0  0.1   1896   528 pts/0    S+   08:45   0:00 ./picofssd

root      5406  1.7  0.8   8228  3684 pts/0    S+   08:47   0:00 /usr/bin/python /opt/pico/picofssd.py

Over the next few days, I can see if this has solved my problem, and I'll post an update. The code above is free to use for anyone.
Posts: 11
Joined: Wed Dec 31, 2014 11:38 am

Re: C version of picofssd.py

Postby Pimaster » Mon Jun 22, 2015 11:27 pm


Thank you so much for it !!!
My Warmest Regards
Warmest Regards
Designing with Mentor Graphics PADS - www.pads.com
Please read and follow the PiForum rules
Site Admin
Posts: 1618
Joined: Fri Sep 14, 2012 7:50 am

Re: C version of picofssd.py

Postby mikelaurense » Fri Jun 26, 2015 11:47 am

Over the last few days I haven't had any issues with this script. FSSD works like a charm, 30 seconds after unplugging the device (I changed it because 120 is too long for me).
Posts: 11
Joined: Wed Dec 31, 2014 11:38 am

Re: C version of picofssd.py

Postby mike506 » Fri Jan 08, 2016 4:03 am

Agreed, works great and instructions were excellent.

I've installed on Raspberry Pi 2 running Ubuntu Mate for the Pi.

Any idea how to install as system service under Mate? It evidently uses different files compared to Raspbian and has dispensed with /etc/inittab, rather using some other files. Any help would be appriciated.
Posts: 2
Joined: Tue Jan 28, 2014 5:24 am

Re: C version of picofssd.py

Postby Nelson » Mon Feb 29, 2016 2:00 pm


I use a Raspberry Pi2 with Raspbian Jessi.
The Systemd Script is the following. That Servicescript restarts automatically, when it crash's.

Code: Select all
Description=Filesystem Shutdown Script for picoUPS



/usr/local/bin/picofssd is the location of the C-Version of the FSSD Script. ;)
Posts: 3
Joined: Fri May 08, 2015 6:18 pm

Return to PIco Python and other Tools

Who is online

Users browsing this forum: No registered users and 1 guest