【问题标题】:Python script doesn't start on bootPython 脚本不会在启动时启动
【发布时间】:2020-08-24 19:33:31
【问题描述】:

我的 Raspberry Pi 中有一个连接到雨量计的 python 脚本。当雨量计检测到下雨时,脚本显示 0.2 并将其写入文件。这是代码:

#!/usr/bin/env python3
import time
import RPi.GPIO as GPIO
BUTTON_GPIO = 16

if __name__ == '__main__':
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    pressed = False
    while True:
        # button is pressed when pin is LOW
        if not GPIO.input(BUTTON_GPIO):
            if not pressed:
                print("0.2")
                pressed = True
        # button not pressed (or released)
        else:
            pressed = False
        time.sleep(0.1)

我的想法是使用这样的代码来节省雨水总量。当 python 脚本显示 0.2 > 将其写入文件。

python3 rain.py >> rain.txt

代码创建了一个文件,但在按 Ctrl + C 完成执行之前不会写入任何内容。

我需要在启动时执行它。我尝试将其添加到 crontab 和 rc.local 但不起作用。

我尝试使用 sudo 和 pi 来执行它。权限为 755。

谢谢!

【问题讨论】:

  • 您确定您已正确按照说明进行操作吗?您是否尝试过 @reboot python /home/pi/myscript.py & 文档中指定的 raspberrypi.org/documentation/linux/usage/cron.md
  • 另外,您的代码有无限循环,但它仍应在执行完成之前写入文件。你确定需要Ctrl + c 来写入文件吗?

标签: python linux raspberry-pi raspbian startup


【解决方案1】:

试试这个

import time
import RPi.GPIO as GPIO
BUTTON_GPIO = 16

if __name__ == '__main__':
    outputfile=open("/var/log/rain.txt","a",0)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    pressed = False
    while True:
        # button is pressed when pin is LOW
        if not GPIO.input(BUTTON_GPIO):
            if not pressed:
                openputfile.write("0.2\n")
                pressed = True
        # button not pressed (or released)
        else:
            pressed = False
        time.sleep(0.1)

以附加模式打开一个文件,使用非缓冲写入。 然后当事件发生时,写入该文件。

不要使用 shell 重定向,因为它会(在这种情况下)缓冲所有程序输出,直到退出,然后写入文件。当然,退出永远不会发生,因为您有一个没有中断的“while True”

【讨论】:

  • 成功了。我使用“/home/pi/rain/rain.txt”而不是“/var/log/rain.txt”。现在该文件在脚本运行并在启动时启动时显示 0.2。非常感谢!
【解决方案2】:

确实,这个构造 command >> file 获取整个 stdout 并刷新到文件中。仅当command 执行结束时才完成。中间结果准备好后,您必须立即写入文件:

#!/usr/bin/env python3
import sys
import time
import RPi.GPIO as GPIO
BUTTON_GPIO = 16

if __name__ == '__main__':
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    pressed = False

    # command line arguments
    if len(sys.argv) > 1: ## file name was passed
        fname = sys.argv[1]
    else: ## standard output
        fname = None

    ## this will clear the file with name `fname`
    ## exchange 'w' for 'a' to keep older data into it
    outfile = open(fname, 'w')
    outfile.close()

    try:
        while True:
            # button is pressed when pin is LOW
            if not GPIO.input(BUTTON_GPIO):
                if not pressed:
                    if fname is None: ## default print
                        print("0.2")
                    else:
                        outfile = open(fname, 'a')
                        print("0.2", file=outfile)
                        outfile.close()
                    pressed = True
            # button not pressed (or released)
            else:
                pressed = False
            time.sleep(0.1)
    except (Exception, KeyboardInterrupt):
        outfile.close()

在这种方法中,您应该运行python3 rain.py rain.txt,一切都会好起来的。 try except 模式可确保在执行因错误或键盘事件而中断时正确关闭文件。

注意print 调用中的file 关键字参数。它选择一个打开的文件对象来写打印的东西。默认为sys.stdout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2015-10-25
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2013-05-07
    相关资源
    最近更新 更多