Last active
April 22, 2020 11:19
-
-
Save normva/818f27409bd07df04de2b7a5c232d70c to your computer and use it in GitHub Desktop.
Python Code for PiCamera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from datetime import datetime | |
from time import sleep | |
from picamera import PiCamera | |
camera = PiCamera() | |
camera.resolution = (1024, 768) | |
camera.rotation = (180) | |
camera.start_preview() | |
myTime = datetime.now() | |
month = myTime.month | |
day = myTime.day | |
hour = myTime.hour | |
minutes = myTime.minute | |
def takePictures(numberOfPictures, pictureInterval): | |
currentTime = datetime.now() | |
myTimeMonth = datetime.strftime(currentTime, "%m") | |
myTimeDay = datetime.strftime(currentTime, "%d") | |
myTimeMin = datetime.strftime(currentTime, "%M") | |
timeStamp = myTimeMonth + myTimeDay + myTimeMin | |
print('Taking pictures') | |
print('Number of pictures: ', numberOfPictures) | |
print('Picture Interval: ', pictureInterval) | |
x = 0 | |
for i in range(numberOfPictures): | |
# camera.capture('image' + timeStamp + str(x) + '{0:04d}.jpg'.format(i)) | |
camera.capture('image' + timeStamp + '{0:03d}.jpg'.format(i)) | |
print('picture: ', i) | |
camera.stop_preview | |
sleep(pictureInterval) | |
x = x + 1 | |
takePictures(3, 60) | |
camera.stop_preview() | |
camera.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
from picamera import PiCamera | |
from os import system | |
from time import sleep | |
import requests, bs4 | |
import time; # This is required to include time module. | |
import urllib.request | |
import json | |
from datetime import datetime, timezone | |
camera = PiCamera() | |
camera.rotation = 180 | |
camera.resolution = (1024, 768) | |
x = 0 | |
# **************************Convert time *************** | |
def convertTicks(ticks): | |
convertedTime = datetime.fromtimestamp(ticks) | |
return convertedTime | |
# ******************************* Get Sunrise time ************* | |
def getSunrise(): | |
# Get Sunrise | |
api_address = 'http://api.openweathermap.org/data/2.5/weather?zip=' | |
key = '&units=imperial&APPID=2f76aaa4e5fc0a67128993431c6423a8' | |
zip = '20191' # Reston | |
url = api_address + zip + key | |
# Get weather data | |
json_data = requests.get(url).json() | |
#print(json_data) | |
sunrise = json_data['sys']['sunrise'] | |
sunset = json_data['sys']['sunset'] | |
return(sunrise) | |
# ******************************* Get Sunset time ************* | |
def getSunset(): | |
# Get Sunset | |
api_address = 'http://api.openweathermap.org/data/2.5/weather?zip=' | |
key = '&units=imperial&APPID=2f76aaa4e5fc0a67128993431c6423a8' | |
zip = '20191' # Reston | |
url = api_address + zip + key | |
# Get weather data | |
json_data = requests.get(url).json() | |
#print(json_data) | |
sunrise = json_data['sys']['sunrise'] | |
sunset = json_data['sys']['sunset'] | |
return(sunset) | |
# *********** Check if current time is past picture start time **************** | |
# ********** Run on Day one only ******************************* | |
def checkForPastSunrise(adjustedSunrise): | |
# Get current time and compare to sunrise and sunset | |
print('**************************************') | |
currentTime = time.time() | |
currentTimeTime = convertTicks(currentTime) | |
print('Current time: ', currentTimeTime) | |
print('Current time: ', currentTime) | |
startPictureTime = convertTicks(adjustedSunrise) | |
print('Current time: ', currentTimeTime) | |
print('Start picture time: ', startPictureTime) | |
pastSunrise = currentTime - adjustedSunrise | |
return pastSunrise # positive value if past sunrise | |
# **********************Test for Sunrise ******************************* | |
def testForSunrise(startTime, sunset): | |
sunrise = getSunrise() # Check for sunrise on startup | |
sunset = getSunset() # Check for sunset on startup | |
currentTime = time.time() | |
currentTimeTime = convertTicks(currentTime) | |
print('Current time: ', currentTimeTime) | |
sunriseTime = convertTicks(sunrise) | |
print('Sunrise time: ', sunriseTime) | |
sunsetTime = convertTicks(sunset) | |
print('Sunset time: ', sunsetTime) | |
adjustedSunrise = sunrise - startTime # start minutes before sunrise (ticks) | |
adjustedSunriseTime = convertTicks(adjustedSunrise) | |
print('Start pictures time: ', adjustedSunriseTime) | |
# test for darkness | |
while currentTime < adjustedSunrise or currentTime > sunset: | |
currentTime = time.time() | |
# calc time before starting to take pictures | |
deltaHours = (adjustedSunrise - currentTime)/3600 | |
deltaMinutes = (adjustedSunrise - currentTime)/60 | |
currentTimeTime = convertTicks(currentTime) | |
print('Current time: ', currentTimeTime) | |
pictureStartTime = convertTicks(adjustedSunrise) | |
print('Picture start time: ', pictureStartTime) | |
print('Hours to start pictures = ', deltaHours) | |
print('Minutes to start pictures = ', deltaMinutes) | |
print(' ') | |
print('***************** ') | |
time.sleep(10) | |
# ******************* Take Pictures ************************** | |
def takePictures(numberOfPictures, pictureInterval): | |
currentTime = datetime.now() | |
myTimeMonth = datetime.strftime(currentTime, "%m") | |
myTimeDay = datetime.strftime(currentTime, "%d") | |
timeStamp = myTimeMonth + myTimeDay | |
print('Taking pictures') | |
print('Number of pictures: ', numberOfPictures) | |
print('Picture Interval: ', pictureInterval) | |
x = 0 | |
for i in range(numberOfPictures): | |
# camera.capture('image' + timeStamp + str(x) + '{0:04d}.jpg'.format(i)) | |
camera.capture('image' + timeStamp + '{0:03d}.jpg'.format(i)) | |
print('picture: ', i) | |
camera.stop_preview() | |
sleep(pictureInterval) | |
x = x + 1 | |
# *********************** Delay to Next Day ******************* | |
def delayToNextDay(hoursToSleep): | |
print('Hours to sleep = ', hoursToSleep) | |
y = 0 | |
for y in range(int(hoursToSleep)): | |
currentTime = time.time() | |
currentTimeTime = convertTicks(currentTime) | |
print('Days left to run: ', daysToRun, ' of ', daysToRunStart, ' days to run.') | |
print('Current time: ', currentTimeTime) | |
print('Sleep hour #: ', y) | |
time.sleep(3600) # Hours to sleep = 3600 * hoursToSleep | |
y = y + 1 | |
# ***********************End of Functions ************************ | |
startTime = '25' | |
startTime = int(startTime) * 60 # convert minutes to ticks | |
numberOfPictures = '45' | |
numberOfPictures = int(numberOfPictures) | |
pictureInterval = '1' | |
pictureInterval = int(pictureInterval) * 60 | |
daysToRun = 1 | |
# daysToRun = int(daysToRun) | |
daysToRunStart = daysToRun | |
sunrise = getSunrise() # Check for sunrise on startup | |
sunset = getSunset() # Check for sunset on startup | |
adjustedSunrise = sunrise - startTime # start minutes before sunrise | |
sunriseTime = convertTicks(sunrise) | |
adjustedSunriseTime = convertTicks(adjustedSunrise) | |
print('Sunrise = ', sunrise) | |
print('Sunrise time = ', sunriseTime) | |
print('Start time = ', startTime) | |
print('Start to take pictures = ', adjustedSunriseTime) | |
print('Adjusted Sunrise = ', adjustedSunrise) | |
print(' ') | |
pastSunrise = checkForPastSunrise(adjustedSunrise) | |
print('pastSunrise = ', pastSunrise) | |
if pastSunrise > 0: | |
print('Executing pastSunrise') | |
hoursToSleep = (20 * 3600) - pastSunrise # ticks | |
hoursToSleep = hoursToSleep / 3600 | |
hoursToSleep = int(hoursToSleep) | |
print('hours to sleep = ', hoursToSleep) | |
delayToNextDay(hoursToSleep) | |
# daysToRun = daysToRun - 1 | |
testForSunrise(startTime, sunset) | |
else: | |
print('Executing pastSunrise - else') | |
# daysToRun = daysToRun - 1 | |
testForSunrise(startTime, sunset) | |
print('Executing while loop') | |
print('daysToRun = ', daysToRun) | |
while daysToRun > 0: | |
takePictures(numberOfPictures, pictureInterval) | |
daysToRun = daysToRun - 1 | |
print('daysToRun (after decrement) = ', daysToRun) | |
if daysToRun > 0: | |
delayToNextDay(20) | |
testForSunrise(startTime, sunset) | |
else: | |
print('Time to end program') | |
print('End of Program') | |
print('daysToRun = ', daysToRun) | |
print('Camera stop_preview()') | |
camera.stop_preview() | |
print('About to close camera') | |
camera.close() | |
print('Camera closed') | |
print('End of Program') | |
#system('convert -delay 20 -loop 0 image*.jpg animation.gif') | |
print('Good bye') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment