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

Graphics, sound and multimedia • program not taken camera pics during segment of time

$
0
0
Hi all

I have written a program/project (partly with ChatGPT) to take each 3 minutes from dusk till dawn with a Raspberry Pi HQ camera.
The period from dusk till dawn is divided in some segments, and in each segment the camera should use a specific shutter time.
However, in the largest segment (middle part - from astro-dusk till nautical dawn), where the camera should have a shutter time of 20", the camera is not taking pictures.
So it seems that my rpi is 'skipping' the middle segment.
I can't see where it goes wrong in my program ... or what I can do find out what goes wrong?

Does anyone can give some advise?
The code of my project is below.
Thanks for any help.

Code:

import timeimport osimport csvimport subprocessfrom datetime import datetime, datefrom astral import LocationInfofrom astral.sun import sun, dawn, duskimport pytzimport boardimport adafruit_dht# Locatiegegevens voor Brusselbrussel = LocationInfo("Brussel", "Belgie", "Europe/Brussels", 50.8503, 4.3517)tz = pytz.timezone(brussel.timezone)# Sluitertijden in microsecondenSLUITERTIJD_MICROS = {    "1/1000": "1000",    "1/2": "500000",    "10": "10000000",    "20": "20000000"}# DHT22 sensor op GPIO4 (BCM)dht_device = adafruit_dht.DHT22(board.D4)def neem_foto(sluitertijd):    now = datetime.now(tz)    timestamp = now.strftime("%Y%m%d_%H%M%S")    datum_pad = now.strftime("%Y/%m/%d")    usb_pad = "/media/pi/7091-0035"    fallback_pad = "/home/pi/fotos"    if os.path.ismount(usb_pad):        opslag_basis = usb_pad        opslag_type = "USB-stick"    else:        opslag_basis = fallback_pad        opslag_type = "SD-kaart (fallback)"        print(f"USB niet beschikbaar - gebruik {opslag_type}")    bestandsmap = os.path.join(os.path.expanduser(opslag_basis), datum_pad)    os.makedirs(bestandsmap, exist_ok=True)    bestandspad = f"{bestandsmap}/foto_{timestamp}.jpg"    microseconden = SLUITERTIJD_MICROS.get(sluitertijd, "1000")    print(f"[{timestamp}] Foto ({opslag_type}) - sluitertijd {sluitertijd} ({microseconden} microseconden)")    subprocess.run([        "libcamera-still",        "-o", bestandspad,        "--shutter", microseconden,        "--gain", "1",           # Vast ISO        "--exposure", "normal",  # Geen auto exposure        "--metering", "centre",  # Lichtmeting in het midden        "--awbgains", "1,1",     # Vaste witbalans        "-n"    ])     # Sensorwaarden loggen    try:        temperatuur = dht_device.temperature        vochtigheid = dht_device.humidity        log_tijd = now.strftime("%Y-%m-%d %H:%M:%S")        log_bestand = f"{opslag_basis}/sensor_log.csv"        schrijf_kop = not os.path.exists(log_bestand)        with open(log_bestand, mode="a", newline='') as f:            writer = csv.writer(f)            if schrijf_kop:                writer.writerow(["Tijdstip", "Temperatuur (C)", "Vochtigheid (%)"])            writer.writerow([log_tijd, f"{temperatuur:.1f}", f"{vochtigheid:.1f}"])        print(f"Temperatuur: {temperatuur:.1f} C | Vochtigheid: {vochtigheid:.1f} %")    except Exception as e:        print(f"Sensorfout: {e}")# Twilight-tijden ophalendef haal_tijden_op():    vandaag = date.today()    zonsdata = sun(brussel.observer, date=vandaag, tzinfo=tz)    return {        "sunrise": zonsdata["sunrise"],        "sunset": zonsdata["sunset"],        "civil_dawn": zonsdata["dawn"],        "civil_dusk": zonsdata["dusk"],        "nautical_dawn": dawn(brussel.observer, date=vandaag, tzinfo=tz, depression=12),        "nautical_dusk": dusk(brussel.observer, date=vandaag, tzinfo=tz, depression=12),        "astro_dawn": dawn(brussel.observer, date=vandaag, tzinfo=tz, depression=18),        "astro_dusk": dusk(brussel.observer, date=vandaag, tzinfo=tz, depression=18),    }# Segmentbepalingdef bepaal_segment(nu, tijden):    midden_nautical_astro_pm = tijden['nautical_dusk'] + (tijden['astro_dusk'] - tijden['nautical_dusk']) / 2    midden_nautical_astro_am = tijden['nautical_dawn'] + (tijden['civil_dawn'] - tijden['nautical_dawn']) / 2    if tijden['civil_dusk'] <= nu < tijden['nautical_dusk']:        return ("1/1000", "Civil tot Nautical twilight (avond)")    elif tijden['nautical_dusk'] <= nu < midden_nautical_astro_pm:        return ("1/2", "Nautical tot midden Astronomical twilight (avond)")    elif midden_nautical_astro_pm <= nu < tijden['astro_dusk']:        return ("10", "Midden Nautical tot Astronomical twilight (avond)")    elif tijden['astro_dusk'] <= nu < tijden['nautical_dawn']:        return ("20", "Astronomische nacht")    elif tijden['nautical_dawn'] <= nu < midden_nautical_astro_am:        return ("10", "Astronomisch tot midden Nautisch twilight (ochtend)")    elif midden_nautical_astro_am <= nu < tijden['civil_dawn']:        return ("1/2", "Midden Nautisch tot Civil twilight (ochtend)")    elif tijden['civil_dawn'] <= nu < tijden['sunrise']:        return ("1/1000", "Civil twilight tot Zonsopgang (ochtend)")    else:        return (None, "Overdag")# Hoofdlusdef main_loop():    laatst_segment = None    while True:        nu = datetime.now(tz)        tijden = haal_tijden_op()        sluitertijd, segment = bepaal_segment(nu, tijden)        if sluitertijd:            print(f"[{nu.strftime('%H:%M:%S')}] Segment: {segment}")            neem_foto(sluitertijd)        else:            if laatst_segment != segment:                print(f"[{nu.strftime('%H:%M:%S')}] {segment} - geen foto's.")                laatst_segment = segment        time.sleep(3 * 60)if __name__ == "__main__":    main_loop()

Statistics: Posted by diedhert — Wed Apr 30, 2025 6:14 am



Viewing all articles
Browse latest Browse all 7520

Trending Articles