inital dump
This commit is contained in:
40
Software/ZK_python/Grenzwerte.txt
Normal file
40
Software/ZK_python/Grenzwerte.txt
Normal file
@ -0,0 +1,40 @@
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
|
||||
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
|
||||
|
||||
|
||||
Monat Auf Unter (in Stunden)
|
||||
_________________
|
||||
Jan 8 17
|
||||
|
||||
Feb 8 17.5
|
||||
|
||||
März 7 18
|
||||
|
||||
April 7 20
|
||||
|
||||
Mai 6 20.5
|
||||
|
||||
Juni 5.5 21.5
|
||||
|
||||
Juli 6 21.5
|
||||
|
||||
Aug 6.5 20.5
|
||||
|
||||
Sep 7.5 19.5
|
||||
|
||||
Okt 7.5 17.5
|
||||
|
||||
Nov 8 16.5
|
||||
|
||||
Dez 8 16.5
|
||||
|
||||
|
||||
|
||||
Auf = Wann geht die Sonne am spätesten im Monat auf.
|
||||
|
||||
Unter = Wann geht die Sonne am frühsten im Monat unter.
|
||||
|
||||
http://sunrisesunset.de/
|
||||
9
Software/ZK_python/Kameraeinstellungen.txt
Normal file
9
Software/ZK_python/Kameraeinstellungen.txt
Normal file
@ -0,0 +1,9 @@
|
||||
Version 0.1
|
||||
Stand 16.03.2017
|
||||
----------------------------------------------------------------------------
|
||||
|
||||
|
||||
camera.iso = 50
|
||||
camera.resolution = (3280,2464)
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
21
Software/ZK_python/Ordnerstruktur.txt
Normal file
21
Software/ZK_python/Ordnerstruktur.txt
Normal file
@ -0,0 +1,21 @@
|
||||
Bilder:
|
||||
/camera01/session01/2018/01_January
|
||||
|
||||
|
||||
Logs:
|
||||
/logs/2018/01_January
|
||||
|
||||
Monate:
|
||||
|
||||
01_January
|
||||
02_February
|
||||
03_March
|
||||
04_April
|
||||
05_May
|
||||
06_June
|
||||
07_July
|
||||
08_August
|
||||
09_September
|
||||
10_October
|
||||
11_November
|
||||
12_December
|
||||
9
Software/ZK_python/TempGrenzwerte.txt
Normal file
9
Software/ZK_python/TempGrenzwerte.txt
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
|
||||
<= -5<>C --> Critical Coold
|
||||
|
||||
<= 5<>C --> Warning Coold
|
||||
|
||||
>= 45<34>C --> Warning Hot
|
||||
|
||||
>= 50<35>C --> Critical Hot
|
||||
185
Software/ZK_python/camera_rc1_006.py
Normal file
185
Software/ZK_python/camera_rc1_006.py
Normal file
@ -0,0 +1,185 @@
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_directory = configParser.get('config', 'ftp_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
print('done!')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_directory)
|
||||
f = open("picture_Number.txt",'r')
|
||||
wakeup_Time = [7,7,6,5,5,4,4,5,5,6,6,6]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 30
|
||||
time_name = time.asctime(time.localtime(time.time()))
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = '01_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit Name:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #nimmt bild 'image.jpg' auf
|
||||
picture_number += 1
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number))
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000:
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
if datetime.now().hour > wakeup_Time[datetime.now().month] and datetime.now().hour < 17:
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until'+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until'+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else:
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
193
Software/ZK_python/camera_rc1_007.py
Normal file
193
Software/ZK_python/camera_rc1_007.py
Normal file
@ -0,0 +1,193 @@
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
print('done!')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("picture_Number.txt",'r')
|
||||
wakeup_Time = [7,7,6,5,5,4,4,5,5,6,6,6]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300
|
||||
time_name = time.asctime(time.localtime(time.time()))
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = '01_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit Name:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #nimmt bild 'image.jpg' auf
|
||||
picture_number += 1
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number))
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000:
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
if datetime.now().hour > wakeup_Time[datetime.now().month] and datetime.now().hour < 17:
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until'+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else:
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
199
Software/ZK_python/camera_rc1_008.py
Normal file
199
Software/ZK_python/camera_rc1_008.py
Normal file
@ -0,0 +1,199 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
print('done!')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("picture_Number.txt",'r')
|
||||
wakeup_Time = [7,7,6,5,5,4,4,5,5,6,6,6]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300
|
||||
time_name = time.asctime(time.localtime(time.time()))
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit Name:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #nimmt bild 'image.jpg' auf
|
||||
picture_number += 1
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number))
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000:
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
if datetime.now().hour > wakeup_Time[datetime.now().month] and datetime.now().hour < 17:
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until'+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else:
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
245
Software/ZK_python/camera_rc1_009.py
Normal file
245
Software/ZK_python/camera_rc1_009.py
Normal file
@ -0,0 +1,245 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#009
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 009'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
print('done!')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("picture_Number.txt",'r')
|
||||
wakeup_Time = [7,7,6,5,5,4,4,5,5,6,6,6]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
if datetime.now().hour > wakeup_Time[datetime.now().month] and datetime.now().hour < 17: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until'+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
248
Software/ZK_python/camera_rc1_010.py
Normal file
248
Software/ZK_python/camera_rc1_010.py
Normal file
@ -0,0 +1,248 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#009
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 009'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
print('done!')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("picture_Number.txt",'r')
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month]*60*60 and seconds1 < bed_Time[datetime.now().month]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until'+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
248
Software/ZK_python/camera_rc1_011.py
Normal file
248
Software/ZK_python/camera_rc1_011.py
Normal file
@ -0,0 +1,248 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#011
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 011'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
print('done!')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("picture_Number.txt",'r')
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
266
Software/ZK_python/camera_rc1_012.py
Normal file
266
Software/ZK_python/camera_rc1_012.py
Normal file
@ -0,0 +1,266 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#012
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 012'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
print('done!')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("picture_Number.txt",'r')
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
268
Software/ZK_python/camera_rc1_013.py
Normal file
268
Software/ZK_python/camera_rc1_013.py
Normal file
@ -0,0 +1,268 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#012
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
import piexif
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 012'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
print('done!')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("picture_Number.txt",'r')
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
piexif.remove(picture_path)
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
267
Software/ZK_python/camera_rc1_014.py
Normal file
267
Software/ZK_python/camera_rc1_014.py
Normal file
@ -0,0 +1,267 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#014
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
import piexif
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configParser.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'config.txt'))
|
||||
|
||||
version = 'starting camera version 014'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
print('done!')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("picture_Number.txt",'r')
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
piexif.remove(picture_path)
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
277
Software/ZK_python/camera_rc2_015.py
Normal file
277
Software/ZK_python/camera_rc2_015.py
Normal file
@ -0,0 +1,277 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#015
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
import piexif
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configParser.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), '/home/pi/config.txt'))
|
||||
|
||||
version = 'starting camera version 015'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enamble_modem = configParser.get('config', 'enamble_modem')
|
||||
print('done!')
|
||||
|
||||
if enamble_modem == 'true':
|
||||
print('start modem connection ...')
|
||||
logging.info('start modem connection ...')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('connection successful')
|
||||
ogging.info(stdoutdata)
|
||||
logging.info('connection successful')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' and enamble_modem == 'true':in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
piexif.remove(picture_path)
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if 'nicht' and enamble_modem == 'true':in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
279
Software/ZK_python/camera_rc2_016.py
Normal file
279
Software/ZK_python/camera_rc2_016.py
Normal file
@ -0,0 +1,279 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#015
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
import piexif
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configParser.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), '/home/pi/config.txt'))
|
||||
|
||||
version = 'starting camera version 015'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enamble_modem = configParser.get('config', 'enamble_modem')
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
if enamble_modem == 'true':
|
||||
print('start modem connection ...')
|
||||
logging.info('start modem connection ...')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('connection successful')
|
||||
ogging.info(stdoutdata)
|
||||
logging.info('connection successful')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enamble_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
piexif.remove(picture_path)
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enamble_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
296
Software/ZK_python/camera_rc2_019.py
Normal file
296
Software/ZK_python/camera_rc2_019.py
Normal file
@ -0,0 +1,296 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#019
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
import piexif
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configParser.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), '/home/pi/config.txt'))
|
||||
|
||||
version = 'starting camera version 019'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enamble_modem = configParser.get('config', 'enamble_modem')
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
if enamble_modem == 'true':
|
||||
print('start modem connection ...')
|
||||
logging.info('start modem connection ...')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('connection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('connection successful')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
picture_number = int(f.read())
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enamble_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
piexif.remove(picture_path)
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enamble_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
305
Software/ZK_python/camera_rc3_021.py
Normal file
305
Software/ZK_python/camera_rc3_021.py
Normal file
@ -0,0 +1,305 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#021
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
#configParser = configparser.RawConfigParser()
|
||||
#configParser.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), '/home/pi/config.txt'))
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 021'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
if enable_modem == 'true':
|
||||
print('start modem connection ...')
|
||||
logging.info('start modem connection ...')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('connection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('connection successful')
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = ''
|
||||
camera.exif_tags['IFD0.Model '] = ''
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
297
Software/ZK_python/camera_rc3_022.py
Normal file
297
Software/ZK_python/camera_rc3_022.py
Normal file
@ -0,0 +1,297 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#022
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 022'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
logging.info('no internet connection on startup trying to reconnect later!')
|
||||
logging.info(str(ea))
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
297
Software/ZK_python/camera_rc3_023.py
Normal file
297
Software/ZK_python/camera_rc3_023.py
Normal file
@ -0,0 +1,297 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#023
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 023'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
logging.info('no internet connection on startup trying to reconnect later!')
|
||||
logging.info(str(ea))
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
#os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
#os.remove(log_directory+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
293
Software/ZK_python/camera_rc3_024.py
Normal file
293
Software/ZK_python/camera_rc3_024.py
Normal file
@ -0,0 +1,293 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#024
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 024'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
142
Software/ZK_python/camera_rc_001.py
Normal file
142
Software/ZK_python/camera_rc_001.py
Normal file
@ -0,0 +1,142 @@
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP('schuttercloud.com','uploader','1234')
|
||||
ftp.cwd('/camera01')
|
||||
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
camera.shutter_speed = 500000
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
wakeup_Time = [7,7,6,5,5,4,4,5,5,6,6,6]
|
||||
i=0
|
||||
camera_number = '01'
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
print ('New ftp connection successfull! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successfull! current dicertory is: '+ftp.pwd())
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload succesful!')
|
||||
logging.info('upload succesful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect('schuttercloud.com')
|
||||
ftp.login('uploader','1234')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
return
|
||||
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 3000
|
||||
time_name = time.asctime(time.localtime(time.time()))
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = '01_'+str(i).zfill(9)+'_'+time_name + '.jpg'
|
||||
picture_path = '/home/pi/Pictures/' + picture_name
|
||||
|
||||
print ("Zeit Name:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #nimmt bild 'image.jpg' auf
|
||||
i=i+1
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000:
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
if datetime.now().hour > wakeup_Time[datetime.now().month] and datetime.now().hour < 17:
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + log_name, open('/home/pi/'+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect('schuttercloud.com')
|
||||
ftp.login('uploader','1234')
|
||||
ftp.storbinary('STOR ' + log_name, open('/home/pi/'+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
156
Software/ZK_python/camera_rc_002.py
Normal file
156
Software/ZK_python/camera_rc_002.py
Normal file
@ -0,0 +1,156 @@
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP('schuttercloud.com','uploader','1234')
|
||||
ftp.cwd('/camera01')
|
||||
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
camera.shutter_speed = 500000
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
wakeup_Time = [7,7,6,5,5,4,4,5,5,6,6,6]
|
||||
i=0
|
||||
camera_number = '01'
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect('schuttercloud.com')
|
||||
ftp.login('uploader','1234')
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
return
|
||||
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 3000
|
||||
time_name = time.asctime(time.localtime(time.time()))
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = '01_'+str(i).zfill(9)+'_'+time_name + '.jpg'
|
||||
picture_path = '/home/pi/Pictures/' + picture_name
|
||||
|
||||
print ("Zeit Name:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #nimmt bild 'image.jpg' auf
|
||||
i=i+1
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000:
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
if datetime.now().hour > wakeup_Time[datetime.now().month] and datetime.now().hour < 17:
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + log_name, open('/home/pi/'+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect('schuttercloud.com')
|
||||
ftp.login('uploader','1234')
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + log_name, open('/home/pi/'+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
156
Software/ZK_python/camera_rc_003.py
Normal file
156
Software/ZK_python/camera_rc_003.py
Normal file
@ -0,0 +1,156 @@
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP('schuttercloud.com','uploader','1234')
|
||||
ftp.cwd('/camera01')
|
||||
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
camera.shutter_speed = 500000
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
wakeup_Time = [7,7,6,5,5,4,4,5,5,6,6,6]
|
||||
i=0
|
||||
camera_number = '01'
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect('schuttercloud.com')
|
||||
ftp.login('uploader','1234')
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
return
|
||||
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300
|
||||
time_name = time.asctime(time.localtime(time.time()))
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = '01_'+str(i).zfill(9)+'_'+time_name + '.jpg'
|
||||
picture_path = '/home/pi/Pictures/' + picture_name
|
||||
|
||||
print ("Zeit Name:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #nimmt bild 'image.jpg' auf
|
||||
i=i+1
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000:
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
if datetime.now().hour > wakeup_Time[datetime.now().month] and datetime.now().hour < 17:
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + log_name, open('/home/pi/'+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect('schuttercloud.com')
|
||||
ftp.login('uploader','1234')
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + log_name, open('/home/pi/'+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
158
Software/ZK_python/camera_rc_004.py
Normal file
158
Software/ZK_python/camera_rc_004.py
Normal file
@ -0,0 +1,158 @@
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP('schuttercloud.com','uploader','1234')
|
||||
ftp.cwd('/camera01')
|
||||
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
camera.shutter_speed = 500000
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
wakeup_Time = [7,7,6,5,5,4,4,5,5,6,6,6]
|
||||
i=0
|
||||
camera_number = '01'
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect('schuttercloud.com')
|
||||
ftp.login('uploader','1234')
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
return
|
||||
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300
|
||||
time_name = time.asctime(time.localtime(time.time()))
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = '01_'+str(i).zfill(9)+'_'+time_name + '.jpg'
|
||||
picture_path = '/home/pi/Pictures/' + picture_name
|
||||
|
||||
print ("Zeit Name:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #nimmt bild 'image.jpg' auf
|
||||
i=i+1
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000:
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
if datetime.now().hour > wakeup_Time[datetime.now().month] and datetime.now().hour < 17:
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + log_name, open('/home/pi/'+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect('schuttercloud.com')
|
||||
ftp.login('uploader','1234')
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + log_name, open('/home/pi/'+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
166
Software/ZK_python/camera_rc_005.py
Normal file
166
Software/ZK_python/camera_rc_005.py
Normal file
@ -0,0 +1,166 @@
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
|
||||
camera = PiCamera()
|
||||
ftp = FTP('schuttercloud.com','uploader','1234')
|
||||
ftp.cwd('/camera01')
|
||||
f = open("picture_Number.txt",'r')
|
||||
wakeup_Time = [7,7,6,5,5,4,4,5,5,6,6,6]
|
||||
picture_number = int(f.read())
|
||||
camera_number = '01'
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect('schuttercloud.com')
|
||||
ftp.login('uploader','1234')
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path) #TODO fallback test
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
return
|
||||
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 20
|
||||
time_name = time.asctime(time.localtime(time.time()))
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = '01_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = '/home/pi/Pictures/' + picture_name
|
||||
|
||||
print ("Zeit Name:", time_name)
|
||||
|
||||
try:
|
||||
camera.capture(picture_path) #nimmt bild 'image.jpg' auf
|
||||
picture_number += 1
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("picture_Number.txt",'w')
|
||||
f.write(str(picture_number))
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000:
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
if datetime.now().hour > wakeup_Time[datetime.now().month] and datetime.now().hour < 17:
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + log_name, open('/home/pi/'+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect('schuttercloud.com')
|
||||
ftp.login('uploader','1234')
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + log_name, open('/home/pi/'+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else:
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#maybe needed! needs to be tested
|
||||
# ftp.connect('schuttercloud.com')
|
||||
# ftp.login('uploader','1234')
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
294
Software/ZK_python/camera_test_025.py
Normal file
294
Software/ZK_python/camera_test_025.py
Normal file
@ -0,0 +1,294 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#025
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 025'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
299
Software/ZK_python/camera_test_026.py
Normal file
299
Software/ZK_python/camera_test_026.py
Normal file
@ -0,0 +1,299 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#026
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 026'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < 1800000: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_name)
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
300
Software/ZK_python/camera_test_027.py
Normal file
300
Software/ZK_python/camera_test_027.py
Normal file
@ -0,0 +1,300 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#027
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 027'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger().handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger().handlers = []
|
||||
logging.basicConfig(filename=log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
300
Software/ZK_python/camera_v2_028.py
Normal file
300
Software/ZK_python/camera_v2_028.py
Normal file
@ -0,0 +1,300 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#028
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 028'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('re-connecting successful!')
|
||||
logging.info('re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('Network Status: ' + stdoutdata)
|
||||
logging.info('Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('not connected, trying to reconnect')
|
||||
logging.info('not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('connected!')
|
||||
logging.info('connected!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
301
Software/ZK_python/camera_v2_029.py
Normal file
301
Software/ZK_python/camera_v2_029.py
Normal file
@ -0,0 +1,301 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#029
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 029'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
reconnect_counter = 0
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP]re-connecting successful!')
|
||||
logging.info('[FTP]re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP]upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print([sakis3g]'not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection successful')
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('trying to reconnect ...')
|
||||
logging.info('trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
301
Software/ZK_python/camera_v2_030.py
Normal file
301
Software/ZK_python/camera_v2_030.py
Normal file
@ -0,0 +1,301 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#030
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 030'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
reconnect_counter = 0
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP]re-connecting successful!')
|
||||
logging.info('[FTP]re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP]upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection successful')
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('[FTP]re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
308
Software/ZK_python/camera_v2_031.py
Normal file
308
Software/ZK_python/camera_v2_031.py
Normal file
@ -0,0 +1,308 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#031
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 031'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
reconnect_counter = 0
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP]re-connecting successful!')
|
||||
logging.info('[FTP]re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP]upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection successful')
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('[FTP]re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('could not change ftp directory !')
|
||||
logging.error('could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
308
Software/ZK_python/camera_v2_032.py
Normal file
308
Software/ZK_python/camera_v2_032.py
Normal file
@ -0,0 +1,308 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#032
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 032'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
print('done!')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
|
||||
print('reading picture number ...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
reconnect_counter = 0
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
|
||||
logging.info(version)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP]re-connecting successful!')
|
||||
logging.info('[FTP]re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP]upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection successful')
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('[FTP]re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --sudo --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('could not change ftp directory !')
|
||||
logging.error('could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
310
Software/ZK_python/camera_v2_033.py
Normal file
310
Software/ZK_python/camera_v2_033.py
Normal file
@ -0,0 +1,310 @@
|
||||
#(c)2017 Jannik Seiler
|
||||
#033
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 033'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
print('done!')
|
||||
|
||||
print('reading picture number...')
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
print('done, picture number is:',str(picture_number))
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
print('initializing logger...')
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
reconnect_counter = 0
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('done')
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
|
||||
logging.info(version)
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP]re-connecting successful!')
|
||||
logging.info('[FTP]re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP]upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection successful')
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
uploadToFTP()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('uploading todays log befor going to sleep!')
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('[FTP]re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g connect --sudo --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('could not change ftp directory !')
|
||||
logging.error('could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
uploadToFTP()
|
||||
353
Software/ZK_python/camera_v3_034.py
Normal file
353
Software/ZK_python/camera_v3_034.py
Normal file
@ -0,0 +1,353 @@
|
||||
#(c)2017 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
#034
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 034'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
|
||||
print('done!')
|
||||
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
print('initializing logger...')
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
reconnect_counter = 0
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:',str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
|
||||
if internet == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
logging.info(version)
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP]re-connecting successful!')
|
||||
logging.info('[FTP]re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP]upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g] disconnected')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection hopefully successful')
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
return
|
||||
|
||||
def checkCpuTemperature():
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
return
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + 300 #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Zeit:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
if internet == 'true': uploadToFTP()
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
if internet == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
if internet == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internet == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('[FTP]re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g] disconnected')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection hopefully successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('could not change ftp directory !')
|
||||
logging.error('could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
if internet == 'true': uploadToFTP()
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
387
Software/ZK_python/camera_v3_035.py
Normal file
387
Software/ZK_python/camera_v3_035.py
Normal file
@ -0,0 +1,387 @@
|
||||
#(c)2017 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
#035
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 035'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
|
||||
print('done!')
|
||||
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
print('initializing logger...')
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
reconnect_counter = 0
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:',str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
|
||||
if internet == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
logging.info(version)
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP]re-connecting successful!')
|
||||
logging.info('[FTP]re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP]upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g] disconnected')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection hopefully successful')
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
return
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs('/media/usb0')
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize)/(st.f_blocks * st.f_frsize))*100)
|
||||
logging.info('Free disk space ' +str(freePercentaged)+' %')
|
||||
print('Free disk space ',str(freePercentaged),' %')
|
||||
if freePercentaged < 20: logging.warning('Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('Storage Drive full, Backup Space in use!')
|
||||
|
||||
return
|
||||
|
||||
def checkCpuTemperature():
|
||||
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
|
||||
return
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + interval #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Time:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
checkDiskSpace()
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer",upload_every)
|
||||
if (internet == 'true')and(upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
logging.info('Skipping upload. Next upload in '+str(upload_every)+' pictures')
|
||||
print('Skipping upload. Next upload in',(upload_every),'pictures')
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
if internet == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
if internet == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internet == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('[FTP]re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g] disconnected')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection hopefully successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('could not change ftp directory !')
|
||||
logging.error('could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
|
||||
if (internet == 'true')and(upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
logging.info('Skipping upload. Next upload in '+str(upload_every)+' pictures')
|
||||
print('Skipping upload. Next upload in',upload_every,'pictures')
|
||||
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
407
Software/ZK_python/camera_v3_036.py
Normal file
407
Software/ZK_python/camera_v3_036.py
Normal file
@ -0,0 +1,407 @@
|
||||
#(c)2017 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
#036
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
from spidev import SpiDev
|
||||
from time import sleep
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 036'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
ioBoard = configParser.get('config', 'I/O_Board')
|
||||
|
||||
print('done!')
|
||||
|
||||
wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
print('initializing logger...')
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
reconnect_counter = 0
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
logging.info(version)
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:',str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
|
||||
if ioBoard =="true":
|
||||
print('Running with the nice IO board!')
|
||||
spi = SpiDev()
|
||||
spi.open(0,0)
|
||||
else:
|
||||
print('Running without the nice IO board!')
|
||||
|
||||
if internet == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP]re-connecting successful!')
|
||||
logging.info('[FTP]re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP]upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g] disconnected')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection hopefully successful')
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
return
|
||||
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs('/media/usb0')
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize)/(st.f_blocks * st.f_frsize))*100)
|
||||
logging.info('Free disk space ' +str(freePercentaged)+' %')
|
||||
print('Free disk space ',str(freePercentaged),' %')
|
||||
if freePercentaged < 20 and freePercentaged > 10: logging.warning('Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('Storage Drive full, Backup Space in use!')
|
||||
return
|
||||
|
||||
def checkCpuTemperature():
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
return
|
||||
|
||||
def readMPC(channel):
|
||||
adc = spi.xfer2([1, (8 + channel) << 4, 0])
|
||||
data = ((adc[1] & 3) << 8) + adc[2]
|
||||
return data
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
def checkSystemVoltage():
|
||||
systemVoltage = (((readMPC(0) / 1023.0 * 3.3)*6.62)*0.95)
|
||||
print("System Input Voltage: %.2f" % systemVoltage, "Volt")
|
||||
logging.info('System Input Voltage: ' + str("%.2f" % systemVoltage) + ' Volt')
|
||||
if systemVoltage < 11.3 and systemVoltage > 9: logging.warning('Low System Input Voltage! Maybe bad power supply or no sun.')
|
||||
if systemVoltage < 9: logging.critical('Very low System Input Voltage! Bad power supply or no sun.')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + interval #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Time:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
checkDiskSpace()
|
||||
if ioBoard =="true": checkSystemVoltage()
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer",upload_every)
|
||||
if (internet == 'true')and(upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
logging.info('Skipping upload. Next upload in '+str(upload_every)+' pictures')
|
||||
print('Skipping upload. Next upload in',(upload_every),'pictures')
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
if internet == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
if internet == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internet == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('[FTP]re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g] disconnected')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection hopefully successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('could not change ftp directory !')
|
||||
logging.error('could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
|
||||
if (internet == 'true')and(upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
logging.info('Skipping upload. Next upload in '+str(upload_every)+' pictures')
|
||||
print('Skipping upload. Next upload in',upload_every,'pictures')
|
||||
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
409
Software/ZK_python/camera_v3_037.py
Normal file
409
Software/ZK_python/camera_v3_037.py
Normal file
@ -0,0 +1,409 @@
|
||||
#(c)2017 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
#037
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
from spidev import SpiDev
|
||||
from time import sleep
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
version = 'starting camera version 037'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
ioBoard = configParser.get('config', 'I/O_Board')
|
||||
|
||||
wakeup_Time = str(configParser.get('config', 'wakeup_Time')).split(',')
|
||||
bed_Time = str(configParser.get('config', 'bed_Time')).split(',')
|
||||
print('done!')
|
||||
|
||||
#wakeup_Time = [8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8]
|
||||
#bed_Time = [17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5]
|
||||
print('initializing logger...')
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
reconnect_counter = 0
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
logging.info(version)
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:',str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
|
||||
if ioBoard =="true":
|
||||
print('Running with the nice IO board!')
|
||||
spi = SpiDev()
|
||||
spi.open(0,0)
|
||||
else:
|
||||
print('Running without the nice IO board!')
|
||||
|
||||
if internet == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464
|
||||
camera.awb_mode = 'auto'
|
||||
camera.exposure_mode = 'auto'
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP]re-connecting successful!')
|
||||
logging.info('[FTP]re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP]upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g] disconnected')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection hopefully successful')
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
return
|
||||
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs('/media/usb0')
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize)/(st.f_blocks * st.f_frsize))*100)
|
||||
logging.info('Free disk space ' +str(freePercentaged)+' %')
|
||||
print('Free disk space ',str(freePercentaged),' %')
|
||||
if freePercentaged < 20 and freePercentaged > 10: logging.warning('Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('Storage Drive full, Backup Space in use!')
|
||||
return
|
||||
|
||||
def checkCpuTemperature():
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
return
|
||||
|
||||
def readMPC(channel):
|
||||
adc = spi.xfer2([1, (8 + channel) << 4, 0])
|
||||
data = ((adc[1] & 3) << 8) + adc[2]
|
||||
return data
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
def checkSystemVoltage():
|
||||
systemVoltage = (((readMPC(0) / 1023.0 * 3.3)*6.62)*0.95)
|
||||
print("System Input Voltage: %.2f" % systemVoltage, "Volt")
|
||||
logging.info('System Input Voltage: ' + str("%.2f" % systemVoltage) + ' Volt')
|
||||
if systemVoltage < 11.3 and systemVoltage > 9: logging.warning('Low System Input Voltage! Maybe bad power supply or no sun.')
|
||||
if systemVoltage < 9: logging.critical('Very low System Input Voltage! Bad power supply or no sun.')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + interval #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Time:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
checkDiskSpace()
|
||||
if ioBoard =="true": checkSystemVoltage()
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > wakeup_Time[datetime.now().month-1]*60*60 and seconds1 < bed_Time[datetime.now().month-1]*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer",upload_every)
|
||||
if (internet == 'true')and(upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
logging.info('Skipping upload. Next upload in '+str(upload_every)+' pictures')
|
||||
print('Skipping upload. Next upload in',(upload_every),'pictures')
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
if internet == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
if internet == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internet == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('[FTP]re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g] disconnected')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection hopefully successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('could not change ftp directory !')
|
||||
logging.error('could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (wakeup_Time[datetime.now().month-1]*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (wakeup_Time[datetime.now().month-1]*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(wakeup_Time[datetime.now().month-1]*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (wakeup_Time[datetime.now().month-1]*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
|
||||
if (internet == 'true')and(upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
logging.info('Skipping upload. Next upload in '+str(upload_every)+' pictures')
|
||||
print('Skipping upload. Next upload in',upload_every,'pictures')
|
||||
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
443
Software/ZK_python/camera_v3_038.py
Normal file
443
Software/ZK_python/camera_v3_038.py
Normal file
@ -0,0 +1,443 @@
|
||||
#(c)2017 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
#038
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
from spidev import SpiDev
|
||||
from time import sleep
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
#ioBoard
|
||||
piezo = 25
|
||||
bootSwitch = 17
|
||||
|
||||
version = 'starting camera version 038'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
ioBoard = configParser.get('config', 'I/O_Board')
|
||||
mute = configParser.get('config', 'mute')
|
||||
wakeup_Time = str(configParser.get('config', 'wakeup_Time')).split(',')
|
||||
bed_Time = str(configParser.get('config', 'bed_Time')).split(',')
|
||||
print('done!')
|
||||
|
||||
|
||||
print('initializing logger...')
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
reconnect_counter = 0
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
logging.info(version)
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt",'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:',str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
|
||||
if ioBoard =="true":
|
||||
print('Running with the nice IO board!')
|
||||
spi = SpiDev()
|
||||
spi.open(0,0)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(piezo, GPIO.OUT)
|
||||
GPIO.setup(bootSwitch, GPIO.IN)
|
||||
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464s
|
||||
camera.start_preview()
|
||||
#camera.start_preview(fullscreen=False, window = (100, 20, 960, 720))
|
||||
print('---starting sharpness tool---')
|
||||
print('--------normal output--------')
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
sleep(5)
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.stop_preview()
|
||||
print('---------zoom output---------')
|
||||
camera.crop = (0.5, 0.5, 0.3, 0.4)
|
||||
camera.start_preview()
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
else:
|
||||
camera.stop_preview()
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
else:
|
||||
print('Running without the nice IO board!')
|
||||
|
||||
if internet == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
try:
|
||||
ftp = FTP(ftp_url,ftp_user,ftp_pw,timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as ea:
|
||||
print('no internet connection on startup trying to reconnect later!')
|
||||
print ('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: '+ftp.pwd())
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
#camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
#camera.shutter_speed = 500000
|
||||
camera.resolution = (3280,2464) #aufloesung der bilder maximal 3280,2464s
|
||||
camera.exposure_mode = 'auto'
|
||||
camera.crop = (0, 0, 0, 0)
|
||||
#camera.brightness = 50 #0 bis 100; default 50
|
||||
#camera.contrast = 0 #-100 bis 100; default 0
|
||||
#camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
#camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
#camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
#get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp)/1000
|
||||
|
||||
#uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP]re-connecting successful!')
|
||||
logging.info('[FTP]re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP]upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except ftplib.all_errors as e9:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g] disconnected')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection hopefully successful')
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
return
|
||||
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs('/media/usb0')
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize)/(st.f_blocks * st.f_frsize))*100)
|
||||
logging.info('Free disk space ' +str(freePercentaged)+' %')
|
||||
print('Free disk space ',str(freePercentaged),' %')
|
||||
if freePercentaged < 20 and freePercentaged > 10: logging.warning('Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('Storage Drive full, Backup Space in use!')
|
||||
return
|
||||
|
||||
def checkCpuTemperature():
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature())+ ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature())+ ' C')
|
||||
return
|
||||
|
||||
def readMPC(channel):
|
||||
adc = spi.xfer2([1, (8 + channel) << 4, 0])
|
||||
data = ((adc[1] & 3) << 8) + adc[2]
|
||||
return data
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix>0:
|
||||
print("sleeping now",time_sleep_unix,"seconds"+'\n')
|
||||
logging.info('sleeping now '+str(time_sleep_unix)+' seconds'+'\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
def checkSystemVoltage():
|
||||
systemVoltage = (((readMPC(0) / 1023.0 * 3.3)*6.62)*0.95)
|
||||
print("System Input Voltage: %.2f" % systemVoltage, "Volt")
|
||||
logging.info('System Input Voltage: ' + str("%.2f" % systemVoltage) + ' Volt')
|
||||
if systemVoltage < 11.3 and systemVoltage > 9: logging.warning('Low System Input Voltage! Maybe bad power supply or no sun.')
|
||||
if systemVoltage < 9: logging.critical('Very low System Input Voltage! Bad power supply or no sun.')
|
||||
return
|
||||
|
||||
#main programm (endless loop)
|
||||
while 1:
|
||||
|
||||
time_next_unix = time.time() + interval #time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) #get time for picture_name
|
||||
time_name = time_name.replace(" ","_")
|
||||
time_name = time_name.replace(":","-")
|
||||
picture_name = camera_number+'_'+str(picture_number).zfill(6)+'_'+time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print ("Time:", time_name)
|
||||
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = '5 minute interval'
|
||||
|
||||
if mute == "false": GPIO.output(piezo, GPIO.HIGH)
|
||||
camera.capture(picture_path) #capture image and save to picture_path (picture_directory + picture_name)
|
||||
if mute == "false": GPIO.output(piezo, GPIO.LOW)
|
||||
|
||||
picture_number += 1 #increase picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print ('Picture size:',os.path.getsize(picture_path),"Byte")
|
||||
logging.info('Picture size: '+str(os.path.getsize(picture_path))+' Byte')
|
||||
checkDiskSpace()
|
||||
if ioBoard =="true": checkSystemVoltage()
|
||||
if os.path.getsize(picture_path) < picture_size: #if picture size is under 1800000 Byte we could sleep
|
||||
print ('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds #seconds since midnight
|
||||
if seconds1 > float(wakeup_Time[datetime.now().month-1])*60*60 and seconds1 < float(bed_Time[datetime.now().month-1])*60*60: #if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print ('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer",upload_every)
|
||||
if (internet == 'true')and(upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
logging.info('Skipping upload. Next upload in '+str(upload_every)+' pictures')
|
||||
print('Skipping upload. Next upload in',(upload_every),'pictures')
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
|
||||
else:
|
||||
os.remove(picture_path) #deleting picture because it's night!
|
||||
picture_number -= 1 #decrease picture_number
|
||||
print('picture_number:',str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt",'w')
|
||||
f.write(str(picture_number)) #save picture_number to a text file
|
||||
f.close()
|
||||
#uploading log file
|
||||
print ('skipping picture upload!')
|
||||
print('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
if internet == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
if internet == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internet == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e:
|
||||
print ('upload failed!')
|
||||
print (e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP]trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('[FTP]re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except ftplib.all_errors as e8:
|
||||
print('[FTP]re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP]re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('/usr/bin/modem3g/sakis3g status')
|
||||
print('[sakis3g]Network Status: ' + stdoutdata)
|
||||
logging.info('[sakis3g]Network Status: ' + stdoutdata)
|
||||
if enable_modem == 'true' and 'Nicht' in stdoutdata:
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
print('[sakis3g]not connected, trying to reconnect')
|
||||
logging.info('[sakis3g]not connected, trying to reconnect')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g] disconnected')
|
||||
stdoutdata = subprocess.getoutput("/usr/bin/modem3g/sakis3g --sudo disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[sakis3g]reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[sakis3g]reconnection hopefully successful')
|
||||
try:
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user,ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
logging.info('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory+log_name,'rb'),1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 '+log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
logging.info('[sakis3g]if this appears you are connected or there is a bug!')
|
||||
except Exception as e3:
|
||||
print(str(e3))
|
||||
logging.error(str(e3))
|
||||
|
||||
#try? check inet connection -> /usr/bin/modem3g/sakis3g status
|
||||
# if nicht dann neu verbinden -> /usr/bin/modem3g/sakis3g connect --console APN='web.vodafone.de' USBINTERFACE='0' USBMODEM='12d1:1436'
|
||||
# danach nochmal status if dann status = nicht dann reboot
|
||||
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('could not change ftp directory !')
|
||||
logging.error('could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
#sleep until next morning
|
||||
print ('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds #seconds since midnight
|
||||
if seconds < (float(wakeup_Time[datetime.now().month-1])*60*60): #if time between 0am and wakeup_Time
|
||||
sleep_time = (float(wakeup_Time[datetime.now().month-1])*60*60)-seconds
|
||||
else: #if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400-seconds+(float(wakeup_Time[datetime.now().month-1])*60*60)
|
||||
print ('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
logging.info('sleeping now '+str(sleep_time)+' seconds or until '+str(wakeup_Time[datetime.now().month-1])+'am!')
|
||||
sleep(sleep_time-10)
|
||||
|
||||
#wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds)+' < '+str((wakeup_Time[datetime.now().month-1]*60*60)))
|
||||
while(datetime.now() - midnight).seconds < (float(wakeup_Time[datetime.now().month-1])*60*60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number+'_'+str(datetime.now().strftime("%A %d. %B %Y"))+'.log'
|
||||
log_name = log_name.replace(" ","-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory+log_name,format='%(asctime)s [%(levelname)s] %(message)s',level=logging.DEBUG,datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for '+str(sleep_time)+' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for '+str(sleep_time)+' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
|
||||
if (internet == 'true')and(upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
logging.info('Skipping upload. Next upload in '+str(upload_every)+' pictures')
|
||||
print('Skipping upload. Next upload in',upload_every,'pictures')
|
||||
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
533
Software/ZK_python/camera_v4_039.py
Normal file
533
Software/ZK_python/camera_v4_039.py
Normal file
@ -0,0 +1,533 @@
|
||||
# (c)2017 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
# 039
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import http.client as httplib
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
from spidev import SpiDev
|
||||
from time import sleep
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
# get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open("/sys/class/thermal/thermal_zone0/temp")
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp) / 1000
|
||||
|
||||
|
||||
# uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except Exception as e:
|
||||
print('upload failed!')
|
||||
print(e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP] trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] re-connecting successful!')
|
||||
logging.info('[FTP] re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP] upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except Exception as e9:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
return
|
||||
|
||||
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs('/media/usb0')
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize) / (st.f_blocks * st.f_frsize)) * 100)
|
||||
logging.info('Free disk space ' + str(freePercentaged) + ' %')
|
||||
print('Free disk space ', str(freePercentaged), ' %')
|
||||
if freePercentaged < 20 and freePercentaged > 10: logging.warning('Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('Storage Drive full, Backup Space in use!')
|
||||
return
|
||||
|
||||
|
||||
def checkCpuTemperature():
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature()) + ' C')
|
||||
return
|
||||
|
||||
|
||||
def readMPC(channel):
|
||||
adc = spi.xfer2([1, (8 + channel) << 4, 0])
|
||||
data = ((adc[1] & 3) << 8) + adc[2]
|
||||
return data
|
||||
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix > 0:
|
||||
print("sleeping now", time_sleep_unix, "seconds" + '\n')
|
||||
logging.info('sleeping now ' + str(time_sleep_unix) + ' seconds' + '\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
|
||||
def checkSystemVoltage():
|
||||
systemVoltage = (((readMPC(0) / 1023.0 * 3.3) * 6.62) * 0.95)
|
||||
print("System Input Voltage: %.2f" % systemVoltage, "Volt")
|
||||
logging.info('System Input Voltage: ' + str("%.2f" % systemVoltage) + ' Volt')
|
||||
if systemVoltage < 11.3 and systemVoltage > 9: logging.warning(
|
||||
'Low System Input Voltage! Maybe bad power supply or no sun.')
|
||||
if systemVoltage < 9: logging.critical('Very low System Input Voltage! Bad power supply or no sun.')
|
||||
return
|
||||
|
||||
|
||||
def reconnectToInternet():
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
print('Server is not online!')
|
||||
logging.info('Internet connection established')
|
||||
logging.error('Server is not online!')
|
||||
global interntTmp
|
||||
interntTmp = 'true'
|
||||
#checkConnectionToFTPServer()
|
||||
return 'true'
|
||||
except Exception as e2:
|
||||
conn.close()
|
||||
print('no Internet connection')
|
||||
print('trying to reconnect to internet')
|
||||
logging.error('no Internet connection!')
|
||||
logging.info('trying to reconnect to internet')
|
||||
if enable_modem == 'true':
|
||||
print('[MODEM] no UMTS connection')
|
||||
logging.error('[MODEM] no UMTS connection')
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/modem3g/sakis3g status')
|
||||
print('[MODEM] Network Status: ' + stdoutdata)
|
||||
logging.info('[MODEM] Network Status: ' + stdoutdata)
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[MODEM] disconnected')
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[MODEM] reconnection successful')
|
||||
logging.info(stdoutdata)
|
||||
sleep(10)
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
logging.info('Internet connection established')
|
||||
return 'true'
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e5))
|
||||
logging.error(str(e5))
|
||||
return 'false'
|
||||
except Exception as e4:
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
return 'false'
|
||||
else:
|
||||
print('LAN or WLAN not connected!')
|
||||
logging.error('LAN or WLAN not connected!')
|
||||
return 'false'
|
||||
|
||||
def checkConnectionToFTPServer():
|
||||
try:
|
||||
# ping ftp url instead something else
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
try:
|
||||
global ftp
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
global internetTmp
|
||||
internetTmp = 'true'
|
||||
global upload_every
|
||||
upload_every = upload_only
|
||||
except Exception as ea:
|
||||
print('no internet or ftp connection trying to reconnect later!')
|
||||
logging.error('no internet or ftp connection trying to reconnect later!')
|
||||
print('Server connection established')
|
||||
logging.info('Server connection established')
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('Server connection not established')
|
||||
logging.info('Server connection not established')
|
||||
reconnectToInternet()
|
||||
|
||||
return
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
# ioBoard
|
||||
piezo = 25
|
||||
bootSwitch = 17
|
||||
|
||||
version = 'starting camera version 039'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
ioBoard = configParser.get('config', 'I/O_Board')
|
||||
mute = configParser.get('config', 'mute')
|
||||
wakeup_Time = str(configParser.get('config', 'wakeup_Time')).split(',')
|
||||
bed_Time = str(configParser.get('config', 'bed_Time')).split(',')
|
||||
print('done!')
|
||||
|
||||
print('initializing logger...')
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
logging.info(version)
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:', str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
internetTmp = internet
|
||||
|
||||
|
||||
if ioBoard == "true":
|
||||
print('Running with the nice IO board!')
|
||||
spi = SpiDev()
|
||||
spi.open(0, 0)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setup(piezo, GPIO.OUT)
|
||||
GPIO.setup(bootSwitch, GPIO.IN)
|
||||
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.resolution = (3280, 2464) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.start_preview()
|
||||
# camera.start_preview(fullscreen=False, window = (100, 20, 960, 720))
|
||||
print('---starting sharpness tool---')
|
||||
print('--------normal output--------')
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
sleep(5)
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.stop_preview()
|
||||
print('---------zoom output---------')
|
||||
camera.crop = (0.5, 0.5, 0.3, 0.4)
|
||||
camera.start_preview()
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
else:
|
||||
camera.stop_preview()
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
else:
|
||||
print('Running without the nice IO board!')
|
||||
|
||||
if internetTmp == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
if enable_modem == 'true': reconnectToInternet()
|
||||
try:
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
except Exception as ea:
|
||||
print('no internet or ftp connection on startup trying to reconnect later!')
|
||||
logging.error('no internet or ftp connection on startup trying to reconnect later!')
|
||||
internetTmp = 'false'
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
# camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
# camera.shutter_speed = 500000
|
||||
camera.resolution = (3280, 2464) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.exposure_mode = 'auto'
|
||||
camera.crop = (0, 0, 0, 0)
|
||||
|
||||
# camera.brightness = 50 #0 bis 100; default 50
|
||||
# camera.contrast = 0 #-100 bis 100; default 0
|
||||
# camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
# camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
# camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
|
||||
|
||||
# main programm (endless loop)
|
||||
while 1:
|
||||
time_next_unix = time.time() + interval # time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) # get time for picture_name
|
||||
time_name = time_name.replace(" ", "_")
|
||||
time_name = time_name.replace(":", "-")
|
||||
picture_name = camera_number + '_' + str(picture_number).zfill(6) + '_' + time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print("Time:", time_name)
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = 'interval capture'
|
||||
|
||||
if mute == "false": GPIO.output(piezo, GPIO.HIGH)
|
||||
print('Capturing image')
|
||||
logging.info('[Camera] Capturing image')
|
||||
camera.capture(picture_path) # capture image and save to picture_path (picture_directory + picture_name)
|
||||
if mute == "false": GPIO.output(piezo, GPIO.LOW)
|
||||
|
||||
picture_number += 1 # increase picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print('Picture size:', os.path.getsize(picture_path), "Byte")
|
||||
logging.info('Picture size: ' + str(os.path.getsize(picture_path)) + ' Byte')
|
||||
checkDiskSpace()
|
||||
if ioBoard == "true": checkSystemVoltage()
|
||||
if os.path.getsize(picture_path) < picture_size:
|
||||
print('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds # seconds since midnight
|
||||
if seconds1 > float(wakeup_Time[datetime.now().month - 1]) * 60 * 60 and seconds1 < float(bed_Time[
|
||||
datetime.now().month - 1]) * 60 * 60: # if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer", upload_every)
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
else:
|
||||
os.remove(picture_path) # deleting picture because it's night!
|
||||
picture_number -= 1 # decrease picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
# uploading log file
|
||||
print('skipping picture upload!')
|
||||
print('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internetTmp == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e:
|
||||
print('upload failed!')
|
||||
print(e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('[FTP] re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e8:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
try:
|
||||
print()
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print('[FTP] re-connecting failed!')
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('could not change ftp directory !')
|
||||
logging.error('could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
# sleep until next morning
|
||||
print('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds # seconds since midnight
|
||||
if seconds < (
|
||||
float(wakeup_Time[
|
||||
datetime.now().month - 1]) * 60 * 60): # if time between 0am and wakeup_Time
|
||||
sleep_time = (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60) - seconds
|
||||
else: # if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400 - seconds + (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60)
|
||||
print('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
logging.info('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
sleep(sleep_time - 10)
|
||||
|
||||
# wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds) + ' < ' + str(
|
||||
(wakeup_Time[datetime.now().month - 1] * 60 * 60)))
|
||||
while (datetime.now() - midnight).seconds < (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for ' + str(sleep_time) + ' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for ' + str(sleep_time) + ' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
checkConnectionToFTPServer()
|
||||
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
|
||||
|
||||
535
Software/ZK_python/camera_v4_040.py
Normal file
535
Software/ZK_python/camera_v4_040.py
Normal file
@ -0,0 +1,535 @@
|
||||
# (c)2017 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
# 040
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import http.client as httplib
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
from spidev import SpiDev
|
||||
from time import sleep
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
# get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open("/sys/class/thermal/thermal_zone0/temp")
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp) / 1000
|
||||
|
||||
|
||||
# uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('upload successful!')
|
||||
except Exception as e:
|
||||
print('upload failed!')
|
||||
print(e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP] trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] re-connecting successful!')
|
||||
logging.info('[FTP] re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP] upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except Exception as e9:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
return
|
||||
|
||||
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs('/media/usb0')
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize) / (st.f_blocks * st.f_frsize)) * 100)
|
||||
logging.info('Free disk space ' + str(freePercentaged) + ' %')
|
||||
print('Free disk space ', str(freePercentaged), ' %')
|
||||
if freePercentaged < 20 and freePercentaged > 10: logging.warning('Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('Storage Drive full, Backup Space in use!')
|
||||
return
|
||||
|
||||
|
||||
def checkCpuTemperature():
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
else:
|
||||
logging.info('Temperature: ' + str(getCpuTemperature()) + ' C')
|
||||
return
|
||||
|
||||
|
||||
def readMPC(channel):
|
||||
adc = spi.xfer2([1, (8 + channel) << 4, 0])
|
||||
data = ((adc[1] & 3) << 8) + adc[2]
|
||||
return data
|
||||
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix > 0:
|
||||
print("sleeping now", time_sleep_unix, "seconds" + '\n')
|
||||
logging.info('sleeping now ' + str(time_sleep_unix) + ' seconds' + '\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
|
||||
def checkSystemVoltage():
|
||||
systemVoltage = (((readMPC(0) / 1023.0 * 3.3) * 6.62) * 0.95)
|
||||
print("System Input Voltage: %.2f" % systemVoltage, "Volt")
|
||||
logging.info('System Input Voltage: ' + str("%.2f" % systemVoltage) + ' Volt')
|
||||
if systemVoltage < 11.3 and systemVoltage > 9: logging.warning(
|
||||
'Low System Input Voltage! Maybe bad power supply or no sun.')
|
||||
if systemVoltage < 9: logging.critical('Very low System Input Voltage! Bad power supply or no sun.')
|
||||
return
|
||||
|
||||
|
||||
def reconnectToInternet():
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
print('Server is maybe not online!')
|
||||
logging.info('Internet connection established')
|
||||
logging.error('Server maybe is not online!')
|
||||
global interntTmp
|
||||
interntTmp = 'true'
|
||||
#checkConnectionToFTPServer()
|
||||
return 'true'
|
||||
except Exception as e2:
|
||||
conn.close()
|
||||
print('no Internet connection')
|
||||
print('trying to reconnect to internet')
|
||||
logging.error('no Internet connection!')
|
||||
logging.info('trying to reconnect to internet')
|
||||
if enable_modem == 'true':
|
||||
print('[MODEM] no UMTS connection')
|
||||
logging.error('[MODEM] no UMTS connection')
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/modem3g/sakis3g status')
|
||||
print('[MODEM] Network Status: ' + stdoutdata)
|
||||
logging.info('[MODEM] Network Status: ' + stdoutdata)
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[MODEM] disconnected')
|
||||
sleep(20)
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[MODEM] reconnection successful attempt')
|
||||
logging.info('[MODEM] reconnection successful attempt')
|
||||
logging.info(stdoutdata)
|
||||
sleep(10)
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
logging.info('Internet connection established')
|
||||
return 'true'
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e5))
|
||||
logging.error(str(e5))
|
||||
return 'false'
|
||||
except Exception as e4:
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
return 'false'
|
||||
else:
|
||||
print('LAN or WLAN not connected!')
|
||||
logging.error('LAN or WLAN not connected!')
|
||||
return 'false'
|
||||
|
||||
def checkConnectionToFTPServer():
|
||||
try:
|
||||
# ping ftp url instead something else
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
try:
|
||||
global ftp
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
global internetTmp
|
||||
internetTmp = 'true'
|
||||
global upload_every
|
||||
upload_every = upload_only
|
||||
except Exception as ea:
|
||||
print('no internet or ftp connection trying to reconnect later!')
|
||||
logging.error('no internet or ftp connection trying to reconnect later!')
|
||||
print('Server connection established')
|
||||
logging.info('Server connection established')
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('Server connection not established')
|
||||
logging.info('Server connection not established')
|
||||
reconnectToInternet()
|
||||
|
||||
return
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
# ioBoard
|
||||
piezo = 25
|
||||
bootSwitch = 17
|
||||
|
||||
version = 'starting camera version 040'
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
ioBoard = configParser.get('config', 'I/O_Board')
|
||||
mute = configParser.get('config', 'mute')
|
||||
wakeup_Time = str(configParser.get('config', 'wakeup_Time')).split(',')
|
||||
bed_Time = str(configParser.get('config', 'bed_Time')).split(',')
|
||||
print('done!')
|
||||
|
||||
print('initializing logger...')
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
logging.info(version)
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:', str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
internetTmp = internet
|
||||
|
||||
|
||||
if ioBoard == "true":
|
||||
print('Running with the nice IO board!')
|
||||
spi = SpiDev()
|
||||
spi.open(0, 0)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setup(piezo, GPIO.OUT)
|
||||
GPIO.setup(bootSwitch, GPIO.IN)
|
||||
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.resolution = (3280, 2464) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.start_preview()
|
||||
# camera.start_preview(fullscreen=False, window = (100, 20, 960, 720))
|
||||
print('---starting sharpness tool---')
|
||||
print('--------normal output--------')
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
sleep(5)
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.stop_preview()
|
||||
print('---------zoom output---------')
|
||||
camera.crop = (0.5, 0.5, 0.3, 0.4)
|
||||
camera.start_preview()
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
else:
|
||||
camera.stop_preview()
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
else:
|
||||
print('Running without the nice IO board!')
|
||||
|
||||
if internetTmp == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
if enable_modem == 'true': reconnectToInternet()
|
||||
try:
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
except Exception as ea:
|
||||
print('no internet or ftp connection on startup trying to reconnect later!')
|
||||
logging.error('no internet or ftp connection on startup trying to reconnect later!')
|
||||
internetTmp = 'false'
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
# camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
# camera.shutter_speed = 500000
|
||||
camera.resolution = (3280, 2464) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.exposure_mode = 'auto'
|
||||
camera.crop = (0, 0, 0, 0)
|
||||
|
||||
# camera.brightness = 50 #0 bis 100; default 50
|
||||
# camera.contrast = 0 #-100 bis 100; default 0
|
||||
# camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
# camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
# camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
|
||||
|
||||
# main programm (endless loop)
|
||||
while 1:
|
||||
time_next_unix = time.time() + interval # time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) # get time for picture_name
|
||||
time_name = time_name.replace(" ", "_")
|
||||
time_name = time_name.replace(":", "-")
|
||||
picture_name = camera_number + '_' + str(picture_number).zfill(6) + '_' + time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print("Time:", time_name)
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = 'interval capture'
|
||||
|
||||
if mute == "false": GPIO.output(piezo, GPIO.HIGH)
|
||||
print('Capturing image')
|
||||
logging.info('[Camera] Capturing image')
|
||||
camera.capture(picture_path) # capture image and save to picture_path (picture_directory + picture_name)
|
||||
if mute == "false": GPIO.output(piezo, GPIO.LOW)
|
||||
|
||||
picture_number += 1 # increase picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print('Picture size:', os.path.getsize(picture_path), "Byte")
|
||||
logging.info('Picture size: ' + str(os.path.getsize(picture_path)) + ' Byte')
|
||||
checkDiskSpace()
|
||||
if ioBoard == "true": checkSystemVoltage()
|
||||
if os.path.getsize(picture_path) < picture_size:
|
||||
print('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds # seconds since midnight
|
||||
if seconds1 > float(wakeup_Time[datetime.now().month - 1]) * 60 * 60 and seconds1 < float(bed_Time[
|
||||
datetime.now().month - 1]) * 60 * 60: # if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer", upload_every)
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
else:
|
||||
os.remove(picture_path) # deleting picture because it's night!
|
||||
picture_number -= 1 # decrease picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
# uploading log file
|
||||
print('skipping picture upload!')
|
||||
print('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internetTmp == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e:
|
||||
print('upload failed!')
|
||||
print(e)
|
||||
logging.error('upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('[FTP] re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e8:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
try:
|
||||
print()
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print('[FTP] re-connecting failed!')
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('could not change ftp directory !')
|
||||
logging.error('could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
# sleep until next morning
|
||||
print('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds # seconds since midnight
|
||||
if seconds < (
|
||||
float(wakeup_Time[
|
||||
datetime.now().month - 1]) * 60 * 60): # if time between 0am and wakeup_Time
|
||||
sleep_time = (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60) - seconds
|
||||
else: # if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400 - seconds + (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60)
|
||||
print('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
logging.info('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
sleep(sleep_time - 10)
|
||||
|
||||
# wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds) + ' < ' + str(
|
||||
(wakeup_Time[datetime.now().month - 1] * 60 * 60)))
|
||||
while (datetime.now() - midnight).seconds < (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for ' + str(sleep_time) + ' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for ' + str(sleep_time) + ' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
checkConnectionToFTPServer()
|
||||
|
||||
checkCpuTemperature()
|
||||
sleepToNextPicture()
|
||||
|
||||
|
||||
|
||||
557
Software/ZK_python/camera_v4_041.py
Normal file
557
Software/ZK_python/camera_v4_041.py
Normal file
@ -0,0 +1,557 @@
|
||||
# (c)2017 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
# 041
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import http.client as httplib
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
from spidev import SpiDev
|
||||
from time import sleep
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
# get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open("/sys/class/thermal/thermal_zone0/temp")
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp) / 1000
|
||||
|
||||
|
||||
# uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('[Upload] successful!')
|
||||
except Exception as e:
|
||||
print('upload failed!')
|
||||
print(e)
|
||||
logging.error('[Upload] failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP] trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] re-connecting successful!')
|
||||
logging.info('[FTP] re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP] upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except Exception as e9:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
return
|
||||
|
||||
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs('/media/usb0')
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize) / (st.f_blocks * st.f_frsize)) * 100)
|
||||
logging.info('[Disk] Free space: ' + str(freePercentaged) + ' %')
|
||||
print('Free disk space ', str(freePercentaged), ' %')
|
||||
if freePercentaged < 20 and freePercentaged > 10: logging.warning('[Disk] Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('[Disk] Storage Drive full, Backup Space in use!')
|
||||
return
|
||||
|
||||
|
||||
def checkCpu():
|
||||
cpuUsage = (os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip())
|
||||
cpuUsage = (float(cpuUsage.replace(',', '.')))*25
|
||||
print('[CPU] Usage: ',str("%.2f" % cpuUsage),' %')
|
||||
logging.info('[CPU] Usage: '+ str("%.2f" % cpuUsage) + ' %')
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('[CPU] Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('[CPU] Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('[CPU] Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('[CPU] Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
else:
|
||||
logging.info('[CPU] Temperature: ' + str(getCpuTemperature()) + ' C')
|
||||
return
|
||||
|
||||
|
||||
def readMPC(channel):
|
||||
adc = spi.xfer2([1, (8 + channel) << 4, 0])
|
||||
data = ((adc[1] & 3) << 8) + adc[2]
|
||||
return data
|
||||
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix > 0:
|
||||
print("sleeping now", time_sleep_unix, "seconds" + '\n')
|
||||
logging.info('sleeping now ' + str(time_sleep_unix) + ' seconds' + '\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
|
||||
def checkSystemVoltage():
|
||||
systemVoltage = (((readMPC(0) / 1023.0 * 3.3) * 6.62) * 0.95)
|
||||
print("System Input Voltage: %.2f" % systemVoltage, "Volt")
|
||||
logging.info('[Voltage] ' + str("%.2f" % systemVoltage) + ' Volt')
|
||||
if systemVoltage < 11.3 and systemVoltage > 9:
|
||||
logging.warning('[Voltage] Low System Input Voltage! Maybe bad power supply or no sun.')
|
||||
if tvOut == 'auto':
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/tvservice -o ')
|
||||
print('[TV Out] Status: ' + stdoutdata)
|
||||
logging.info('[TV Out] Status: ' + stdoutdata)
|
||||
if systemVoltage < 9: logging.critical('[Voltage] Very low System Input Voltage! Bad power supply or no sun.')
|
||||
return
|
||||
|
||||
|
||||
def reconnectToInternet():
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
print('Server is maybe not online!')
|
||||
logging.info('Internet connection established')
|
||||
logging.error('Server maybe is not online!')
|
||||
global interntTmp
|
||||
interntTmp = 'true'
|
||||
#checkConnectionToFTPServer()
|
||||
return 'true'
|
||||
except Exception as e2:
|
||||
conn.close()
|
||||
print('no Internet connection')
|
||||
print('trying to reconnect to internet')
|
||||
logging.error('no Internet connection!')
|
||||
logging.info('trying to reconnect to internet')
|
||||
if enable_modem == 'true':
|
||||
print('[MODEM] no UMTS connection')
|
||||
logging.error('[MODEM] no UMTS connection')
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/modem3g/sakis3g status')
|
||||
print('[MODEM] Network Status: ' + stdoutdata)
|
||||
logging.info('[MODEM] Network Status: ' + stdoutdata)
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[MODEM] disconnected')
|
||||
sleep(20)
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[MODEM] reconnection successful attempt')
|
||||
logging.info('[MODEM] reconnection successful attempt')
|
||||
logging.info(stdoutdata)
|
||||
sleep(10)
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
logging.info('Internet connection established')
|
||||
return 'true'
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e5))
|
||||
logging.error(str(e5))
|
||||
return 'false'
|
||||
except Exception as e4:
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
return 'false'
|
||||
else:
|
||||
print('LAN or WLAN not connected!')
|
||||
logging.error('LAN or WLAN not connected!')
|
||||
return 'false'
|
||||
|
||||
def checkConnectionToFTPServer():
|
||||
try:
|
||||
# ping ftp url instead something else
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
try:
|
||||
global ftp
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('[FTP] New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
global internetTmp
|
||||
internetTmp = 'true'
|
||||
global upload_every
|
||||
upload_every = upload_only
|
||||
except Exception as ea:
|
||||
print('[FTP] no internet or ftp connection trying to reconnect later!')
|
||||
logging.error('[FTP] no internet or ftp connection trying to reconnect later!')
|
||||
print('[FTP] Server connection established')
|
||||
logging.info('[FTP] Server connection established')
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[FTP] Server connection not established')
|
||||
logging.info('[FTP] Server connection not established')
|
||||
reconnectToInternet()
|
||||
|
||||
return
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
# ioBoard
|
||||
piezo = 25
|
||||
bootSwitch = 17
|
||||
|
||||
|
||||
version = 'starting camera version 041'
|
||||
print('Copyright CopterSicht - info@coptersicht.de 2015-2017')
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
ioBoard = configParser.get('config', 'I/O_Board')
|
||||
mute = configParser.get('config', 'mute')
|
||||
wakeup_Time = str(configParser.get('config', 'wakeup_Time')).split(',')
|
||||
bed_Time = str(configParser.get('config', 'bed_Time')).split(',')
|
||||
tvOut = configParser.get('config', 'TV_Output')
|
||||
print('done!')
|
||||
|
||||
print('initializing logger...')
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
logging.info(version)
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:', str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
|
||||
sleep(15)
|
||||
|
||||
#camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
internetTmp = internet
|
||||
|
||||
|
||||
if ioBoard == "true":
|
||||
print('Running with the nice IO board!')
|
||||
spi = SpiDev()
|
||||
spi.open(0, 0)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setup(piezo, GPIO.OUT)
|
||||
GPIO.setup(bootSwitch, GPIO.IN)
|
||||
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera = PiCamera()
|
||||
camera.resolution = (3280, 2464) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.start_preview()
|
||||
# camera.start_preview(fullscreen=False, window = (100, 20, 960, 720))
|
||||
print('---starting sharpness tool---')
|
||||
print('--------normal output--------')
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
sleep(5)
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.stop_preview()
|
||||
print('---------zoom output---------')
|
||||
camera.crop = (0.5, 0.5, 0.3, 0.4)
|
||||
camera.start_preview()
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
else:
|
||||
camera.stop_preview()
|
||||
camera.close()
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
else:
|
||||
print('Running without the nice IO board!')
|
||||
|
||||
|
||||
if tvOut == 'false':
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/tvservice -o ')
|
||||
print('[TV Out] Status: ' + stdoutdata)
|
||||
logging.info('[TV Out] Status: ' + stdoutdata)
|
||||
|
||||
if internetTmp == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
if enable_modem == 'true': reconnectToInternet()
|
||||
try:
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
except Exception as ea:
|
||||
print('no internet or ftp connection on startup trying to reconnect later!')
|
||||
logging.error('no internet or ftp connection on startup trying to reconnect later!')
|
||||
internetTmp = 'false'
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
# camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
# camera.shutter_speed = 500000
|
||||
|
||||
|
||||
# camera.brightness = 50 #0 bis 100; default 50
|
||||
# camera.contrast = 0 #-100 bis 100; default 0
|
||||
# camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
# camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
# camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
|
||||
|
||||
# main programm (endless loop)
|
||||
while 1:
|
||||
time_next_unix = time.time() + interval # time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) # get time for picture_name
|
||||
time_name = time_name.replace(" ", "_")
|
||||
time_name = time_name.replace(":", "-")
|
||||
picture_name = camera_number + '_' + str(picture_number).zfill(6) + '_' + time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print("Time:", time_name)
|
||||
camera = PiCamera()
|
||||
camera.resolution = (3280, 2464) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.exposure_mode = 'auto'
|
||||
camera.crop = (0, 0, 0, 0)
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = 'interval capture'
|
||||
|
||||
if mute == "false": GPIO.output(piezo, GPIO.HIGH)
|
||||
print('Capturing image')
|
||||
logging.info('[Camera] Capturing image')
|
||||
camera.capture(picture_path) # capture image and save to picture_path (picture_directory + picture_name)
|
||||
camera.close()
|
||||
if mute == "false": GPIO.output(piezo, GPIO.LOW)
|
||||
|
||||
picture_number += 1 # increase picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print('Picture size:', os.path.getsize(picture_path), "Byte")
|
||||
logging.info('[Camera] Picture size: ' + str(os.path.getsize(picture_path)) + ' Byte')
|
||||
checkDiskSpace()
|
||||
if ioBoard == "true": checkSystemVoltage()
|
||||
if os.path.getsize(picture_path) < picture_size:
|
||||
print('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds # seconds since midnight
|
||||
if seconds1 > float(wakeup_Time[datetime.now().month - 1]) * 60 * 60 and seconds1 < float(bed_Time[
|
||||
datetime.now().month - 1]) * 60 * 60: # if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer", upload_every)
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
checkCpu()
|
||||
sleepToNextPicture()
|
||||
else:
|
||||
os.remove(picture_path) # deleting picture because it's night!
|
||||
picture_number -= 1 # decrease picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
# uploading log file
|
||||
print('skipping picture upload!')
|
||||
print('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internetTmp == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('[FTP] uploaded log-file successful!')
|
||||
logging.info('[FTP] uploaded log-file successful!')
|
||||
except Exception as e:
|
||||
print('[FTP] upload failed!')
|
||||
print(e)
|
||||
logging.error('[FTP] upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('[FTP] re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e8:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
try:
|
||||
print()
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('[FTP] uploaded log-file successful!')
|
||||
logging.info('[FTP] uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print('[FTP] re-connecting failed!')
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('[FTP] could not change ftp directory !')
|
||||
logging.error('[FTP] could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
# sleep until next morning
|
||||
print('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds # seconds since midnight
|
||||
if seconds < (
|
||||
float(wakeup_Time[
|
||||
datetime.now().month - 1]) * 60 * 60): # if time between 0am and wakeup_Time
|
||||
sleep_time = (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60) - seconds
|
||||
else: # if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400 - seconds + (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60)
|
||||
print('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
logging.info('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
sleep(sleep_time - 10)
|
||||
|
||||
# wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds) + ' < ' + str(
|
||||
(wakeup_Time[datetime.now().month - 1] * 60 * 60)))
|
||||
while (datetime.now() - midnight).seconds < (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for ' + str(sleep_time) + ' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for ' + str(sleep_time) + ' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
checkConnectionToFTPServer()
|
||||
|
||||
checkCpu()
|
||||
sleepToNextPicture()
|
||||
|
||||
|
||||
|
||||
595
Software/ZK_python/camera_v4_042.py
Normal file
595
Software/ZK_python/camera_v4_042.py
Normal file
@ -0,0 +1,595 @@
|
||||
# (c)2017-2018 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
# 042
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import http.client as httplib
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
from spidev import SpiDev
|
||||
import RPi.GPIO as GPIO
|
||||
import re
|
||||
import fcntl
|
||||
|
||||
# get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open("/sys/class/thermal/thermal_zone0/temp")
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp) / 1000
|
||||
|
||||
|
||||
# uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('[Upload] successful!')
|
||||
except Exception as e:
|
||||
print('upload failed!')
|
||||
print(e)
|
||||
logging.error('[Upload] failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP] trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] re-connecting successful!')
|
||||
logging.info('[FTP] re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP] upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except Exception as e9:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
return
|
||||
|
||||
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs('/media/usb0')
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize) / (st.f_blocks * st.f_frsize)) * 100)
|
||||
logging.info('[Disk] Free space: ' + str(freePercentaged) + ' %')
|
||||
print('Free disk space ', str(freePercentaged), ' %')
|
||||
if freePercentaged < 20 and freePercentaged > 10: logging.warning('[Disk] Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('[Disk] Storage Drive full, Backup Space in use!')
|
||||
return
|
||||
|
||||
|
||||
def checkCpu():
|
||||
try:
|
||||
cpuUsage = (os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip())
|
||||
cpuUsage = (float(cpuUsage.replace(',', '.'))) * 25
|
||||
print('[CPU] Usage: ', str("%.2f" % cpuUsage), ' %')
|
||||
logging.info('[CPU] Usage: ' + str("%.2f" % cpuUsage) + ' %')
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('[CPU] Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('[CPU] Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('[CPU] Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('[CPU] Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
else:
|
||||
logging.info('[CPU] Temperature: ' + str(getCpuTemperature()) + ' C')
|
||||
return
|
||||
except Exception as e2:
|
||||
print('[CPU] ' + str(e2))
|
||||
logging.warning('[CPU] ' + str(e2))
|
||||
|
||||
def readMPC(channel):
|
||||
adc = spi.xfer2([1, (8 + channel) << 4, 0])
|
||||
data = ((adc[1] & 3) << 8) + adc[2]
|
||||
return data
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix > 0:
|
||||
print("sleeping now", time_sleep_unix, "seconds" + '\n')
|
||||
logging.info('sleeping now ' + str(time_sleep_unix) + ' seconds' + '\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
def checkSystemVoltage():
|
||||
systemVoltage = (((readMPC(0) / 1023.0 * 3.3) * 6.62) * 0.95)
|
||||
print("System Input Voltage: %.2f" % systemVoltage, "Volt")
|
||||
logging.info('[Voltage] ' + str("%.2f" % systemVoltage) + ' Volt')
|
||||
if systemVoltage < 11.3 and systemVoltage > 9:
|
||||
logging.warning('[Voltage] Low System Input Voltage! Maybe bad power supply or no sun.')
|
||||
if tvOut == 'auto':
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/tvservice -o ')
|
||||
print('[TV Out] Status: ' + stdoutdata)
|
||||
logging.info('[TV Out] Status: ' + stdoutdata)
|
||||
if systemVoltage < 9: logging.critical('[Voltage] Very low System Input Voltage! Bad power supply or no sun.')
|
||||
return
|
||||
|
||||
|
||||
def reconnectToInternet():
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
print('Server is maybe not online!')
|
||||
logging.info('Internet connection established')
|
||||
logging.error('Server maybe is not online!')
|
||||
global interntTmp
|
||||
interntTmp = 'true'
|
||||
#checkConnectionToFTPServer()
|
||||
return 'true'
|
||||
except Exception as e2:
|
||||
conn.close()
|
||||
print('no Internet connection')
|
||||
print('trying to reconnect to internet')
|
||||
logging.error('no Internet connection!')
|
||||
logging.info('trying to reconnect to internet')
|
||||
if enable_modem == 'true':
|
||||
print('[MODEM] no UMTS connection')
|
||||
logging.error('[MODEM] no UMTS connection')
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/modem3g/sakis3g status')
|
||||
print('[MODEM] Network Status: ' + stdoutdata)
|
||||
logging.info('[MODEM] Network Status: ' + stdoutdata)
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[MODEM] disconnected')
|
||||
sleep(20)
|
||||
resetModem()
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[MODEM] reconnection successful attempt')
|
||||
logging.info('[MODEM] reconnection successful attempt')
|
||||
logging.info(stdoutdata)
|
||||
sleep(10)
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
logging.info('Internet connection established')
|
||||
return 'true'
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e5))
|
||||
logging.error(str(e5))
|
||||
return 'false'
|
||||
except Exception as e4:
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
return 'false'
|
||||
else:
|
||||
print('LAN or WLAN not connected!')
|
||||
logging.error('LAN or WLAN not connected!')
|
||||
return 'false'
|
||||
|
||||
def checkConnectionToFTPServer():
|
||||
try:
|
||||
# ping ftp url instead something else
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
try:
|
||||
global ftp
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('[FTP] New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
global internetTmp
|
||||
internetTmp = 'true'
|
||||
global upload_every
|
||||
upload_every = upload_only
|
||||
except Exception as ea:
|
||||
print('[FTP] no internet or ftp connection trying to reconnect later!')
|
||||
logging.error('[FTP] no internet or ftp connection trying to reconnect later!')
|
||||
print('[FTP] Server connection established')
|
||||
logging.info('[FTP] Server connection established')
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[FTP] Server connection not established')
|
||||
logging.info('[FTP] Server connection not established')
|
||||
reconnectToInternet()
|
||||
|
||||
return
|
||||
|
||||
def resetModem():
|
||||
try:
|
||||
lsusb_cmd = 'lsusb | grep {}'.format('Huawei')
|
||||
lsusb_out = subprocess.check_output(lsusb_cmd, shell=True)
|
||||
parts = re.search(r'Bus (?P<bus>\d+) Device (?P<dev>\d+): ID [:\d\w]+ (?P<desc>.*)$', str(lsusb_out))
|
||||
bus = parts.group('bus')
|
||||
dev = parts.group('dev')
|
||||
desc = parts.group('desc').strip()
|
||||
print('[Modem] Found device {} on bus {} for "{}"'.format(bus, dev, desc))
|
||||
f = open('/dev/bus/usb/{}/{}'.format(bus, dev), 'w', os.O_WRONLY)
|
||||
fcntl.ioctl(f, 21780, 0)
|
||||
sleep(15)
|
||||
print('[Modem] Reset successfully attempt')
|
||||
except Exception as ea:
|
||||
print('[Modem] Reset Error: ' + str(ea))
|
||||
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
# ioBoard
|
||||
piezo = 25
|
||||
bootSwitch = 17
|
||||
|
||||
version = 'starting camera version 042'
|
||||
print('Copyright CopterSicht - info@coptersicht.de 2015-' + str(datetime.now().year))
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
ioBoard = configParser.get('config', 'I/O_Board')
|
||||
mute = configParser.get('config', 'mute')
|
||||
wakeup_Time = str(configParser.get('config', 'wakeup_Time')).split(',')
|
||||
bed_Time = str(configParser.get('config', 'bed_Time')).split(',')
|
||||
tvOut = configParser.get('config', 'TV_Output')
|
||||
width = configParser.get('config', 'Width')
|
||||
height = configParser.get('config', 'Height')
|
||||
print('done!')
|
||||
|
||||
print('initializing logger...')
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
try:
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
except Exception as er:
|
||||
print('[Logger] Could not create .log' + str(er))
|
||||
print('[USB] Could not find USB storage!')
|
||||
while 1:
|
||||
checkConnectionToFTPServer()
|
||||
print('[Timlapse Core] Waiting for maintenance!')
|
||||
sleep(3600)
|
||||
logging.info(version)
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:', str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
|
||||
sleep(15)
|
||||
|
||||
#camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
internetTmp = internet
|
||||
|
||||
if ioBoard == "true":
|
||||
print('Running with the nice IO board!')
|
||||
spi = SpiDev()
|
||||
spi.open(0, 0)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setup(piezo, GPIO.OUT)
|
||||
GPIO.setup(bootSwitch, GPIO.IN)
|
||||
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera = PiCamera()
|
||||
camera.resolution = (3280, 2464) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.awb_mode = 'auto'
|
||||
camera.start_preview()
|
||||
# camera.start_preview(fullscreen=False, window = (100, 20, 960, 720))
|
||||
print('---starting sharpness tool---')
|
||||
print('--------normal output--------')
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
sleep(5)
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.stop_preview()
|
||||
print('---------zoom output---------')
|
||||
camera.crop = (0.5, 0.5, 0.3, 0.4)
|
||||
camera.start_preview()
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
else:
|
||||
camera.stop_preview()
|
||||
camera.close()
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
else:
|
||||
print('Running without the nice IO board!')
|
||||
|
||||
|
||||
if tvOut == 'false':
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/tvservice -o ')
|
||||
print('[TV Out] Status: ' + stdoutdata)
|
||||
logging.info('[TV Out] Status: ' + stdoutdata)
|
||||
|
||||
if internetTmp == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
if enable_modem == 'true': reconnectToInternet()
|
||||
try:
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
except Exception as ea:
|
||||
print('no internet or ftp connection on startup trying to reconnect later!')
|
||||
logging.error('no internet or ftp connection on startup trying to reconnect later!')
|
||||
internetTmp = 'false'
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
# camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
# camera.shutter_speed = 500000
|
||||
|
||||
|
||||
# camera.brightness = 50 #0 bis 100; default 50
|
||||
# camera.contrast = 0 #-100 bis 100; default 0
|
||||
# camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
# camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
# camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
print('[Camera] Resolution: ' + width + 'x' + height)
|
||||
logging.info('[Camera] Resolution: ' + width + 'x' + height)
|
||||
|
||||
|
||||
# main programm (endless loop)
|
||||
while 1:
|
||||
try:
|
||||
time_next_unix = time.time() + interval # time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) # get time for picture_name
|
||||
time_name = time_name.replace(" ", "_")
|
||||
time_name = time_name.replace(":", "-")
|
||||
picture_name = camera_number + '_' + str(picture_number).zfill(6) + '_' + time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print("Time:", time_name)
|
||||
camera = PiCamera()
|
||||
camera.resolution = (int(width), int(height)) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.exposure_mode = 'auto'
|
||||
camera.awb_mode = 'auto'
|
||||
camera.crop = (0, 0, 0, 0)
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) ' + str(datetime.now().year) + ' CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = 'interval capture'
|
||||
|
||||
if mute == "false": GPIO.output(piezo, GPIO.HIGH)
|
||||
print('Capturing image')
|
||||
logging.info('[Camera] Capturing image')
|
||||
camera.capture(picture_path) # capture image and save to picture_path (picture_directory + picture_name)
|
||||
camera.close()
|
||||
if mute == "false": GPIO.output(piezo, GPIO.LOW)
|
||||
picture_number += 1 # increase picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print('Picture size:', os.path.getsize(picture_path), "Byte")
|
||||
logging.info('[Camera] Picture size: ' + str(os.path.getsize(picture_path)) + ' Byte')
|
||||
checkDiskSpace()
|
||||
if ioBoard == "true": checkSystemVoltage()
|
||||
if os.path.getsize(picture_path) < picture_size:
|
||||
print('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds # seconds since midnight
|
||||
if seconds1 > float(wakeup_Time[datetime.now().month - 1]) * 60 * 60 and seconds1 < float(bed_Time[
|
||||
datetime.now().month - 1]) * 60 * 60: # if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer", upload_every)
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
checkCpu()
|
||||
sleepToNextPicture()
|
||||
else:
|
||||
os.remove(picture_path) # deleting picture because it's night!
|
||||
picture_number -= 1 # decrease picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
# uploading log file
|
||||
print('skipping picture upload!')
|
||||
print('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internetTmp == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('[FTP] uploaded log-file successful!')
|
||||
logging.info('[FTP] uploaded log-file successful!')
|
||||
except Exception as e:
|
||||
print('[FTP] upload failed!')
|
||||
print(e)
|
||||
logging.error('[FTP] upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('[FTP] re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e8:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
try:
|
||||
print()
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('[FTP] uploaded log-file successful!')
|
||||
logging.info('[FTP] uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print('[FTP] re-connecting failed!')
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('[FTP] could not change ftp directory !')
|
||||
logging.error('[FTP] could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
# sleep until next morning
|
||||
print('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds # seconds since midnight
|
||||
if seconds < (
|
||||
float(wakeup_Time[
|
||||
datetime.now().month - 1]) * 60 * 60): # if time between 0am and wakeup_Time
|
||||
sleep_time = (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60) - seconds
|
||||
else: # if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400 - seconds + (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60)
|
||||
print('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
logging.info('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
sleep(sleep_time - 10)
|
||||
|
||||
# wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds) + ' < ' + str(
|
||||
(wakeup_Time[datetime.now().month - 1] * 60 * 60)))
|
||||
while (datetime.now() - midnight).seconds < (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for ' + str(sleep_time) + ' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for ' + str(sleep_time) + ' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
else:
|
||||
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
checkConnectionToFTPServer()
|
||||
|
||||
checkCpu()
|
||||
sleepToNextPicture()
|
||||
|
||||
except Exception as loopError:
|
||||
print('[Timlapse Core] ' + str(loopError))
|
||||
try:
|
||||
logging.error('[FTP] could not change ftp directory !')
|
||||
except Exception as er:
|
||||
print('[Timlapse Core] Could not log: ' + str(er))
|
||||
|
||||
|
||||
611
Software/ZK_python/camera_v4_043.py
Normal file
611
Software/ZK_python/camera_v4_043.py
Normal file
@ -0,0 +1,611 @@
|
||||
# (c)2017-2018 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
# 043
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import http.client as httplib
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
from spidev import SpiDev
|
||||
import RPi.GPIO as GPIO
|
||||
import re
|
||||
import fcntl
|
||||
|
||||
# get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open("/sys/class/thermal/thermal_zone0/temp")
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp) / 1000
|
||||
|
||||
|
||||
# uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('[Upload] successful!')
|
||||
except Exception as e:
|
||||
print('upload failed!')
|
||||
print(e)
|
||||
logging.error('[Upload] failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP] trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] re-connecting successful!')
|
||||
logging.info('[FTP] re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP] upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except Exception as e9:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
return
|
||||
|
||||
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs('/media/usb0')
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize) / (st.f_blocks * st.f_frsize)) * 100)
|
||||
logging.info('[Disk] Free space: ' + str(freePercentaged) + ' %')
|
||||
print('Free disk space ', str(freePercentaged), ' %')
|
||||
if freePercentaged < 20 and freePercentaged > 10: logging.warning('[Disk] Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('[Disk] Storage Drive full, Backup Space in use!')
|
||||
return
|
||||
|
||||
|
||||
def checkCpu():
|
||||
try:
|
||||
cpuUsage = (os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip())
|
||||
cpuUsage = (float(cpuUsage.replace(',', '.'))) * 25
|
||||
print('[CPU] Usage: ', str("%.2f" % cpuUsage), ' %')
|
||||
logging.info('[CPU] Usage: ' + str("%.2f" % cpuUsage) + ' %')
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('[CPU] Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('[CPU] Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('[CPU] Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('[CPU] Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
else:
|
||||
logging.info('[CPU] Temperature: ' + str(getCpuTemperature()) + ' C')
|
||||
return
|
||||
except Exception as e2:
|
||||
print('[CPU] ' + str(e2))
|
||||
logging.warning('[CPU] ' + str(e2))
|
||||
|
||||
def readMPC(channel):
|
||||
adc = spi.xfer2([1, (8 + channel) << 4, 0])
|
||||
data = ((adc[1] & 3) << 8) + adc[2]
|
||||
return data
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix > 0:
|
||||
print("sleeping now", time_sleep_unix, "seconds" + '\n')
|
||||
logging.info('sleeping now ' + str(time_sleep_unix) + ' seconds' + '\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
def checkSystemVoltage():
|
||||
systemVoltage = (((readMPC(0) / 1023.0 * 3.3) * 6.62) * 0.95)
|
||||
print("System Input Voltage: %.2f" % systemVoltage, "Volt")
|
||||
logging.info('[Voltage] ' + str("%.2f" % systemVoltage) + ' Volt')
|
||||
if systemVoltage < 11.3 and systemVoltage > 9:
|
||||
logging.warning('[Voltage] Low System Input Voltage! Maybe bad power supply or no sun.')
|
||||
if tvOut == 'auto':
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/tvservice -o ')
|
||||
print('[TV Out] Status: ' + stdoutdata)
|
||||
logging.info('[TV Out] Status: ' + stdoutdata)
|
||||
if systemVoltage < 9: logging.critical('[Voltage] Very low System Input Voltage! Bad power supply or no sun.')
|
||||
return
|
||||
|
||||
|
||||
def reconnectToInternet():
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.coptersicht.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
print('Server is maybe not online!')
|
||||
logging.info('Internet connection established')
|
||||
logging.error('Server maybe is not online!')
|
||||
global interntTmp
|
||||
interntTmp = 'true'
|
||||
#checkConnectionToFTPServer()
|
||||
return 'true'
|
||||
except Exception as e2:
|
||||
conn.close()
|
||||
print('no Internet connection')
|
||||
print('trying to reconnect to internet')
|
||||
logging.error('no Internet connection!')
|
||||
logging.info('trying to reconnect to internet')
|
||||
if enable_modem == 'true':
|
||||
print('[MODEM] no UMTS connection')
|
||||
logging.error('[MODEM] no UMTS connection')
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/modem3g/sakis3g status')
|
||||
print('[MODEM] Network Status: ' + stdoutdata)
|
||||
logging.info('[MODEM] Network Status: ' + stdoutdata)
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[MODEM] disconnected')
|
||||
sleep(20)
|
||||
resetModem()
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[MODEM] reconnection successful attempt')
|
||||
logging.info('[MODEM] reconnection successful attempt')
|
||||
logging.info(stdoutdata)
|
||||
sleep(10)
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
logging.info('Internet connection established')
|
||||
return 'true'
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e5))
|
||||
logging.error(str(e5))
|
||||
return 'false'
|
||||
except Exception as e4:
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
return 'false'
|
||||
else:
|
||||
print('LAN or WLAN not connected!')
|
||||
logging.error('LAN or WLAN not connected!')
|
||||
return 'false'
|
||||
|
||||
def checkConnectionToFTPServer():
|
||||
try:
|
||||
# ping ftp url instead something else
|
||||
conn = httplib.HTTPConnection("www.coptersicht.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
try:
|
||||
global ftp
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('[FTP] New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
global internetTmp
|
||||
internetTmp = 'true'
|
||||
global upload_every
|
||||
upload_every = upload_only
|
||||
print('[FTP] Server connection established')
|
||||
logging.info('[FTP] Server connection established')
|
||||
except Exception as ea:
|
||||
internetTmp = 'false'
|
||||
print('[FTP] no internet or ftp connection trying to reconnect later!')
|
||||
logging.error('[FTP] no internet or ftp connection trying to reconnect later!')
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[FTP] Server connection not established')
|
||||
logging.info('[FTP] Server connection not established')
|
||||
reconnectToInternet()
|
||||
|
||||
return
|
||||
|
||||
def resetModem():
|
||||
try:
|
||||
lsusb_cmd = 'lsusb | grep {}'.format('Huawei')
|
||||
lsusb_out = subprocess.check_output(lsusb_cmd, shell=True)
|
||||
parts = re.search(r'Bus (?P<bus>\d+) Device (?P<dev>\d+): ID [:\d\w]+ (?P<desc>.*)$', str(lsusb_out))
|
||||
bus = parts.group('bus')
|
||||
dev = parts.group('dev')
|
||||
desc = parts.group('desc').strip()
|
||||
print('[Modem] Found device {} on bus {} for "{}"'.format(bus, dev, desc))
|
||||
f = open('/dev/bus/usb/{}/{}'.format(bus, dev), 'w', os.O_WRONLY)
|
||||
fcntl.ioctl(f, 21780, 0)
|
||||
sleep(15)
|
||||
print('[Modem] Reset successfully attempt')
|
||||
except Exception as ea:
|
||||
print('[Modem] Reset Error: ' + str(ea))
|
||||
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
# ioBoard
|
||||
piezo = 25
|
||||
bootSwitch = 17
|
||||
|
||||
version = 'starting camera version 043'
|
||||
print('Copyright CopterSicht - info@coptersicht.de 2015-' + str(datetime.now().year))
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
ioBoard = configParser.get('config', 'I/O_Board')
|
||||
mute = configParser.get('config', 'mute')
|
||||
wakeup_Time = str(configParser.get('config', 'wakeup_Time')).split(',')
|
||||
bed_Time = str(configParser.get('config', 'bed_Time')).split(',')
|
||||
tvOut = configParser.get('config', 'TV_Output')
|
||||
width = configParser.get('config', 'Width')
|
||||
height = configParser.get('config', 'Height')
|
||||
rebootCMD = configParser.get('config', 'Reboot')
|
||||
print('done!')
|
||||
|
||||
print('initializing logger...')
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
try:
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
except Exception as er:
|
||||
print('[Logger] Could not create .log' + str(er))
|
||||
print('[USB] Could not find USB storage!')
|
||||
while 1:
|
||||
checkConnectionToFTPServer()
|
||||
print('[Timlapse Core] Waiting for maintenance!')
|
||||
sleep(3600)
|
||||
logging.info(version)
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:', str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
internetTmp = internet
|
||||
rebootFlag = 'false'
|
||||
|
||||
if ioBoard == "true":
|
||||
print('Running with the nice IO board!')
|
||||
spi = SpiDev()
|
||||
spi.open(0, 0)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setup(piezo, GPIO.OUT)
|
||||
GPIO.setup(bootSwitch, GPIO.IN)
|
||||
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera = PiCamera()
|
||||
camera.resolution = (3280, 2464) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.awb_mode = 'auto'
|
||||
camera.start_preview()
|
||||
# camera.start_preview(fullscreen=False, window = (100, 20, 960, 720))
|
||||
print('---starting sharpness tool---')
|
||||
print('--------normal output--------')
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
sleep(5)
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.stop_preview()
|
||||
print('---------zoom output---------')
|
||||
camera.crop = (0.5, 0.5, 0.3, 0.4)
|
||||
camera.start_preview()
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
else:
|
||||
camera.stop_preview()
|
||||
camera.close()
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
else:
|
||||
print('Running without the nice IO board!')
|
||||
|
||||
if rebootCMD == 'true':
|
||||
print('Automatic reboot is allowed while wake up!')
|
||||
logging.info('Automatic reboot is allowed while wakeup!')
|
||||
|
||||
if tvOut == 'false':
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/tvservice -o ')
|
||||
print('[TV Out] Status: ' + stdoutdata)
|
||||
logging.info('[TV Out] Status: ' + stdoutdata)
|
||||
|
||||
if internetTmp == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
if enable_modem == 'true': reconnectToInternet()
|
||||
try:
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
except Exception as ea:
|
||||
print('no internet or ftp connection on startup trying to reconnect later!')
|
||||
logging.error('no internet or ftp connection on startup trying to reconnect later!')
|
||||
internetTmp = 'false'
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
# camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
# camera.shutter_speed = 500000
|
||||
|
||||
|
||||
# camera.brightness = 50 #0 bis 100; default 50
|
||||
# camera.contrast = 0 #-100 bis 100; default 0
|
||||
# camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
# camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
# camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
print('[Camera] Resolution: ' + width + 'x' + height)
|
||||
logging.info('[Camera] Resolution: ' + width + 'x' + height)
|
||||
|
||||
|
||||
# main programm (endless loop)
|
||||
while 1:
|
||||
try:
|
||||
time_next_unix = time.time() + interval # time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) # get time for picture_name
|
||||
time_name = time_name.replace(" ", "_")
|
||||
time_name = time_name.replace(":", "-")
|
||||
picture_name = camera_number + '_' + str(picture_number).zfill(6) + '_' + time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print("Time:", time_name)
|
||||
#camera = PiCamera()
|
||||
camera.resolution = (int(width), int(height)) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.exposure_mode = 'auto'
|
||||
camera.awb_mode = 'auto'
|
||||
camera.crop = (0, 0, 0, 0)
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) ' + str(datetime.now().year) + ' CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = 'interval capture'
|
||||
|
||||
if mute == "false": GPIO.output(piezo, GPIO.HIGH)
|
||||
print('Capturing image')
|
||||
logging.info('[Camera] Capturing image')
|
||||
camera.capture(picture_path) # capture image and save to picture_path (picture_directory + picture_name)
|
||||
#camera.close()
|
||||
if mute == "false": GPIO.output(piezo, GPIO.LOW)
|
||||
picture_number += 1 # increase picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print('Picture size:', os.path.getsize(picture_path), "Byte")
|
||||
logging.info('[Camera] Picture size: ' + str(os.path.getsize(picture_path)) + ' Byte')
|
||||
checkDiskSpace()
|
||||
if ioBoard == "true": checkSystemVoltage()
|
||||
if os.path.getsize(picture_path) < picture_size:
|
||||
print('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds # seconds since midnight
|
||||
if seconds1 > float(wakeup_Time[datetime.now().month - 1]) * 60 * 60 and seconds1 < float(bed_Time[
|
||||
datetime.now().month - 1]) * 60 * 60: # if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer", upload_every)
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
checkCpu()
|
||||
sleepToNextPicture()
|
||||
else:
|
||||
os.remove(picture_path) # deleting picture because it's night!
|
||||
picture_number -= 1 # decrease picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
# uploading log file
|
||||
print('skipping picture upload!')
|
||||
print('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internetTmp == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('[FTP] uploaded log-file successful!')
|
||||
logging.info('[FTP] uploaded log-file successful!')
|
||||
except Exception as e:
|
||||
print('[FTP] upload failed!')
|
||||
print(e)
|
||||
logging.error('[FTP] upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('[FTP] re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e8:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
try:
|
||||
print()
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('[FTP] uploaded log-file successful!')
|
||||
logging.info('[FTP] uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print('[FTP] re-connecting failed!')
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('[FTP] could not change ftp directory !')
|
||||
logging.error('[FTP] could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
checkConnectionToFTPServer()
|
||||
if internetTmp == 'false':
|
||||
rebootFlag = 'true'
|
||||
# sleep until next morning
|
||||
print('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds # seconds since midnight
|
||||
if seconds < (
|
||||
float(wakeup_Time[
|
||||
datetime.now().month - 1]) * 60 * 60): # if time between 0am and wakeup_Time
|
||||
sleep_time = (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60) - seconds
|
||||
else: # if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400 - seconds + (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60)
|
||||
print('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
logging.info('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
sleep(sleep_time - 10)
|
||||
|
||||
# wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds) + ' < ' + str(
|
||||
(wakeup_Time[datetime.now().month - 1] * 60 * 60)))
|
||||
while (datetime.now() - midnight).seconds < (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for ' + str(sleep_time) + ' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('slept for ' + str(sleep_time) + ' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
if rebootFlag == 'true':
|
||||
rebootFlag = 'false'
|
||||
stdoutdata = subprocess.getoutput('shutdown -r now')
|
||||
print('[Timelapse Core] Reboot: ' + stdoutdata)
|
||||
logging.info('[Timelapse Core] Reboot: ' + stdoutdata)
|
||||
sleep(70)
|
||||
else:
|
||||
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
checkConnectionToFTPServer()
|
||||
|
||||
checkCpu()
|
||||
sleepToNextPicture()
|
||||
|
||||
except Exception as loopError:
|
||||
print('[Timlapse Core] ' + str(loopError))
|
||||
try:
|
||||
logging.error('[FTP] could not change ftp directory !')
|
||||
except Exception as er:
|
||||
print('[Timlapse Core] Could not log: ' + str(er))
|
||||
|
||||
|
||||
653
Software/ZK_python/camera_v4_044.py
Normal file
653
Software/ZK_python/camera_v4_044.py
Normal file
@ -0,0 +1,653 @@
|
||||
# (c)2017-2018 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
# 044
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import http.client as httplib
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
from spidev import SpiDev
|
||||
import RPi.GPIO as GPIO
|
||||
import re
|
||||
import fcntl
|
||||
|
||||
# get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open("/sys/class/thermal/thermal_zone0/temp")
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp) / 1000
|
||||
|
||||
|
||||
# uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('[Upload] successful!')
|
||||
except Exception as e:
|
||||
print('upload failed!')
|
||||
print(e)
|
||||
logging.error('[Upload] failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP] trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] re-connecting successful!')
|
||||
logging.info('[FTP] re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP] upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except Exception as e9:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
return
|
||||
|
||||
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs('/media/usb0')
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize) / (st.f_blocks * st.f_frsize)) * 100)
|
||||
logging.info('[Disk] Free space: ' + str(freePercentaged) + ' %')
|
||||
print('Free disk space ', str(freePercentaged), ' %')
|
||||
if freePercentaged < 20 and freePercentaged > 10: logging.warning('[Disk] Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('[Disk] Storage Drive full, Backup Space in use!')
|
||||
return
|
||||
|
||||
|
||||
def checkCpu():
|
||||
try:
|
||||
cpuUsage = (os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip())
|
||||
cpuUsage = (float(cpuUsage.replace(',', '.'))) * 25
|
||||
cpuUsageStr = str(cpuUsage) + ' %'
|
||||
print('[CPU] Usage: ', cpuUsageStr)
|
||||
logging.info('[CPU] Usage: ' + str("%.2f" % cpuUsage) + ' %')
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('[CPU] Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 5.0:
|
||||
logging.warning('[CPU] Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('[CPU] Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('[CPU] Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
else:
|
||||
logging.info('[CPU] Temperature: ' + str(getCpuTemperature()) + ' C')
|
||||
return
|
||||
except Exception as e2:
|
||||
print('[CPU] ' + str(e2))
|
||||
logging.warning('[CPU] ' + str(e2))
|
||||
|
||||
def readMPC(channel):
|
||||
adc = spi.xfer2([1, (8 + channel) << 4, 0])
|
||||
data = ((adc[1] & 3) << 8) + adc[2]
|
||||
return data
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix > 0:
|
||||
print("sleeping now", time_sleep_unix, "seconds" + '\n')
|
||||
logging.info('sleeping now ' + str(time_sleep_unix) + ' seconds' + '\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
def checkSystemVoltage():
|
||||
systemVoltage = (((readMPC(0) / 1023.0 * 3.3) * 6.62) * 0.95)
|
||||
print("System Input Voltage: %.2f" % systemVoltage, "Volt")
|
||||
logging.info('[Voltage] ' + str("%.2f" % systemVoltage) + ' Volt')
|
||||
if systemVoltage < 11.3 and systemVoltage > 9:
|
||||
logging.warning('[Voltage] Low System Input Voltage! Maybe bad power supply or no sun.')
|
||||
if tvOut == 'auto':
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/tvservice -o ')
|
||||
print('[TV Out] Status: ' + stdoutdata)
|
||||
logging.info('[TV Out] Status: ' + stdoutdata)
|
||||
if systemVoltage < 9: logging.critical('[Voltage] Very low System Input Voltage! Bad power supply or no sun.')
|
||||
return
|
||||
|
||||
|
||||
def reconnectToInternet():
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.coptersicht.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
print('Server is maybe not online!')
|
||||
logging.info('Internet connection established')
|
||||
logging.error('Server maybe is not online!')
|
||||
global interntTmp
|
||||
interntTmp = 'true'
|
||||
#checkConnectionToFTPServer()
|
||||
return 'true'
|
||||
except Exception as e2:
|
||||
conn.close()
|
||||
print('no Internet connection')
|
||||
print('trying to reconnect to internet')
|
||||
logging.error('no Internet connection!')
|
||||
logging.info('trying to reconnect to internet')
|
||||
if enable_modem == 'true':
|
||||
print('[MODEM] no UMTS connection')
|
||||
logging.error('[MODEM] no UMTS connection')
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/modem3g/sakis3g status')
|
||||
print('[MODEM] Network Status: ' + stdoutdata)
|
||||
logging.info('[MODEM] Network Status: ' + stdoutdata)
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[MODEM] disconnected')
|
||||
sleep(20)
|
||||
resetModem()
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[MODEM] reconnection successful attempt')
|
||||
logging.info('[MODEM] reconnection successful attempt')
|
||||
logging.info(stdoutdata)
|
||||
sleep(10)
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
logging.info('Internet connection established')
|
||||
return 'true'
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e5))
|
||||
logging.error(str(e5))
|
||||
return 'false'
|
||||
except Exception as e4:
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
return 'false'
|
||||
else:
|
||||
print('LAN or WLAN not connected!')
|
||||
logging.error('LAN or WLAN not connected!')
|
||||
return 'false'
|
||||
|
||||
def checkConnectionToFTPServer():
|
||||
try:
|
||||
# ping ftp url instead something else
|
||||
conn = httplib.HTTPConnection("www.coptersicht.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
try:
|
||||
global ftp
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('[FTP] New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
global internetTmp
|
||||
internetTmp = 'true'
|
||||
global upload_every
|
||||
upload_every = upload_only
|
||||
print('[FTP] Server connection established')
|
||||
logging.info('[FTP] Server connection established')
|
||||
except Exception as ea:
|
||||
internetTmp = 'false'
|
||||
print('[FTP] no internet or ftp connection trying to reconnect later!')
|
||||
logging.error('[FTP] no internet or ftp connection trying to reconnect later!')
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[FTP] Server connection not established')
|
||||
logging.info('[FTP] Server connection not established')
|
||||
reconnectToInternet()
|
||||
|
||||
return
|
||||
|
||||
def resetModem():
|
||||
try:
|
||||
lsusb_cmd = 'lsusb | grep {}'.format('Huawei')
|
||||
lsusb_out = subprocess.check_output(lsusb_cmd, shell=True)
|
||||
parts = re.search(r'Bus (?P<bus>\d+) Device (?P<dev>\d+): ID [:\d\w]+ (?P<desc>.*)$', str(lsusb_out))
|
||||
bus = parts.group('bus')
|
||||
dev = parts.group('dev')
|
||||
desc = parts.group('desc').strip()
|
||||
print('[Modem] Found device {} on bus {} for "{}"'.format(bus, dev, desc))
|
||||
f = open('/dev/bus/usb/{}/{}'.format(bus, dev), 'w', os.O_WRONLY)
|
||||
fcntl.ioctl(f, 21780, 0)
|
||||
sleep(15)
|
||||
print('[Modem] Reset successfully attempt')
|
||||
except Exception as ea:
|
||||
print('[Modem] Reset Error: ' + str(ea))
|
||||
|
||||
def rebootSystem():
|
||||
rebootFlag = 'false'
|
||||
os.system('sudo shutdown -r now')
|
||||
print('[Timelapse Core] Reboot')
|
||||
logging.info('[Timelapse Core] Reboot')
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(2)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(2)
|
||||
sleep(70)
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
# ioBoard
|
||||
piezo = 25
|
||||
bootSwitch = 17
|
||||
|
||||
version = 'starting camera version 044'
|
||||
print('Copyright CopterSicht - info@coptersicht.de 2015-' + str(datetime.now().year))
|
||||
print(version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
ioBoard = configParser.get('config', 'I/O_Board')
|
||||
mute = configParser.get('config', 'mute')
|
||||
wakeup_Time = str(configParser.get('config', 'wakeup_Time')).split(',')
|
||||
bed_Time = str(configParser.get('config', 'bed_Time')).split(',')
|
||||
tvOut = configParser.get('config', 'TV_Output')
|
||||
width = configParser.get('config', 'Width')
|
||||
height = configParser.get('config', 'Height')
|
||||
rebootCMD = configParser.get('config', 'Reboot')
|
||||
|
||||
print('done!')
|
||||
|
||||
print('initializing logger...')
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
try:
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
except Exception as er:
|
||||
print('[Logger] Could not create .log' + str(er))
|
||||
print('[USB] Could not find USB storage!')
|
||||
while 1:
|
||||
checkConnectionToFTPServer()
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
if internetTmp == 'false':
|
||||
rebootFlag = 'true'
|
||||
print('[Timelapse Core] Reboot Flag set')
|
||||
if rebootFlag == 'true':
|
||||
rebootSystem()
|
||||
print('[Timlapse Core] Waiting for maintenance!')
|
||||
sleep(3600)
|
||||
logging.info(version)
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:', str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
internetTmp = internet
|
||||
rebootFlag = 'false'
|
||||
|
||||
if ioBoard == "true":
|
||||
print('Running with the nice IO board!')
|
||||
spi = SpiDev()
|
||||
spi.open(0, 0)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setup(piezo, GPIO.OUT)
|
||||
GPIO.setup(bootSwitch, GPIO.IN)
|
||||
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera = PiCamera()
|
||||
camera.resolution = (3280, 2464) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.awb_mode = 'auto'
|
||||
camera.start_preview()
|
||||
# camera.start_preview(fullscreen=False, window = (100, 20, 960, 720))
|
||||
print('---starting sharpness tool---')
|
||||
print('--------normal output--------')
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
sleep(5)
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.stop_preview()
|
||||
print('---------zoom output---------')
|
||||
camera.crop = (0.5, 0.5, 0.3, 0.4)
|
||||
camera.start_preview()
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
else:
|
||||
camera.stop_preview()
|
||||
camera.close()
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
else:
|
||||
print('Running without the nice IO board!')
|
||||
|
||||
if rebootCMD == 'true':
|
||||
print('Automatic reboot is allowed while wake up!')
|
||||
logging.info('Automatic reboot is allowed while wakeup!')
|
||||
|
||||
if tvOut == 'false':
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/tvservice -o ')
|
||||
print('[TV Out] Status: ' + stdoutdata)
|
||||
logging.info('[TV Out] Status: ' + stdoutdata)
|
||||
|
||||
if internetTmp == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
if enable_modem == 'true': reconnectToInternet()
|
||||
try:
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
except Exception as ea:
|
||||
print('no internet or ftp connection on startup trying to reconnect later!')
|
||||
logging.error('no internet or ftp connection on startup trying to reconnect later!')
|
||||
internetTmp = 'false'
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
# camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
# camera.shutter_speed = 500000
|
||||
|
||||
|
||||
# camera.brightness = 50 #0 bis 100; default 50
|
||||
# camera.contrast = 0 #-100 bis 100; default 0
|
||||
# camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
# camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
# camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
print('[Camera] Resolution: ' + width + 'x' + height)
|
||||
logging.info('[Camera] Resolution: ' + width + 'x' + height)
|
||||
|
||||
|
||||
# main programm (endless loop)
|
||||
while 1:
|
||||
try:
|
||||
time_next_unix = time.time() + interval # time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) # get time for picture_name
|
||||
time_name = time_name.replace(" ", "_")
|
||||
time_name = time_name.replace(":", "-")
|
||||
picture_name = camera_number + '_' + str(picture_number).zfill(6) + '_' + time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print("Time:", time_name)
|
||||
#camera = PiCamera()
|
||||
camera.resolution = (int(width), int(height)) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.exposure_mode = 'auto'
|
||||
camera.awb_mode = 'auto'
|
||||
camera.crop = (0, 0, 0, 0)
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) ' + str(datetime.now().year) + ' CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = 'interval capture'
|
||||
|
||||
if mute == "false": GPIO.output(piezo, GPIO.HIGH)
|
||||
print('Capturing image')
|
||||
logging.info('[Camera] Capturing image')
|
||||
camera.capture(picture_path) # capture image and save to picture_path (picture_directory + picture_name)
|
||||
#camera.close()
|
||||
if mute == "false": GPIO.output(piezo, GPIO.LOW)
|
||||
picture_number += 1 # increase picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print('Picture size:', os.path.getsize(picture_path), "Byte")
|
||||
logging.info('[Camera] Picture size: ' + str(os.path.getsize(picture_path)) + ' Byte')
|
||||
checkDiskSpace()
|
||||
if ioBoard == "true": checkSystemVoltage()
|
||||
if os.path.getsize(picture_path) < picture_size:
|
||||
print('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds # seconds since midnight
|
||||
if seconds1 > float(wakeup_Time[datetime.now().month - 1]) * 60 * 60 and seconds1 < float(bed_Time[
|
||||
datetime.now().month - 1]) * 60 * 60: # if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer", upload_every)
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
#checkCpu()
|
||||
#sleepToNextPicture()
|
||||
else:
|
||||
os.remove(picture_path) # deleting picture because it's night!
|
||||
picture_number -= 1 # decrease picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
# uploading log file
|
||||
print('skipping picture upload!')
|
||||
print('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internetTmp == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('[FTP] uploaded log-file successful!')
|
||||
logging.info('[FTP] uploaded log-file successful!')
|
||||
except Exception as e:
|
||||
print('[FTP] upload failed!')
|
||||
print(e)
|
||||
logging.error('[FTP] upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('[FTP] re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e8:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
try:
|
||||
print()
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('[FTP] uploaded log-file successful!')
|
||||
logging.info('[FTP] uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print('[FTP] re-connecting failed!')
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('[FTP] could not change ftp directory !')
|
||||
logging.error('[FTP] could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
print('[Timelapse Core] Checking again Internet Connection before sleep')
|
||||
logging.info('[Timelapse Core] Checking again Internet Connection before sleep')
|
||||
checkConnectionToFTPServer()
|
||||
if internetTmp == 'false':
|
||||
rebootFlag = 'true'
|
||||
print('[Timelapse Core] Reboot Flag set')
|
||||
logging.info('[Timelapse Core] Reboot Flag set')
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(0.5)
|
||||
|
||||
# sleep until next morning
|
||||
print('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds # seconds since midnight
|
||||
if seconds < (
|
||||
float(wakeup_Time[
|
||||
datetime.now().month - 1]) * 60 * 60): # if time between 0am and wakeup_Time
|
||||
sleep_time = (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60) - seconds
|
||||
else: # if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400 - seconds + (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60)
|
||||
print('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
logging.info('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
sleep(sleep_time - 10)
|
||||
|
||||
# wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds) + ' < ' + str(
|
||||
(wakeup_Time[datetime.now().month - 1] * 60 * 60)))
|
||||
while (datetime.now() - midnight).seconds < (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for ' + str(sleep_time) + ' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info(version)
|
||||
logging.info('slept for ' + str(sleep_time) + ' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
if rebootFlag == 'true':
|
||||
rebootSystem()
|
||||
else:
|
||||
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
checkConnectionToFTPServer()
|
||||
|
||||
checkCpu()
|
||||
sleepToNextPicture()
|
||||
|
||||
except Exception as loopError:
|
||||
print('[Timlapse Core] ' + str(loopError))
|
||||
try:
|
||||
logging.error('[FTP] could not change ftp directory !')
|
||||
except Exception as er:
|
||||
print('[Timlapse Core] Could not log: ' + str(er))
|
||||
|
||||
|
||||
650
Software/ZK_python/camera_v4_045.py
Normal file
650
Software/ZK_python/camera_v4_045.py
Normal file
@ -0,0 +1,650 @@
|
||||
# (c)2017-2018 Jannik Seiler & Hendrik Schutter
|
||||
|
||||
# 045
|
||||
|
||||
from picamera import PiCamera
|
||||
from time import sleep
|
||||
from ftplib import FTP
|
||||
from fractions import Fraction
|
||||
from datetime import datetime
|
||||
import ftplib
|
||||
import time
|
||||
import os
|
||||
import http.client as httplib
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import configparser
|
||||
import subprocess
|
||||
from spidev import SpiDev
|
||||
import RPi.GPIO as GPIO
|
||||
import re
|
||||
import fcntl
|
||||
|
||||
# get cpu temperature
|
||||
def getCpuTemperature():
|
||||
tempFile = open("/sys/class/thermal/thermal_zone0/temp")
|
||||
cpu_temp = tempFile.read()
|
||||
tempFile.close()
|
||||
return float(cpu_temp) / 1000
|
||||
|
||||
|
||||
# uploading picture to ftp and make chmod 754 when done sleep until sleeping time is over
|
||||
def uploadToFTP():
|
||||
print('trying to upload file ...')
|
||||
logging.info('trying to upload file ...')
|
||||
try:
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('upload successful!')
|
||||
logging.info('[Upload] successful!')
|
||||
except Exception as e:
|
||||
print('upload failed!')
|
||||
print(e)
|
||||
logging.error('[Upload] failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP] trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] re-connecting successful!')
|
||||
logging.info('[FTP] re-connecting successful!')
|
||||
ftp.storbinary('STOR ' + picture_name, open(picture_path, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + picture_name)
|
||||
os.remove(picture_path)
|
||||
print('[FTP] upload successful!')
|
||||
logging.info('[FTP]upload successful!')
|
||||
except Exception as e9:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e9)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e9))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
return
|
||||
|
||||
|
||||
def checkDiskSpace():
|
||||
st = os.statvfs(picture_directory)
|
||||
free = st.f_bavail * st.f_frsize
|
||||
total = st.f_blocks * st.f_frsize
|
||||
|
||||
freePercentaged = int(((st.f_bavail * st.f_frsize) / (st.f_blocks * st.f_frsize)) * 100)
|
||||
logging.info('[Disk] Free space: ' + str(freePercentaged) + ' %')
|
||||
print('Free disk space ', str(freePercentaged), ' %')
|
||||
if freePercentaged < 20 and freePercentaged > 10: logging.warning('[Disk] Storage Drive nearly full!')
|
||||
if freePercentaged < 10: logging.critical('[Disk] Storage Drive full, Backup Space in use!')
|
||||
return
|
||||
|
||||
|
||||
def checkCpu():
|
||||
try:
|
||||
if getCpuTemperature() <= -5.0:
|
||||
logging.critical('[CPU] Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() <= 7.0:
|
||||
logging.warning('[CPU] Coold: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 45.0:
|
||||
logging.warning('[CPU] Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
elif getCpuTemperature() >= 50.0:
|
||||
logging.critical('[CPU] Hot: ' + str(getCpuTemperature()) + ' C')
|
||||
else:
|
||||
logging.info('[CPU] Temperature: ' + str(getCpuTemperature()) + ' C')
|
||||
return
|
||||
except Exception as e2:
|
||||
print('[CPU] ' + str(e2))
|
||||
logging.warning('[CPU] ' + str(e2))
|
||||
|
||||
def readMPC(channel):
|
||||
adc = spi.xfer2([1, (8 + channel) << 4, 0])
|
||||
data = ((adc[1] & 3) << 8) + adc[2]
|
||||
return data
|
||||
|
||||
def sleepToNextPicture():
|
||||
time_sleep_unix = time_next_unix - time.time()
|
||||
if time_sleep_unix > 0:
|
||||
print("sleeping now", time_sleep_unix, "seconds" + '\n')
|
||||
logging.info('sleeping now ' + str(time_sleep_unix) + ' seconds' + '\n')
|
||||
sleep(time_sleep_unix)
|
||||
else:
|
||||
print('no sleep needed, behind schedule!')
|
||||
logging.warning('no sleep needed, behind schedule!')
|
||||
return
|
||||
|
||||
def checkSystemVoltage():
|
||||
systemVoltage = (((readMPC(0) / 1023.0 * 3.3) * 6.62) * 0.95)
|
||||
print("System Input Voltage: %.2f" % systemVoltage, "Volt")
|
||||
logging.info('[Voltage] ' + str("%.2f" % systemVoltage) + ' Volt')
|
||||
if systemVoltage < 11.2 and systemVoltage > 9:
|
||||
logging.warning('[Voltage] Low System Input Voltage! Maybe bad power supply or no sun.')
|
||||
if tvOut == 'auto':
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/tvservice -o ')
|
||||
print('[TV Out] Status: ' + stdoutdata)
|
||||
logging.info('[TV Out] Status: ' + stdoutdata)
|
||||
if systemVoltage < 9: logging.critical('[Voltage] Very low System Input Voltage! Bad power supply or no sun.')
|
||||
return
|
||||
|
||||
|
||||
def reconnectToInternet():
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.coptersicht.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
print('Server is maybe not online!')
|
||||
logging.info('Internet connection established')
|
||||
logging.error('Server maybe is not online!')
|
||||
global interntTmp
|
||||
interntTmp = 'true'
|
||||
#checkConnectionToFTPServer()
|
||||
return 'true'
|
||||
except Exception as e2:
|
||||
conn.close()
|
||||
print('no Internet connection')
|
||||
print('trying to reconnect to internet')
|
||||
logging.error('no Internet connection!')
|
||||
logging.info('trying to reconnect to internet')
|
||||
if enable_modem == 'true':
|
||||
print('[MODEM] no UMTS connection')
|
||||
logging.error('[MODEM] no UMTS connection')
|
||||
try:
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/modem3g/sakis3g status')
|
||||
print('[MODEM] Network Status: ' + stdoutdata)
|
||||
logging.info('[MODEM] Network Status: ' + stdoutdata)
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g disconnect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
logging.info(stdoutdata)
|
||||
logging.info('[MODEM] disconnected')
|
||||
sleep(20)
|
||||
resetModem()
|
||||
stdoutdata = subprocess.getoutput(
|
||||
"sudo /usr/bin/modem3g/sakis3g connect --console APN='" + modem_apn + "' USBINTERFACE='0' USBMODEM='12d1:1436'")
|
||||
print(stdoutdata)
|
||||
print('[MODEM] reconnection successful attempt')
|
||||
logging.info('[MODEM] reconnection successful attempt')
|
||||
logging.info(stdoutdata)
|
||||
sleep(10)
|
||||
try:
|
||||
conn = httplib.HTTPConnection("www.google.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
print('Internet connection established')
|
||||
logging.info('Internet connection established')
|
||||
return 'true'
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e5))
|
||||
logging.error(str(e5))
|
||||
return 'false'
|
||||
except Exception as e4:
|
||||
print('[MODEM] UMTS connection failed')
|
||||
logging.error('[MODEM] UMTS connection failed')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
return 'false'
|
||||
else:
|
||||
print('LAN or WLAN not connected!')
|
||||
logging.error('LAN or WLAN not connected!')
|
||||
return 'false'
|
||||
|
||||
def checkConnectionToFTPServer():
|
||||
try:
|
||||
# ping ftp url instead something else
|
||||
conn = httplib.HTTPConnection("www.coptersicht.de", timeout=5)
|
||||
conn.request("HEAD", "/")
|
||||
conn.close()
|
||||
try:
|
||||
global ftp
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('[FTP] New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('[FTP] New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
global internetTmp
|
||||
internetTmp = 'true'
|
||||
global upload_every
|
||||
upload_every = upload_only
|
||||
print('[FTP] Server connection established')
|
||||
logging.info('[FTP] Server connection established')
|
||||
except Exception as ea:
|
||||
internetTmp = 'false'
|
||||
print('[FTP] no internet or ftp connection trying to reconnect later!')
|
||||
logging.error('[FTP] no internet or ftp connection trying to reconnect later!')
|
||||
except Exception as e5:
|
||||
conn.close()
|
||||
print('[FTP] Server connection not established')
|
||||
logging.info('[FTP] Server connection not established')
|
||||
reconnectToInternet()
|
||||
|
||||
return
|
||||
|
||||
def resetModem():
|
||||
try:
|
||||
lsusb_cmd = 'lsusb | grep {}'.format('Huawei')
|
||||
lsusb_out = subprocess.check_output(lsusb_cmd, shell=True)
|
||||
parts = re.search(r'Bus (?P<bus>\d+) Device (?P<dev>\d+): ID [:\d\w]+ (?P<desc>.*)$', str(lsusb_out))
|
||||
bus = parts.group('bus')
|
||||
dev = parts.group('dev')
|
||||
desc = parts.group('desc').strip()
|
||||
print('[Modem] Found device {} on bus {} for "{}"'.format(bus, dev, desc))
|
||||
f = open('/dev/bus/usb/{}/{}'.format(bus, dev), 'w', os.O_WRONLY)
|
||||
fcntl.ioctl(f, 21780, 0)
|
||||
sleep(15)
|
||||
print('[Modem] Reset successfully attempt')
|
||||
except Exception as ea:
|
||||
print('[Modem] Reset Error: ' + str(ea))
|
||||
|
||||
def rebootSystem():
|
||||
rebootFlag = 'false'
|
||||
os.system('sudo shutdown -r now')
|
||||
print('[Timelapse Core] Reboot')
|
||||
logging.info('[Timelapse Core] Reboot')
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(2)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(2)
|
||||
sleep(70)
|
||||
|
||||
configParser = configparser.RawConfigParser()
|
||||
configFilePath = r'/home/pi/config.txt'
|
||||
configParser.read(configFilePath)
|
||||
|
||||
# ioBoard
|
||||
piezo = 25
|
||||
bootSwitch = 17
|
||||
|
||||
version = '045'
|
||||
print('Copyright CopterSicht - info@coptersicht.de 2015-' + str(datetime.now().year))
|
||||
print('starting camera version: ' + version)
|
||||
print('reading config...')
|
||||
ftp_url = configParser.get('config', 'ftp_url')
|
||||
ftp_picture_directory = configParser.get('config', 'ftp_picture_directory')
|
||||
ftp_log_directory = configParser.get('config', 'ftp_log_directory')
|
||||
ftp_user = configParser.get('config', 'ftp_user')
|
||||
ftp_pw = configParser.get('config', 'ftp_pw')
|
||||
picture_directory = configParser.get('config', 'picture_directory')
|
||||
log_directory = configParser.get('config', 'log_directory')
|
||||
camera_number = configParser.get('config', 'camera_number')
|
||||
enable_modem = configParser.get('config', 'enable_modem')
|
||||
picture_size = int(configParser.get('config', 'picture_size'))
|
||||
internet = configParser.get('config', 'internet')
|
||||
modem_apn = configParser.get('config', 'modem_apn')
|
||||
upload_only = int(configParser.get('config', 'upload_only'))
|
||||
interval = int(configParser.get('config', 'interval'))
|
||||
ioBoard = configParser.get('config', 'I/O_Board')
|
||||
mute = configParser.get('config', 'mute')
|
||||
wakeup_Time = str(configParser.get('config', 'wakeup_Time')).split(',')
|
||||
bed_Time = str(configParser.get('config', 'bed_Time')).split(',')
|
||||
tvOut = configParser.get('config', 'TV_Output')
|
||||
width = configParser.get('config', 'Width')
|
||||
height = configParser.get('config', 'Height')
|
||||
rebootCMD = configParser.get('config', 'Reboot')
|
||||
|
||||
print('done!')
|
||||
|
||||
print('initializing logger...')
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
try:
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
except Exception as er:
|
||||
print('[Logger] Could not create .log' + str(er))
|
||||
print('[USB] Could not find USB storage!')
|
||||
while 1:
|
||||
checkConnectionToFTPServer()
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
if internetTmp == 'false':
|
||||
rebootFlag = 'true'
|
||||
print('[Timelapse Core] Reboot Flag set')
|
||||
if rebootFlag == 'true':
|
||||
rebootSystem()
|
||||
print('[Timlapse Core] Waiting for maintenance!')
|
||||
sleep(3600)
|
||||
logging.info('starting camera version: ' + version)
|
||||
print('done')
|
||||
print('reading picture number...')
|
||||
try:
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
except Exception as ea:
|
||||
print('No picture_Number.txt found. Creating new .. ')
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(0))
|
||||
f.close()
|
||||
f = open("/home/pi/picture_Number.txt", 'r')
|
||||
picture_number = int(f.read())
|
||||
f.close()
|
||||
logging.info('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('Maybe a fatal error occourd! New picture_Number was created. Risk of redundancy!')
|
||||
print('done, picture number is:', str(picture_number))
|
||||
print('sleeping for 15 seconds until boot is finished!')
|
||||
|
||||
sleep(15)
|
||||
|
||||
camera = PiCamera()
|
||||
upload_every = upload_only
|
||||
internetTmp = internet
|
||||
rebootFlag = 'false'
|
||||
|
||||
if ioBoard == "true":
|
||||
print('Running with the nice IO board!')
|
||||
spi = SpiDev()
|
||||
spi.open(0, 0)
|
||||
spi.max_speed_hz = 5000
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setup(piezo, GPIO.OUT)
|
||||
GPIO.setup(bootSwitch, GPIO.IN)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
#camera = PiCamera()
|
||||
camera.resolution = (3280, 2464) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.awb_mode = 'auto'
|
||||
camera.start_preview()
|
||||
# camera.start_preview(fullscreen=False, window = (100, 20, 960, 720))
|
||||
print('---starting sharpness tool---')
|
||||
print('--------normal output--------')
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
sleep(5)
|
||||
if GPIO.input(bootSwitch) == 0:
|
||||
camera.stop_preview()
|
||||
print('---------zoom output---------')
|
||||
camera.crop = (0.5, 0.5, 0.3, 0.4)
|
||||
camera.start_preview()
|
||||
while GPIO.input(bootSwitch) == 0: sleep(1)
|
||||
camera.stop_preview()
|
||||
else:
|
||||
camera.stop_preview()
|
||||
camera.close()
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(1)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
else:
|
||||
print('Running without the nice IO board!')
|
||||
|
||||
if rebootCMD == 'true':
|
||||
print('Automatic reboot is allowed while wake up!')
|
||||
logging.info('Automatic reboot is allowed while wakeup!')
|
||||
|
||||
if tvOut == 'false':
|
||||
stdoutdata = subprocess.getoutput('sudo /usr/bin/tvservice -o ')
|
||||
print('[TV Out] Status: ' + stdoutdata)
|
||||
logging.info('[TV Out] Status: ' + stdoutdata)
|
||||
|
||||
if internetTmp == 'true':
|
||||
print('Running in Online mode, internet connection is required')
|
||||
if enable_modem == 'true': reconnectToInternet()
|
||||
try:
|
||||
ftp = FTP(ftp_url, ftp_user, ftp_pw, timeout=20)
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
print('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
logging.info('New ftp connection successful! current dicertory is: ' + ftp.pwd())
|
||||
except Exception as ea:
|
||||
print('no internet or ftp connection on startup trying to reconnect later!')
|
||||
logging.error('no internet or ftp connection on startup trying to reconnect later!')
|
||||
internetTmp = 'false'
|
||||
else:
|
||||
print('Running in Offline mode, no internet connection is required')
|
||||
|
||||
# camera.framerate = 15 #Fraction(2,6) #framrate 15 bei video und 2592x1944
|
||||
# camera.shutter_speed = 500000
|
||||
|
||||
|
||||
# camera.brightness = 50 #0 bis 100; default 50
|
||||
# camera.contrast = 0 #-100 bis 100; default 0
|
||||
# camera.iso = 0 #0 bis 1600; default 0(auto)
|
||||
# camera.zoom = (0, 0, 1.0, 1.0) #0.0 bis 1.0 (x, y, w, h)
|
||||
# camera.sharpness = 0 #-100 bis 100; default 0
|
||||
|
||||
print('[Camera] Resolution: ' + width + 'x' + height)
|
||||
logging.info('[Camera] Resolution: ' + width + 'x' + height)
|
||||
|
||||
|
||||
# main programm (endless loop)
|
||||
while 1:
|
||||
try:
|
||||
time_next_unix = time.time() + interval # time for next picture
|
||||
time_name = time.asctime(time.localtime(time.time())) # get time for picture_name
|
||||
time_name = time_name.replace(" ", "_")
|
||||
time_name = time_name.replace(":", "-")
|
||||
picture_name = camera_number + '_' + str(picture_number).zfill(6) + '_' + time_name + '.jpg'
|
||||
picture_path = picture_directory + picture_name
|
||||
|
||||
print("Time:", time_name)
|
||||
#camera = PiCamera()
|
||||
camera.resolution = (int(width), int(height)) # aufloesung der bilder maximal 3280,2464s
|
||||
camera.exposure_mode = 'auto'
|
||||
camera.awb_mode = 'auto'
|
||||
camera.crop = (0, 0, 0, 0)
|
||||
try:
|
||||
camera.exif_tags['IFD0.Make'] = 'CopterSicht'
|
||||
camera.exif_tags['IFD0.Model'] = 'Long Term Timelapse Camera'
|
||||
camera.exif_tags['IFD0.ShutterSpeedValue '] = ''
|
||||
camera.exif_tags['IFD0.ISOSpeedRatings '] = ''
|
||||
camera.exif_tags['IFD0.WhiteBalance '] = ''
|
||||
camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) ' + str(datetime.now().year) + ' CopterSicht'
|
||||
camera.exif_tags['IFD0.ImageDescription'] = 'interval capture'
|
||||
|
||||
if mute == "false": GPIO.output(piezo, GPIO.HIGH)
|
||||
print('Capturing image')
|
||||
logging.info('[Camera] Capturing image')
|
||||
camera.capture(picture_path) # capture image and save to picture_path (picture_directory + picture_name)
|
||||
#camera.close()
|
||||
if mute == "false": GPIO.output(piezo, GPIO.LOW)
|
||||
picture_number += 1 # increase picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
except Exception as e2:
|
||||
logging.error(str(e2))
|
||||
|
||||
print('Picture size:', os.path.getsize(picture_path), "Byte")
|
||||
logging.info('[Camera] Picture size: ' + str(os.path.getsize(picture_path)) + ' Byte')
|
||||
checkDiskSpace()
|
||||
if ioBoard == "true": checkSystemVoltage()
|
||||
if os.path.getsize(picture_path) < picture_size:
|
||||
print('it seems to be night!')
|
||||
logging.info('it seems to be night!')
|
||||
midnight1 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds1 = (datetime.now() - midnight1).seconds # seconds since midnight
|
||||
if seconds1 > float(wakeup_Time[datetime.now().month - 1]) * 60 * 60 and seconds1 < float(bed_Time[
|
||||
datetime.now().month - 1]) * 60 * 60: # if it is before 5pm, it cann't be night -> not going to sleep
|
||||
print('cannot be night!')
|
||||
logging.warning('cannot be night!')
|
||||
print("uploadTimer", upload_every)
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
#checkCpu()
|
||||
#sleepToNextPicture()
|
||||
else:
|
||||
os.remove(picture_path) # deleting picture because it's night!
|
||||
picture_number -= 1 # decrease picture_number
|
||||
print('picture_number:', str(picture_number))
|
||||
f = open("/home/pi/picture_Number.txt", 'w')
|
||||
f.write(str(picture_number)) # save picture_number to a text file
|
||||
f.close()
|
||||
# uploading log file
|
||||
print('skipping picture upload!')
|
||||
print('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': print('uploading todays log befor going to sleep!')
|
||||
logging.info('skipping picture upload!')
|
||||
logging.info('sleeping until ' + str(wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
if internetTmp == 'true': logging.info('uploading todays log befor going to sleep!')
|
||||
if internetTmp == 'true':
|
||||
try:
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('[FTP] uploaded log-file successful!')
|
||||
logging.info('[FTP] uploaded log-file successful!')
|
||||
except Exception as e:
|
||||
print('[FTP] upload failed!')
|
||||
print(e)
|
||||
logging.error('[FTP] upload failed!')
|
||||
logging.error(str(e))
|
||||
try:
|
||||
print('[FTP] trying to reconnect ...')
|
||||
logging.info('[FTP]trying to reconnect ...')
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('[FTP] re-connecting ftp successful!')
|
||||
logging.info('[FTP]re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('uploaded log-file successful!')
|
||||
logging.info('uploaded log-file successful!')
|
||||
except Exception as e8:
|
||||
print('[FTP] re-connecting failed!')
|
||||
print(e8)
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
logging.error(str(e8))
|
||||
if reconnectToInternet() == 'true':
|
||||
print('Reconnect successful')
|
||||
logging.info('[Internet] Reconnect successful!')
|
||||
try:
|
||||
print()
|
||||
ftp.connect(ftp_url)
|
||||
ftp.login(ftp_user, ftp_pw)
|
||||
print('re-connecting ftp successful!')
|
||||
ftp.cwd(ftp_log_directory)
|
||||
ftp.storbinary('STOR ' + log_name, open(log_directory + log_name, 'rb'), 1024)
|
||||
ftp.sendcmd('SITE CHMOD 754 ' + log_name)
|
||||
print('[FTP] uploaded log-file successful!')
|
||||
logging.info('[FTP] uploaded log-file successful!')
|
||||
except Exception as e4:
|
||||
print('[FTP] re-connecting failed!')
|
||||
logging.error('[FTP] re-connecting failed!')
|
||||
print(str(e4))
|
||||
logging.error(str(e4))
|
||||
else:
|
||||
print('Reconnect failed')
|
||||
logging.error('[Internet] Reconnect failed!')
|
||||
try:
|
||||
ftp.cwd(ftp_picture_directory)
|
||||
except Exception as er:
|
||||
print('[FTP] could not change ftp directory !')
|
||||
logging.error('[FTP] could not change ftp directory !')
|
||||
print(str(er))
|
||||
logging.error(str(er))
|
||||
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
print('[Timelapse Core] Checking again Internet Connection before sleep')
|
||||
logging.info('[Timelapse Core] Checking again Internet Connection before sleep')
|
||||
checkConnectionToFTPServer()
|
||||
if internetTmp == 'false':
|
||||
rebootFlag = 'true'
|
||||
print('[Timelapse Core] Reboot Flag set')
|
||||
logging.info('[Timelapse Core] Reboot Flag set')
|
||||
if mute == "false":
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.HIGH)
|
||||
sleep(0.5)
|
||||
GPIO.output(piezo, GPIO.LOW)
|
||||
sleep(0.5)
|
||||
|
||||
# sleep until next morning
|
||||
print('going to sleep for a while ...')
|
||||
logging.info('going to sleep for a while ...')
|
||||
midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
seconds = (datetime.now() - midnight).seconds # seconds since midnight
|
||||
if seconds < (
|
||||
float(wakeup_Time[
|
||||
datetime.now().month - 1]) * 60 * 60): # if time between 0am and wakeup_Time
|
||||
sleep_time = (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60) - seconds
|
||||
else: # if time between wakeup_Time and 12pm
|
||||
sleep_time = 86400 - seconds + (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60)
|
||||
print('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
logging.info('sleeping now ' + str(sleep_time) + ' seconds or until ' + str(
|
||||
wakeup_Time[datetime.now().month - 1]) + 'am!')
|
||||
sleep(sleep_time - 10)
|
||||
|
||||
# wake up 10 seconds before we should for exact start
|
||||
print(str((datetime.now() - midnight).seconds) + ' < ' + str(
|
||||
(wakeup_Time[datetime.now().month - 1] * 60 * 60)))
|
||||
while (datetime.now() - midnight).seconds < (float(wakeup_Time[datetime.now().month - 1]) * 60 * 60):
|
||||
sleep(0.5)
|
||||
log_name = camera_number + '_' + str(datetime.now().strftime("%A %d. %B %Y")) + '.log'
|
||||
log_name = log_name.replace(" ", "-")
|
||||
logging.getLogger('').handlers = []
|
||||
logging.basicConfig(filename=log_directory + log_name, format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S')
|
||||
print('slept for ' + str(sleep_time) + ' seconds')
|
||||
print('Time to wakeup! \n')
|
||||
logging.info('Camera versiom: ' + version)
|
||||
logging.info('slept for ' + str(sleep_time) + ' seconds')
|
||||
logging.info('Time to wakeup! \n')
|
||||
if rebootFlag == 'true':
|
||||
rebootSystem()
|
||||
else:
|
||||
|
||||
if (internetTmp == 'true') and (upload_every == 1):
|
||||
uploadToFTP()
|
||||
upload_every = upload_only
|
||||
else:
|
||||
upload_every -= 1
|
||||
if upload_every < 0:
|
||||
upload_every = 0
|
||||
if upload_every == 0:
|
||||
logging.info('Skipping upload. Next picture will be uploaded')
|
||||
print('Skipping upload. Next picture will be uploaded')
|
||||
else:
|
||||
logging.info('Skipping upload. Next upload in ' + str(upload_every) + ' pictures')
|
||||
print('Skipping upload. Next upload in', (upload_every), 'pictures')
|
||||
|
||||
if internet == 'true' and internetTmp == 'false':
|
||||
checkConnectionToFTPServer()
|
||||
|
||||
checkCpu()
|
||||
sleepToNextPicture()
|
||||
|
||||
except Exception as loopError:
|
||||
print('[Timlapse Core] ' + str(loopError))
|
||||
try:
|
||||
logging.error('[FTP] could not change ftp directory !')
|
||||
except Exception as er:
|
||||
print('[Timlapse Core] Could not log: ' + str(er))
|
||||
|
||||
|
||||
23
Software/ZK_python/config.txt
Normal file
23
Software/ZK_python/config.txt
Normal file
@ -0,0 +1,23 @@
|
||||
[config]
|
||||
ftp_url = schuttercloud.com
|
||||
ftp_picture_directory = /camera03/Position02
|
||||
ftp_log_directory = /logs
|
||||
ftp_user = uploader
|
||||
ftp_pw = XXXXXXXXXXXXXXXXXXXXXXXXX
|
||||
enable_modem = true
|
||||
picture_directory = /media/usb/pictures/
|
||||
log_directory = /media/usb/log/
|
||||
picture_size = 2300000
|
||||
camera_number = 03
|
||||
internet = true
|
||||
modem_apn = web.vodafone.de
|
||||
upload_only = 1
|
||||
interval = 300
|
||||
I/O_Board = false
|
||||
mute = true
|
||||
wakeup_Time: 8,8,7,7,6,5.5,6,6.5,7.5,7.5,8,8
|
||||
bed_Time: 17,17.5,18,20,20.5,21.5,21.5,20.5,19.5,17.5,16.5,16.5
|
||||
TV_Output: false
|
||||
Width: 3280
|
||||
Height: 2464
|
||||
Reboot: false
|
||||
Reference in New Issue
Block a user