2

我有一个fileA.py基于 GPIO 触发器控制无线电演播室照明的主文件。我关心的部分是在一个 While True 循环中,如下所示:

While True:
    #other triggers for other things unrelated

    if GPIO.input(25) == GPIO.LOW and (state) = 'off':
        blue()
        studiostatus = "online"

    #other triggers for other things unrelated
    
    elif count > 7200:
        dbo()
        clockoff()
        studiostatus = "offline"

然后我fileB.py在同一个 Raspberry Pi 上同时运行第二个 python 文件

import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler

def some_function():
    print "some_function got called"

class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/doorbell':
            some_function()

        self.send_response(200)
        

httpd = SocketServer.TCPServer(("", 8080), MyHandler)
httpd.serve_forever()

但是我希望它能够不断更新studiostatus变量 fromfileA.py以便行fileB.py变为

if self.path == '/doorbell' and studiostatus = "online":
    action1()
elif self.path == '/doorbell' and studiostatus = "offline":
    action2()

任何帮助都非常感谢,因为目前我看不到树木的木材如何最好地解决这个问题

4

1 回答 1

0

如果我理解正确,当有人在建筑物内时,您希望一些灯闪烁。请注意,前一句没有提及 HTTP、文件、变量、GPIO 或代码的任何技术细节。在描述您想要做什么时,这始终是您应该开始的地方。

现在来谈谈“如何”……在高层次上,您有一个门铃,可以将命令作为 HTTP 请求发送到 RPi。您需要向灯光控件发送一个 GPIO 信号。

看起来你在两个单独的文件中工作,你作为单独的脚本运行。一个文件通过 GPIO 引脚控制灯,另一个文件从 Ring 门铃接收 HTTP 请求。

解决此问题的一种方法是启动脚本,当 Ring 检测到有人进入房子时闪烁灯,并在检测到每个人都离开时终止脚本。

另一种解决方案是像您一样使用状态值,但是 HTTP 服务器将状态写入文件,而 GPI 脚本从文件中读取它。

于 2021-12-02T17:44:02.877 回答