1

I am trying to use a SenseHat and Pi camera to do some data logging. What I want my program to do is when run, it should collect sensor data and take a photo every 10 seconds. The issue that happens when I run my program is the fact that it only takes photos but does not log any sensor data. Any help would be appreciated. I am quite new to 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


##### 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 #####
sense = SenseHat()
batch_data= []
camera = PiCamera()


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 = []

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.