0

I'm a Python newb so please bear with me if I'm asking a stupid question.

I have a RPi3 running Jessie lite. I have Apache and PHP installed. I also have netatalk installed and have mounted my pi e.g. open afp://192.168.1.17 so that I can access it via OSX Finder.

In Apache I have set document root to a directory in home so that I can reach and edit files on my laptop via VS Code (installed on laptop, not RPi).

I have a really simple PHP / HTML form which sends an on/off value to a Python script which turns an LED on and off and all works perfectly. However I'm not sure how to properly go about debugging Python scripts that are called by PHP. For example my PHP file includes:

if (!empty($_GET["action"])) {
 $output = shell_exec("sudo python /home/pi/raspberry-pi-play/switchLED.py ".strtolower($_GET["action"])) ;
 echo "switching led: ". strtoupper($_GET["action"]);
}

in my Python script I have:

import sys
import RPi.GPIO as GPIO ## Import GPIO library
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
#getting the input parms
argsLen = len(sys.argv)
if argsLen == 2:
 if sys.argv[1] == "on":
 #Turn ON
 print("Turning LED ON")
 GPIO.output(7,True)

In VSCode I get a warning unable to import RPi.GPIO which I'm not sure I can do anything about, but I would like to understand how I can print out the values that are being received by the Python script. For example where can I see print("Turning LED ON") being printed? Nothing seems to show within VS Code integrated terminal.

asked Feb 1, 2019 at 21:30
1
  • i am unable to test this idea ..... stackoverflow.com/questions/26459760/… ........... $output = shell_exec("sudo python -m pudb.run /home/pi/raspberry-pi-play/switchLED.py " ............. Commented Feb 1, 2019 at 21:55

1 Answer 1

1

How about logging to a file:

import datetime
def logPrint(s):
 now = datetime.datetime.now()
 filename='/home/pi/log'+now.strftime('%Y-%m-%d')+'.txt' 
 f=open(filename,'a')
 f.write(now.strftime('%H:%M:%S')+' '+s+'\n')
 f.close()
logPrint('testing')

This will produce a file eg /home/pi/log2019-02-01.txt containing:

21:42:00 testing
answered Feb 1, 2019 at 21:48
2
  • thanks for this, works great! One quick question, if i log logPrint('arg = ' + sys.argv[1]) the log get generated. If I add a function in e.g. logPrint('arg = ' + sys.argv[1] + 'lenght = ' + len(sys.argv)) it doesn't. Is there a reason for that or is it my error?! Commented Feb 1, 2019 at 22:14
  • 1
    len(..) is an int so add str(len(..)) Commented Feb 1, 2019 at 22:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.