Use variable from While True Loop in another file

Viewed 416

I have a main file fileA.py that controls the lighting in my radio studio based upon GPIO triggers. The part I'm concerned about is in a While True loop as below:

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"

I then have a second python file fileB.py running at the same time on the same Raspberry Pi

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

However I would like this to continually update with the studiostatus varible from fileA.py so that the line in fileB.py becomes

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

Any help much appreciated as currently I can't see the wood for the trees in how best to tackle this

1 Answers

If I understand correctly, you want some lights to flash when someone is in the building. Notice how the previous sentence does not mention HTTP, files, variables, GPIO, or any technical details of your code. This is always where you should start when describing what you want to do.

Now to talk about the "how"...at a high level, you have a doorbell that can send a command to the RPi as an HTTP request. And you need to send a GPIO signal to the controls for the light.

It looks like you have this working in two separate files that you run as separate scripts. One file that controls the lights via GPIO pins and another that receives HTTP requests from the Ring doorbell.

One way to solve this problem is to just start the script that flashes the lights when the Ring detects someone entered the house and kill the script when it detects that everyone left.

Another solution is to use a status value like you are, but the HTTP server writes the status to a file and the GPI script reads it from the file.

Related