【问题标题】:Raspi 3 PIR sensor - Python script - invalid syntaxRaspi 3 PIR 传感器 - Python 脚本 - 语法无效
【发布时间】:2023-04-02 23:15:01
【问题描述】:

实际上我在“魔镜”工作,现在我遇到了一个问题,我的 python 脚本应该打开/关闭我的显示器。

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

我试图运行脚本,但 python 告诉我在第 25 行有一个语法错误,它正好指向 &amp 之后和 gt 之前的分号

直到现在我都没有使用过python,因此我对python的语法一无所知。

如果你们能花一点时间帮助我解决我的问题,我将不胜感激。

我得到了python 2.7.9版

【问题讨论】:

    标签: python raspberry-pi raspbian magic-mirror


    【解决方案1】:

    这不是原始 Python 文件的精确副本。您在复制文件时复制了一些 HTML 标记。

    > 替换为>

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

    您还有缩进问题和其他应该摆脱的 HTML 内容:

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

    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)
    

    【讨论】:

    • 嘿,非常感谢!!这就像一个魅力 xD ...我以前遇到过这个问题,但我不记得了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    相关资源
    最近更新 更多