1

I have a Python script that runs at startup and looks for GPIO inputs (the buttons on my PiTFT screen). I can make the buttons do things like shut down the Pi (sudo shutdown now), but I'm stumped as to how to make the buttons launch applications like lxterminal or pithos. Here's what I have for the script - can anyone suggest why the button for shutdown works but the buttons for launching apps doesn't? I have added a line in my /etc/rc.local that starts the script at startup via sudo, if that helps....

#!/bin/python
# a simple script for using the tactile buttons on the TFT

import RPi.GPIO as GPIO
import time
import os

# Use Broadcom SOC Pin Numbers
# setup with internal pullups and pin in READ mode

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)

#  Main Loop

while 1:
    if ( GPIO.input(22) == False ):
        os.system("sudo pithos")
        time.sleep(1)
    elif ( GPIO.input(27) == False ):
        os.system("sudo lxterminal --geometry=40x10")
        time.sleep(1)
    elif ( GPIO.input(18) == False ):
        os.system("sudo shutdown -h now")
        time.sleep(1)
ahmetertem
  • 424
  • 1
  • 5
  • 18
Wolfgang
  • 11
  • 1
  • 3
  • Have you confirmed the code takes the paths by adding a print statement? By the way you should only use sudo when needed. – joan Apr 15 '16 at 08:12
  • does it help if you add sleep before the python launch command. All my scripts in python use it. I think it should help – Shan-Desai Nov 19 '16 at 20:09

3 Answers3

1

You may use subprocess.call() function instad of os.system (reference link)

import subprocess
subprocess.call(["sudo", "lxterminal", "--geometry=40x10"])
ahmetertem
  • 424
  • 1
  • 5
  • 18
  • I tried that and the apps still don't launch. The Pi boots to the desktop, I punch the buttons, and still the only command that works is the one that does the shutdown.... :( – Wolfgang Apr 15 '16 at 01:58
1

This might be caused by some missing environment variables; namely the DISPLAY variable. It would explain why a command that doesn't use the display, e.g. sudo shutdown -h now, works while sudo lxterminal --geometry=40x10 doesn't.

Assuming that your display is numbered 0 (default), try adding this at the top of your python script:

import os
os.putenv('DISPLAY', ':0')

It essentially tells the system where to send your lxterminal window in case you're not starting the process manually/from terminal. You can see the number of your display by typing echo $DISPLAY in terminal:

$ echo $DISPLAY
:0
jDo
  • 516
  • 2
  • 6
  • Tried that - the PiTFT LCD display had a display value of :0.0 - and still nothing. I think I need to figure out how to make my script write a logfile so that I can try to debug it that way.... – Wolfgang Apr 16 '16 at 18:16
  • @Wolfgang Yeah, that's always a good idea; so is using the subprocess module instead of os.system and writing out the full path to executables such as lxterminal; e.g. import subprocess, subprocess.call(["/usr/bin/lxterminal", "--geometry=40x10"]) – jDo Apr 16 '16 at 19:16
1

First of all, if you're running the script from rc.local you don't need to execute commands with sudo because they already are running with root privileges. It's redundant.

Have you confirmed that the commands you are attempting to execute are actually installed? Try running them manually from the command line.

A simple way to allow debugging is to simply insert print statements in your code:

print "debug: attempting to launch lxterminal"

They should show up in a system log, or you could redirect them to someplace specific by modifying the line that calls your script in rc.local like this:

/usr/local/bin/script.location.py >> /var/log/custom.script.log 2>&1

Maybe that will provide more information you can use to figure out what is going on.

Best of luck!

Jon Musselwhite
  • 774
  • 4
  • 16