Files
timelapse-raspi/Time_Lapse_Connection.py

134 lines
4.3 KiB
Python

import time
from datetime import datetime
import picamera2 as pc
import smbus2
import os
import json
import shutil
import requests
class TimeLapse:
def __init__(self):
self.url = "https://timelapse.kerboul.me/api/camera/upload"
self.headers = {
'accept': 'application/json'
}
self.data = {
"timestamp": "",
"temperature": "",
'humidity': "",
}
self.project_path = "//home//timelapse//Documents//Time_Lapse//PROJECT//"
def create_Folder(self):
filename = self.data["timestamp"]
os.mkdir(self.project_path + filename)
def create_Json(self):
filename = self.project_path + self.data["timestamp"] + "//" + self.data["timestamp"] + ".json"
with open(filename, "w") as file:
json.dump(self.get_Alldic(), file)
def get_Alldic(self):
return self.data
def get_Hygrodata(self):
bus = smbus2.SMBus(1)
ans = bus.write_byte(0x44, 0xFD)
time.sleep(0.1)
ans=smbus2.i2c_msg.read(0x44,6)
bus.i2c_rdwr(ans)
data = list(ans)
t_ticks = data[0]*256 + data[1] #Pas de CRC utilisé
rh_ticks = data[3]*256 + data[4] #Pas de CRC utilisé
self.data["temperature"] = (175*t_ticks)/65535. -45
self.data["humidity"] = (125*rh_ticks)/65535. -6
return self.data
def get_humidity(self):
return self.data["humidity"]
def get_temperature(self):
return self.data["temperature"]
def get_TimeStamp(self): #ATTENTION AU NOMMAGE DES FICHIERS !!! PAS DE : ET ESPACES
now = datetime.now()
#dt_string = now.strftime("%d_%m_%Y_%H_%M_%S")
dt_string = now.strftime("%Y-%m-%d %H:%M:%S")
print("date and time =", str(dt_string))
self.data["timestamp"]= str(dt_string)
return str(dt_string)
def pick_Picture(self):
cam = pc.Picamera2()
conf = cam.create_still_configuration(main={"size": (800, 600)})
cam.configure(conf)
cam.start()
# Allow camera time to initialize
time.sleep(2)
timestamp = self.get_TimeStamp()
path = self.project_path + timestamp + "//" + timestamp + ".jpg"
self.create_Folder()
image = cam.capture_file(path)
cam.close()
def del_Folder(self, name):
shutil.rmtree(self.project_path + name)
def Singleroutin(self):
TL.get_Hygrodata()
TL.pick_Picture()
TL.create_Json()
timestamp = str(TL.data["timestamp"])
path_file = self.project_path +timestamp+ "//" + timestamp + ".jpg"
answer_from_server = TL.upload_measurement( path_file, timestamp, TL.get_temperature(), TL.get_humidity())
print(answer_from_server)
try:
if (answer_from_server["message"]== "Measurement uploaded successfully"):
TL.del_Folder(timestamp)
except Exception as e:
print(e)
def upload_measurement(self, image_path, timestamp, temperature, humidity):
data = {
'timestamp': timestamp,
'temperature': temperature,
'humidity': humidity
}
#print(data)
#print(image_path)
files = {
'image': (image_path, open(image_path, 'rb'), 'image/jpeg')
}
try:
response = requests.post(self.url, headers=self.headers, data=data, files=files)
response.raise_for_status()
response_data = response.json()
return response_data
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Exception as err:
print(f"Other error occurred: {err}")
if __name__ == "__main__":
print("FICHIER TIMELAPSE")
TL = TimeLapse()
TL.Singleroutin()
time.sleep(1)
""" FONCTIONNEL
TL.get_Hygrodata()
TL.pick_Picture()
TL.create_Json()
try:
TL.del_Folder("13_03_2025_12_07_56")
except:
pass
"""
# print(TL.get_data())
# TL.get_Picture()
# print("Temper/home/timelapse/Documents/Time_Lapse/PROJECTature is : ", TL.get_temperature())
# print("Humidity is : ", TL.get_humidity())
# datas = TL.get_Alldic()
# print(datas)