morticiaskeeper
Posts: 63
Joined: Mon Aug 27, 2012 6:05 pm

Button sensing in the background

Tue Dec 15, 2015 9:12 pm

for my aquarium controller, I have various Python scripts, controlled by cron, to perform various tasks during the day.

Every 15 minutes, water and ambient temperatures are recorded to a SQL database.

At set times during the day, a lights.py script is run. This reads the current state of the GPIO pins using wiring pi.digitalRead(). The pins states are then set. Basically, no lights, half lights, full lights, half lights, no lights.

I would like to have a single button to step through the sequence. A buttons.py script detects the button press and executes lights.py.

This all works, as long as the SSH window starting the buttons.py script is open. As soon as I close the SSH connection, buttons.py stops running.

I have tried running buttons.py at various times in cron and calling it via upstart, but I can't get the button detected unless I'm running the script via SSH.

Any ideas?

PiGraham
Posts: 3391
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Button sensing in the background

Tue Dec 15, 2015 9:37 pm

Try running you button script as a background task (daemon). Then it should remain running when the terminal session that started it closes. Put & at the end of the command line. Some info here http://www.cyberciti.biz/faq/linux-comm ... ackground/

morticiaskeeper
Posts: 63
Joined: Mon Aug 27, 2012 6:05 pm

Re: Button sensing in the background

Tue Dec 15, 2015 9:56 pm

I forgot about that - I blame it on old age :-)

it now stays detecting in the background, thanks.

But...

On reboot, i still need to run the buttons.py script from SSH before it will detect. I've tried adding the command to the @reboot section of cron, and I've tried running the script from my gpioStart bash script that sets the pin modes on bootup.

User avatar
rpdom
Posts: 12741
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: Button sensing in the background

Tue Dec 15, 2015 10:00 pm

morticiaskeeper wrote:I forgot about that - I blame it on old age :-)

it now stays detecting in the background, thanks.

But...

On reboot, i still need to run the buttons.py script from SSH before it will detect. I've tried adding the command to the @reboot section of cron, and I've tried running the script from my gpioStart bash script that sets the pin modes on bootup.
@reboot should work. It does for me.

Try adding logging to the crontab line, then check the logs after a reboot

Code: Select all

@reboot ~/cron/lights >>~/tmp/lights.log 2>~/tmp/lights.err
That writes two logfiles in /tmp. One contains the normal output of the script, the other contains any errors

morticiaskeeper
Posts: 63
Joined: Mon Aug 27, 2012 6:05 pm

Re: Button sensing in the background

Tue Dec 15, 2015 10:15 pm

@reboot works, inasmuch as the cron log shows it has run.

The gpioStart bash script works out reboot, resetting the pin modes, so I added the buttons.py & call to the end, after a time delay, but no luck.

I'll try the error logging tomorrow

morticiaskeeper
Posts: 63
Joined: Mon Aug 27, 2012 6:05 pm

Re: Button sensing in the background

Wed Dec 16, 2015 7:58 pm

I tried adding the logging to crontab. the log files are created, but are blank.

/var/log/cron.log shows:

Dec 16 19:53:39 RaspiZero1 cron[862]: (CRON) INFO (pidfile fd = 3)
Dec 16 19:53:39 RaspiZero1 cron[863]: (CRON) STARTUP (fork ok)
Dec 16 19:53:39 RaspiZero1 cron[863]: (CRON) INFO (Running @reboot jobs)
Dec 16 19:53:39 RaspiZero1 CRON[892]: (root) CMD (sudo /home/pi/gpioStart.sh)
Dec 16 19:53:39 RaspiZero1 CRON[894]: (root) CMD (sudo python /home/pi/zbutton.py & >>/home/pi/button.log 2>/home/pi/button.e$

chrisco
Posts: 1
Joined: Fri Jun 06, 2014 10:14 am

Re: Button sensing in the background

Thu Dec 17, 2015 4:15 pm

Try sudo crontab -e
You need to modify cron for the root user

morticiaskeeper
Posts: 63
Joined: Mon Aug 27, 2012 6:05 pm

Re: Button sensing in the background

Thu Dec 17, 2015 7:46 pm

I did use sudo. I tend to sudo su every session. Then I used crontab -e to put the line in.

According to the log I posted, it did the job, but the button logs are blank and the button isn't being detected.

morticiaskeeper
Posts: 63
Joined: Mon Aug 27, 2012 6:05 pm

Re: Button sensing in the background

Sat Dec 19, 2015 6:23 pm

UPDATE

I moved the whole system to the destination Raspi2 today, from the Zero I was developing on.

Now the buttons Python script runs from @reboot on crontab.

The whole system now monitors and records water & ambient temperatures to a SQL database, as well as switching white LED's at time intervals, with a push button override. A constant video feed and temperature data is available via web pages.

The next job will be to trigger LED and email notifications highlighting abnormal temperatures and then I can look at feeding.

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 3 guests