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
  • 434
  • 1
  • 5
  • 19
Wolfgang
  • 11
  • 1
  • 3

3 Answers3

1

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

import subprocess
subprocess.call(["sudo", "lxterminal", "--geometry=40x10"])
ahmetertem
  • 434
  • 1
  • 5
  • 19
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
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
  • 794
  • 5
  • 17