openmv/scripts/examples/01-Camera/00-Snapshot/time_lapse_photos.py

84 lines
2.7 KiB
Python

# This work is licensed under the MIT license.
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Time Lapse Photos (Credit nedhorning)
#
# This example shows off how to take time lapse photos using your OpenMV
# Cam and using the RTC module along with a timer interrupt to achieve
# very low power operation.
#
# Note that if the USB is still plugged in when the camera is taking
# pictures it will run the bootloader each time. Please power the camera
# from something other than USB to not have the bootloader run.
import machine
import sensor
import os
# Create and init RTC object. This will allow us to set the current time for
# the RTC and let us set an interrupt to wake up later on.
rtc = machine.RTC()
newFile = False
try:
os.stat("time.txt")
except OSError:
# If the log file doesn't exist then set the RTC and set newFile to True
# datetime format: year, month, day, weekday (Monday=1, Sunday=7),
# hours (24 hour clock), minutes, seconds, subseconds (counts down from 255 to 0)
rtc.datetime((2018, 3, 9, 5, 13, 0, 0, 0))
newFile = True
# Extract the date and time from the RTC object.
dateTime = rtc.datetime()
year = str(dateTime[0])
month = "%02d" % dateTime[1]
day = "%02d" % dateTime[2]
hour = "%02d" % dateTime[4]
minute = "%02d" % dateTime[5]
second = "%02d" % dateTime[6]
subSecond = str(dateTime[7])
# Image file name based on RTC
newName = "I" + year + month + day + hour + minute + second
# Enable RTC interrupts every 10 seconds, camera will RESET after wakeup from deepsleep Mode.
rtc.wakeup(10000)
BLUE_LED_PIN = 3
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.skip_frames(time=1000) # Let new settings take affect.
led = machine.LED("LED_BLUE")
if newFile:
# If log file does not exist then create it.
with open("time.txt", "a") as timeFile:
# Write text file to keep track of date, time and image number.
timeFile.write("name, year, month, day, hours, minutes, seconds, subseconds\n")
timeFile.write(
f"{newName},{year},{month},{day},{hour},{minute},{second},{subSecond}\n"
)
else:
with open("time.txt", "a") as timeFile:
# Append to date, time and image number to text file.
timeFile.write(
f"{newName},{year},{month},{day},{hour},{minute},{second},{subSecond}\n"
)
if not "images" in os.listdir():
os.mkdir("images") # Make a temp directory
# Take photo and save to SD card
led.on()
img = sensor.snapshot()
img.save("images/" + newName, quality=90)
led.off()
# Enter Deepsleep Mode (i.e. the OpenMV Cam effectively turns itself off except for the RTC).
machine.deepsleep()