Camera not recording consistently to SD cards

Hello,

I am creating an OpenMV code that records for 15 minutes every 4 hours. The majority of the time, this works well, but for some of my cameras (not all), there is only one stream on the SD card (ProGrade V60 64GB SD cards) and the file is very small- too small to have any video stream on it. I’ve tried removing the main.py file and re-adding it. I’ve also tried changing the batteries more frequently, but neither of these changes have completely resolved the issue. Does anyone have any idea what could be causing this intermittent problem? Here’s the code below for reference:

import pyb, sensor, os, utime, image

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.VGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.

IR_led = pyb.Pin("P7", pyb.Pin.OUT_PP)
status_led = pyb.Pin("P9", pyb.Pin.OUT_PP)
power_led = pyb.Pin("P8", pyb.Pin.OUT_PP)
power_led.high()
count=0
started=False
while(True):
    #record for 15 min and then stop for 4 hours
    if started==False:
        IR_led.high()
        status_led.high()
        count+=1
        stream_filename=("/stream%d.bin"%count) #set the filename
        print(stream_filename)
        started=True
        stream = image.ImageIO(stream_filename, "w")
    else:
        current_millis=pyb.millis()
        IR_led.high()
        status_led.high()
        while pyb.elapsed_millis(current_millis)<900000:
            img = sensor.snapshot()
            stream.write(img)

        #wait for 4 hours and set started to false
        IR_led.low()
        status_led.low()
        stream.close()
        started=False
        print("g0ing to sleep.....zzzz....")
        #utime.sleep(20)
        utime.sleep(14400)  # sleep for 4 hours
        print("out of sleep!")

Thanks!

This is very hard to debug, unless you can provide something concrete to start with, I can’t fix it.

Which cameras ? It sounds like it might be throwing an exception early and exiting the while loop. I’d use shorter delays (like record 100ms, sleep for 1000ms), add a try/except around the code and write out the exception message to a file. For example:


try:
    # some code that may be failing
except Exception as e:
    with open('error.txt', 'w') as f:
        f.write(str(e))
    raise(e)