Jump to content

GPIO Buttons does not work


jps

Recommended Posts

Hi. I have made a system with O Pi Lite and Armbian to automate an aquarium from where to control the light, the bubbles and with a webcam I can see the system through the web, everything automated from Cron. From a web page I can turn on and off the lights and bubbles from web buttons. Everything is fine.

The problem arises when I want to put some phisical buttons for 3 things: turn off the system, change the state of light and bubbles. I have used a code in Python and the PYA20 library. This is the code. It works very well, but when I start it with the system, the program stop working soon and the buttons stop to work.

 

#!/usr/bin/env python
from pyA20.gpio import gpio
from pyA20.gpio import port
import subprocess
import time
import os

# PIN 29 - IN Light (luz)
onoff = port.PA7
# PIN 7  - IN Bubbles (burbujas)
luzboton = port.PA6
# PIN 8  - IN Shutdown
burbujasboton = port.PA13
# OUT Pins
luzvivo = port.PA8
burbujasvivo = port.PA9

gpio.init()
#Button ON-OFF
gpio.setcfg(onoff, gpio.INPUT)
gpio.pullup(onoff, gpio.PULLUP)
#Button light
gpio.setcfg(luzboton, gpio.INPUT)
gpio.pullup(luzboton, gpio.PULLUP)
#Button bubbles
gpio.setcfg(burbujasboton, gpio.INPUT)
gpio.pullup(burbujasboton, gpio.PULLUP)
# OUT
gpio.setcfg(luzvivo, gpio.OUTPUT)
gpio.setcfg(burbujasvivo, gpio.OUTPUT)

while True:
    # PROCESS ON-OFF need 5 seconds to avoid mistaken shutdowns
    if gpio.input(onoff) == 0:
        #start counting pressed time
        print ("pulsado")
        time.sleep(5)
        if gpio.input(onoff) == 0 :
            os.system("shutdown now -h")

    # PROCESS LIGHT
    if gpio.input(luzboton) == 0:
        print ("pulsado luz")
        estadoluz = gpio.input(luzvivo)
        gpio.output(luzvivo, not estadoluz)
        time.sleep(1)

    # PROCESS bubbles
    if gpio.input(burbujasboton) == 0:
        print ("pulsado bubbles")
        estadoburbujas = gpio.input(burbujasvivo)
        gpio.output(burbujasvivo, not estadoburbujas)
        # To avoid bounce
        time.sleep(1)

If i check the procces, i can see it, but i can also see that consumes a lot of CPU:

 

ps aux | grep botones
root       652 95.1  0.6   8552  3412 ?        Rs   10:53   2:36 python /etc/init.d/botones.py start

What i am doing wrong?

 

Thank you in advance.

Link to comment
Share on other sites

Well, now the consume of CPU lower to 0,1%... thank you very much.

 

BUT... still dying. The file is in /etc/init.d/ and have +x permission. The file starts but after a while dies.... The Python file is called "botones.py". In syslog:

 

Nov  7 16:31:03 localhost systemd[1]: botones.py.service start operation timed out. Terminating.
Nov  7 16:31:03 localhost rsyslogd-2007: action 'action 17' suspended, next retry is Tue Nov  7 16:31:33 2017 [try http://www.rsyslog.com/e/2007 ]
Nov  7 16:31:03 localhost systemd[1]: Failed to start (null).
Nov  7 16:31:03 localhost systemd[1]: Unit botones.py.service entered failed state.

If i run from the monitor, never dies....

Link to comment
Share on other sites

I can not make it work from rc.local... i tried to launch with:

 

nohup python /home/orangepi/scripts/botones.py &

and with and without sudo but nothing never runs. I tried too to run from /etc/init.d/....

 

What am i lossing?

 

Link to comment
Share on other sites

By the way, in my rc.local i have other scripts that runs normally:

 

#!/bin/sh -e

# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/etc/init.d/entrehoras.sh
# CÁMARA HP
/etc/init.d/runmjpg.sh
# Cámara GEMBIRD
#/etc/init.d/runmjpgGEMBIRD.sh
nohup python /home/orangepi/scripts/botones.py > /tmp/botones.log 2>&1
#/etc/init.d/botones.sh
exit 0

 

Link to comment
Share on other sites

BTW, you have an ampersand missing :

nohup python /home/orangepi/scripts/botones.py > /tmp/botones.log 2>&1 &

And you don't have any /tmp/botones.log created ? Strange  ! Add some print() in the python script ...

 

Try also to change the first line from " #!/usr/bin/env python" to " #!/usr/bin/python"

Link to comment
Share on other sites

Yes!. It was the "&" at the end of the calls to the scripts. The 3 of them... Now i have too a "/tmp/botones.log".

 

With that and the "sleep.time(0.1)" at the end of the while, all works fine.

 

Thank you both!!!

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines