mariusmilatz
Posts: 17
Joined: Mon Aug 06, 2018 11:09 am

Python Script 'Invalid Syntax'

Fri Aug 17, 2018 9:55 am

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?

User avatar
PeterO
Posts: 5066
Joined: Sun Jul 22, 2012 4:14 pm

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 10:04 am

Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12322
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 10:37 am

added code tags, and moved to python section

mariusmilatz
Posts: 17
Joined: Mon Aug 06, 2018 11:09 am

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 10:43 am

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()

Heater
Posts: 13592
Joined: Tue Jul 17, 2012 3:02 pm

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 11:18 am

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.
Memory in C++ is a leaky abstraction .

DirkS
Posts: 9956
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 11:21 am

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

mariusmilatz
Posts: 17
Joined: Mon Aug 06, 2018 11:09 am

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 11:38 am

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]

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 11:49 am

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"])
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

mariusmilatz
Posts: 17
Joined: Mon Aug 06, 2018 11:09 am

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 12:05 pm

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

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

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 12:31 pm

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!)
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

mariusmilatz
Posts: 17
Joined: Mon Aug 06, 2018 11:09 am

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 12:44 pm

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

[email protected]:~ $ 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?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 12:49 pm

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
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

mariusmilatz
Posts: 17
Joined: Mon Aug 06, 2018 11:09 am

Re: Python Script 'Invalid Syntax'

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

[email protected]:~ $ 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.

DirkS
Posts: 9956
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 1:14 pm

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

[email protected]:~ $ 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

mariusmilatz
Posts: 17
Joined: Mon Aug 06, 2018 11:09 am

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 1:30 pm

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?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 2:42 pm

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

mariusmilatz
Posts: 17
Joined: Mon Aug 06, 2018 11:09 am

Re: Python Script 'Invalid Syntax'

Fri Aug 17, 2018 3:02 pm

Yes, i will try that.

Thanks anyway for helping me.

Big help.

mariusmilatz
Posts: 17
Joined: Mon Aug 06, 2018 11:09 am

Re: Python Script 'Invalid Syntax'

Sun Aug 19, 2018 4:02 pm

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()'

mariusmilatz
Posts: 17
Joined: Mon Aug 06, 2018 11:09 am

Re: Python Script 'Invalid Syntax'

Sun Aug 19, 2018 4:04 pm

oscillating*

Return to “Python”