Page 1 of 1

Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 9:55 am
by mariusmilatz
Hi,

I am not very familiar with any of raspberry pi product or the scripts used to program them.

The result i am trying to accomplish:
- I am trying to create a camera with a trigger switch for recording. (when trigger pressed the camera records, when released the camera stops recording).
- Trigger switch input is 3v when not pressed & 0v when pressed.
- The script should run every time the raspberry pi is turned on.

The script that i used: ((sample.py) - saved file name)

Code: Select all

import time
import datetime
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(29,GPIO.IN,pull_up_down=GPIO.PUD.UP)

def startrecording()
	raspivid -t 600000 -ex auto -b 17000000 -o /home/pi/t1_'date+%d%m%Y_%H%M-%S'.h264

def stoprecording()
	pkill raspivid

while True:
	GPIO.wait_for_edge(29,GPIO.FALLING)
		startrecording()
	GPIO.wait_for_edge(29,GPIO.RISING)
		stoprecording()
The script after 'while True' should be on a continues loop.

To make the script run on startup i:

sudo nano /etc/rc.local
(edited the script)
sudo python /home/pi/sample.py
(line above 'Exit 0')


Once i rebooted the raspberry pi i got a 'Invalid Syntax'

Can anyone help me as i really don't know what i am doing?

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 10:04 am
by PeterO

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 10:37 am
by mahjongg
added code tags, and moved to python section

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 10:43 am
by mariusmilatz
Do you mean instead of this:

while True:
GPIO.wait_for_edge(29,GPIO.FALLING)
startrecording()
GPIO.wait_for_edge(29,GPIO.RISING)
stoprecording()

Write this:

If true :
GPIO.wait_for_edge(29,GPIO.FALLING)
do_this startrecording()
GPIO.wait_for_edge(29,GPIO.RISING)
do_this stoprecording()

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 11:18 am
by Heater
1) You did say in the OP "The script after 'while True' should be on a continues loop." So "while True" would be the thing not "if"

2) Your "if true" would be useless. "True" is always true so the test is redundant.

3) Did you mean "if True". I think using a lower case "true" is an error.

4) Without proper indentation your code will fail at the "if" clause.

5) You did not use code tags around your example so it's gibberish anyway.

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 11:21 am
by DirkS
Heater wrote:
Fri Aug 17, 2018 11:18 am
5) You did not use code tags around your example so it's gibberish anyway.
mahjongg added them more than half an hour before you posted this...
You should also be able to see the indentation when you quote the post

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 11:38 am
by mariusmilatz
I am quite confused now.

This is the code that i am using at the moment which gives me the 'Invalid Syntax' message.

Code: Select all

import time
import datetime
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(29,GPIO.IN,pull_up_down=GPIO.PUD.UP)

def startrecording()
	raspivid -t 600000 -ex auto -b 17000000 -o /home/pi/t1_'date+%d%m%Y_%H%M-%S'.h264

def stoprecording()
	pkill raspivid

while True:
	GPIO.wait_for_edge(29,GPIO.FALLING)
		startrecording()
	GPIO.wait_for_edge(29,GPIO.RISING)
		stoprecording()

- Should I be using 'while True' or 'if True'?
-Code tag. "pkill raspivid" or pkill raspivid. is this what is meant by code tags?

[MOD Edit, again added code tags]

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 11:49 am
by elParaguayo
As above, please make sure your code is in code tags (read the link in PeterO's post). Without it, we lose the indentation and your code will make no sense.

Next, when posting about python errors, post the whole error message (which includes line references) so we can home in on the relevant part.

The bits that immediately jump out at me are:

Code: Select all

def startrecording()
    raspivid -t 600000 -ex auto -b 17000000 -o /home/pi/t1_'date+%d%m%Y_%H%M-%S'.h264

def stoprecording()
    pkill raspivid
The raspivid and pkill lines are not python commands and so the python interpreter won't know how to handle them.

To run shell commands, you need to use the subprocess module. Something like this:

Code: Select all

import subprocess

def startrecording()
    subprocess.call(["raspivid", "-t", "600000", "-ex", "auto", "-b", "17000000", "-o", "/home/pi/t1_'date+%d%m%Y_%H%M-%S'.h264"])

def stoprecording()
    subprocess.call(["pkill", "raspivid"])

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 12:05 pm
by mariusmilatz
Just put the subprocess code into the script:

Code: Select all

import time
import datetime
import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BOARD)
GPIO.setup(29,GPIO.IN,pull_up_down=GPIO.PUD.UP)

def startrecording()
        subprocess.call(["raspivid", "-t", "600000", "-ex", "auto", "-b", "17000000", "-o", "/home/pi/t1_'date+%d%m%Y_%H%M-%S'.h264"])

def stoprecording()
        subprocess.call(["pkill", "raspivid"])

while True:
        GPIO.wait_for_edge(29,GPIO.FALLING)
                startrecording()
        GPIO.wait_for_edge(29,GPIO.RISING)
                stoprecording()
 
The error message is:

Code: Select all

pi@raspberrypi:~ $ sudo python /home/pi/super8.py
  File "/home/pi/super8.py", line 9
    def startrecording()
                       ^
SyntaxError: invalid syntax

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 12:31 pm
by elParaguayo
When you define functions, you need a colon at the end of the line:

Code: Select all

def startrecording():
Update your code throughout (and, yes, I missed it when I commented on using subprocess!)

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 12:44 pm
by mariusmilatz
The new code is

Code: Select all


import time
import datetime
import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BOARD)
GPIO.setup(29,GPIO.IN,pull_up_down=GPIO.PUD.UP)

def startrecording():
        subprocess.call(["raspivid", "-t", "600000", "-ex", "auto", "-b", "17000000", "-o", "/home/pi/t1_'date+%d%m%Y_%H%M-%S'.h264"])

def stoprecording():
        subprocess.call(["pkill", "raspivid"])

while True:
        GPIO.wait_for_edge(29,GPIO.FALLING)
                startrecording()
        GPIO.wait_for_edge(29,GPIO.RISING)
                stoprecording()

when i run the new code i get this:

Code: Select all

pi@raspberrypi:~ $ sudo python /home/pi/super8.py
  File "/home/pi/super8.py", line 10
SyntaxError: Non-ASCII character '\xe2' in file /home/pi/super8.py on line 10, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
I have check the http:// address but doesn't make much sense to me. do i have to put "#!/usr/bin/python" in the script?

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 12:49 pm
by elParaguayo
You can try putting this at the very top of your file

Code: Select all

# -*- coding: utf-8 -*-
However, it shouldn't be necessary here and I'm not sure why you're getting that message as there's no obvious stray character in your code.

EDIT: I wonder if it's to do with this: /home/pi/t1_'date+%d%m%Y_%H%M-%S'.h264

Can you just try with a simple filename to start with e.g. /home/pi/t1_test.h264

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 1:09 pm
by mariusmilatz
the error was happening due to the spaces after the ',' signs.

The error that i am now getting is:

Code: Select all

pi@raspberrypi:~ $ sudo python super8.py
Traceback (most recent call last):
  File "super8.py", line 8, in <module>
    GPIO.setup(29,GPIO.IN,pull_up_down=GPIO.PUD.UP)
AttributeError: 'module' object has no attribute 'PUD'
Do i even need the code 'pull_up_down=GPIO.PUD.UP' part?
The GPIO pin 29 is connected to a wire which is at 3v. when i press the trigger the wire voltage goes to 0v. when i relies the trigger the wire voltage goes back to 3v.
This is how i want to stop and start the RPi to stop and start recording.

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 1:14 pm
by DirkS
mariusmilatz wrote:
Fri Aug 17, 2018 1:09 pm
the error was happening due to the spaces after the ',' signs.

The error that i am now getting is:

Code: Select all

pi@raspberrypi:~ $ sudo python super8.py
Traceback (most recent call last):
  File "super8.py", line 8, in <module>
    GPIO.setup(29,GPIO.IN,pull_up_down=GPIO.PUD.UP)
AttributeError: 'module' object has no attribute 'PUD'
It's PUD_UP instead of PUD.UP

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 1:30 pm
by mariusmilatz
Thanks. Now there is no error message.

For some reason the camera doesn't start recording when i press the trigger.

Do i have to import picamera to script?

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 2:42 pm
by elParaguayo
You don't need to import picamera if you're calling a shell script (whether that is the best way to use the camera is beyond my expertise as I've never used the camera but I'd normally prefer to use python bindings rather than call external scripts).

As for your code not starting, you could put some print statements in the code to debug and see what's happening (just add the print statements to your current code, don't copy this):

Code: Select all

def startrecording():
        print("In startrecording")
        subprocess.call(["raspivid", "-t", "600000", "-ex", "auto", "-b", "17000000", "-o", "/home/pi/t1_'date+%d%m%Y_%H%M-%S'.h264"])
        print("Leaving startrecording")
        
def stoprecording():
        print("In stoprecording")
        subprocess.call(["pkill", "raspivid"])
        print("leaving stoprecording")

while True:
        print("Entering loop.")
        print("Waiting for falling edge...")
        GPIO.wait_for_edge(29,GPIO.FALLING)
                print("Falling edge. Calling startrecording")
                startrecording()
        print("Waiting for rising edge...") 
        GPIO.wait_for_edge(29,GPIO.RISING)
                print("Rising edge. Calling stoprecording")
                stoprecording()
        print("Restarting loop")
Another idea, you should probably change the raspivid command to subprocess.Popen rather than subprocess.call as call waits for the command to finish before continuing.

Re: Python Script 'Invalid Syntax'

Posted: Fri Aug 17, 2018 3:02 pm
by mariusmilatz
Yes, i will try that.

Thanks anyway for helping me.

Big help.

Re: Python Script 'Invalid Syntax'

Posted: Sun Aug 19, 2018 4:02 pm
by mariusmilatz
Do you know by any chance know how to create a hold button function in python.

at the moment the trigger signal is slightly occulting and coursing the edge detection to go off unintentionally.

the script that i am using:

Code: Select all

import datetime
import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BOARD)
GPIO.setup(37,GPIO.IN,pull_up_down=GPIO.PUD_UP)

def startrecording():
    print("In startrecording")
    subprocess.call(["raspivid", "-t", "600000", "-ex", "auto", "-b", "17000000", "-o", "/home/pi/t1_'date+%d%m%Y_%H%M-%S'.h264"])
    print("leaving startrecording")

def stoprecording():
    print("In stoprecording")
    subprocess.call(["pkill", "raspivid"])
    print("leaving stoprecording")

while True:
    print("Entering loop")
    print("Waiting for falling edge...")
    GPIO.wait_for_edge(37,GPIO.FALLING)
    print("Faslling edge. Calling startrecording")
    startrecording()
    print("Waiting for rising edge...")
    GPIO.wait_for_edge(37,GPIO.RISING)
    print("Rising edge. Calling stoprecording")
    stoprecording()
    print("Restarting loop")


so the RISING edge is the problem. if i could get something like, if edge changes longer then 1 second execute 'stoprecording()'

Re: Python Script 'Invalid Syntax'

Posted: Sun Aug 19, 2018 4:04 pm
by mariusmilatz
oscillating*