jtloper1
Posts: 10
Joined: Tue Jan 20, 2015 11:37 pm

Button press script won't run in background

Wed Jun 26, 2019 7:21 am

This is the net iteration is the push button script from this topic: https://www.raspberrypi.org/forums/view ... 2&t=243196
Because the script is expected to wait for the button press I need it to in the background. The script is called from rc.local (see below) . push_button2.py below:

Code: Select all

#!/usr/bin/env python
import daemon
import time
import os
import I2C_LCD_driver
mylcd = I2C_LCD_driver.lcd()
mylcd.lcd_display_string("READY", 1)
print(time.ctime() + " Push Button Script Loaded")
import RPi.GPIO as GPIO
printer_busy = False
def button_callback(channel):
    while True:
     print(time.ctime() + " Print Button Pressed")
    global printer_busy
    if printer_busy:
        # Already printing, do nothing
        return

    printer_busy = True
#    mylcd.lcd_clear()
    mylcd.lcd_display_string("PRINTING", 1)
    os.chdir("/media/pi/BB2B-791B/sandwich")
#    os.system("sh print")
    time.sleep(1)
    mylcd.lcd_clear()
    mylcd.lcd_display_string("READY", 1)
    printer_busy = False    
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup event on pin 10 rising edge
def run():
    with daemon.DaemonContext():
        button_callback(channel)
if __name__ == "__main__":
    run()
GPIO.cleanup() # Clean up
According to the LCD behavior and log file contents that the scripts loads and then exits. There are no error messages in the log or console.
Running the code in Thorny produces
2019-06-25-235256_1920x1080_scrot.png
2019-06-25-235256_1920x1080_scrot.png (195.17 KiB) Viewed 285 times
I am unconcerned about those errors because the test script taken from here: https://stackoverflow.com/a/8375012/5651732 does the same thing in Thorny but run fine in the console and gives the expected behavior of populating the log file with the current time.

Return to “Python”