pythonraspberry-piraspbianmagic-mirror

Raspi 3 PIR sensor - Python script - invalid syntax


actually I´m working at a "Magic Mirror" and now I got a problem with the python script wich should turning my monitor on/off.

I copied the python script from this site

#!/usr/bin/env python

import sys
import time
import RPi.GPIO as io
import subprocess

io.setmode(io.BCM)
SHUTOFF_DELAY = 60  # seconds
PIR_PIN = 7         # Pin 26 on the board

def main():
io.setup(PIR_PIN, io.IN)
turned_off = False
last_motion_time = time.time()

while True:
    if io.input(PIR_PIN):
        last_motion_time = time.time()
        sys.stdout.flush()
        if turned_off:
            turned_off = False
            turn_on()
    else:
        if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY):
            turned_off = True
            turn_off()
    time.sleep(.1)

def turn_on():
subprocess.call("sh /home/pi/Documents/PIR/monitor_on.sh", shell=True)

def turn_off():
subprocess.call("sh /home/pi/Documents/PIR/monitor_off.sh", shell=True)

if __name__ == '__main__':
try:
    main()
except KeyboardInterrupt:
    io.cleanup()

I tried to run the script, but python tell me there is a syntax error at line 25, it points exactly at the semicolon after &amp and before gt

I didn't worked with python until now, therefore I don't know anything about the syntax of python.

I would appreciate it very much if you guys will take a minute to help me solving my problem.

I got the python version 2.7.9


Solution

  • This is not the exact copy of the original Python file. You copied some HTML markup while copying the file.

    Replace > with >.

        if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY):
            turned_off = True
            turn_off()
    

    You also have indentation issues and other HTML stuff you should get rid of:

    def main():
        io.setup(PIR_PIN, io.IN)
        turned_off = False
        last_motion_time = time.time()
    

    and

    def turn_on():
        subprocess.call("sh /home/pi/Documents/PIR/monitor_on.sh", shell=True)
    
    def turn_off():
        subprocess.call("sh /home/pi/Documents/PIR/monitor_off.sh", shell=True)