Hi Guys,
I am doing a project in which I am getting the data from the sensors recorded alongside photos taken on the pi camera every 10 seconds. I have sourced the code for the data logger and camera and they work well individually. But when I put them together, the only thing that works is the camera taking pictures every 10 seconds, the data from the sensors are not recorded.
I have done a few things like making the camera timelapse a function and declaring it in the 'While True' loop but still the only thing that works is the camera taking photos and no data logger working. I'm out of ideas on what to do and it would be appreciated if someone can guide me in the right path to getting both the data logger and the timelapse camera to work together in one program.
I am doing a project in which I am getting the data from the sensors recorded alongside photos taken on the pi camera every 10 seconds. I have sourced the code for the data logger and camera and they work well individually. But when I put them together, the only thing that works is the camera taking pictures every 10 seconds, the data from the sensors are not recorded.
I have done a few things like making the camera timelapse a function and declaring it in the 'While True' loop but still the only thing that works is the camera taking photos and no data logger working. I'm out of ideas on what to do and it would be appreciated if someone can guide me in the right path to getting both the data logger and the timelapse camera to work together in one program.
Python:
##### Libraries #####
from sense_hat import SenseHat
from datetime import datetime
from time import sleep
from picamera import PiCamera
##### Logging Settings #####
FILENAME = ""
WRITE_FREQUENCY = 100
red = (255,0,0)
green = (0,255,0)
sense = SenseHat()
batch_data= []
camera = PiCamera()
##### Functions #####
def file_setup(filename):
header =["temp_h","temp_p","humidity","pressure", "temp", "timestamp"]
with open(filename,"w") as f:
f.write(",".join(str(value) for value in header)+ "\n")
def log_data():
output_string = ",".join(str(value) for value in sense_data)
batch_data.append(output_string)
def get_sense_data():
sense_data=[]
sense_data.append(sense.get_temperature_from_humidity())
sense_data.append(sense.get_temperature_from_pressure())
sense_data.append(sense.get_humidity())
sense_data.append(sense.get_pressure())
sense_data.append(sense.get_temperature())
sense_data.append(datetime.now())
return sense_data
def cam():
sleep(2)
for filename in camera.capture_continuous('img{counter:03d}.jpg'):
print('Captured %s' % filename)
sleep(10)
##### Main Program #####
if FILENAME == "":
filename = "SenseLog-"+str(datetime.now())+".csv" #this pretty much helps with the naming of the file of the CSV. It will pretty much save the name of the CSV as the data and time this program was run.
else:
filename = FILENAME+"-"+str(datetime.now())+".csv"
file_setup(filename)
while True:
sense_data = get_sense_data()
log_data()
cam()
if len(batch_data) >= WRITE_FREQUENCY:
print("Writing to file..")
with open(filename,"a") as f:
for line in batch_data:
f.write(line + "\n")
batch_data = []