3

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

Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • An inelegant solution is to exchange data through a file. Something like write the variable data to a `.txt` file and then read it from the other script. An even more inelegant solution would be to put the loop in a function, import `fileA` into `fileB` and run the function while declaring the variables you want as global. – Abdur Rakib Dec 02 '21 at 15:33
  • I've considered exchanging via a text file, my only concern is that ```fileB``` would have to open and read this every 0.2seconds or so which is definitely not good for the SD card in the Pi as it runs 24/7! – Adam Davies Dec 02 '21 at 15:35
  • You can keep the file open and just read it when you need. No need to close it – Alexandru DuDu Dec 02 '21 at 15:36
  • Then the second option is my best effort. global variable, as far as I know, most coders hate that. But I find it very useful. – Abdur Rakib Dec 02 '21 at 15:37
  • "However I would like this to continually update with the studiostatus varible from fileA.py so that the line in fileB.py becomes" This description is too much about *how* you want to accomplish something. Instead, try to describe *what* you want the raspberry pi to do. For example, "when I press a button, the lights turn on" or something along those lines. – Code-Apprentice Dec 02 '21 at 15:43
  • @AdamDavies Also, reading from an SD card frequently shouldn't be a problem. Older SD cards can have problems when you write to them often, but my understanding is that these problems have been mostly solved in newer cards. – Code-Apprentice Dec 02 '21 at 15:45
  • do you have any problem with the idea of running program A from program b? if not, you could use threading. (this will limit to read variable from only one more "program" ) – Ulises Bussi Dec 02 '21 at 15:46
  • @Code-Apprentice Apologies. The main ```fileA``` takes physical GPIO triggers and sends http commands to a philips hue Bridge to control the lighting (mic live, silence detected etc etc). I am now trying to integrate a Ring doorbell which can only communicate with the Pi via an http command (using IFTT). However I only want the lights to flash if there is someone physically in the building, which is the studiostatus defined in that file. If the doorbell is pressed but the building is empty, I want there to be no command sent to the hue – Adam Davies Dec 02 '21 at 15:48

1 Answers1

1

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268