Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3843

Python • Failure of successive file.writes in callback routine

$
0
0

Code:

def lightcallback(channel): # night time function    global flagnight # do this to use variable within function    time.sleep(0.1) # DEBOUNCE delay to remove false positives    if GPIO.input(18)==0: # DEBOUNCE recheck thatlight is low        print("debounce check complete - light trigger valid")        now = datetime.now()        formatted_time = now.strftime('%Y/%m/%d %H:%M:%S')        linetoinsert = formatted_time + "   LIGHT ON" + "\n"        file1 = open(r'logfile_cam.txt', 'a', newline='') #open file for LIGHT ON time        file1.write (linetoinsert)        file1.close()        print (linetoinsert)        if (flagnight == False): # prior status = daytime            picam2.set_controls({"ExposureTime":10000, "AnalogueGain": 16.0})            picam2.set_controls({"FrameDurationLimits": (100000,100000)}) # this overrides the configuration setting but is not shown when config setting is printed            print ("Change to Night exposure")            flagnight = 1        else: # do nothing already set to night exposure            print ("Continue Night exposure")        recordandsave () # call the function to record and save file        while GPIO.input(18) == 0: # do more recordings until the light goes off            print("light still on - next successive recording")            now = datetime.now()            formatted_time = now.strftime('%Y/%m/%d %H:%M:%S')            linetoinsert = formatted_time + "   LIGHT ON SUCCESSIVE" + "\n"            file1 = open(r'logfile_cam.txt', 'a', newline='') #open file for SUCCESSIVE LIGHT ON time            file1.write (linetoinsert) # ****** THIS LINE DOES NOT WORK********            file1.close()            print (linetoinsert)            recordandsave () # call the function to record and save file        print ("waiting for trigger")
Hi,
I have a python program to make 12 second recordings from Pi Camera 3 which are triggered by a security light going on and pulling a GPIO pin 18 low. I log the start of each recording in a file logfile_cam.txt. Multiple recordings are made if the light stays on longer than 12 seconds. The program works as expected from a recording point of view. In my code below the first file write correctly inserts a line into the logfile. The successive recordings are made and the terminal output is as expected. However the successive lines are never entered into the logfile. So I believe the code is going past the second file write which is in the "while GPIO.input(18)==0" loop but I cannot figure why the logfile_cam is not reopened and written to again by the second write.file statement commented *THIS LINE DOES NOT WORK*. I can load the complete code if that would be helpful.
Thanks,
Jonathan.

Statistics: Posted by Jonathan122 — Fri May 03, 2024 11:18 am



Viewing all articles
Browse latest Browse all 3843

Trending Articles