[SOLVED] SenseHat data logger and camera code not working together

noyaus

Honorable
Jul 15, 2016
156
1
10,715
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.




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 = []
 
Solution
If each part, imaging and data logging, is working independently then try to determine where the problem lies by inserting a few test "prints" at strategic points in your main program.

Just use print to display the variables at various steps as your program executes.

Example:

# This print line is showing the values for variables X,Y, Z after line # ____

print('Variable values for X,YZ', x, y, z)


Track the variable changes and determine if they change (or do not change) as expected.

Focus on a just a couple of variables at a time. If they track as intended then change the prints to show other variables.

Try a " print run" with only imaging variables, a run with only data logging variables, and lastly a run using a...
If each part, imaging and data logging, is working independently then try to determine where the problem lies by inserting a few test "prints" at strategic points in your main program.

Just use print to display the variables at various steps as your program executes.

Example:

# This print line is showing the values for variables X,Y, Z after line # ____

print('Variable values for X,YZ', x, y, z)


Track the variable changes and determine if they change (or do not change) as expected.

Focus on a just a couple of variables at a time. If they track as intended then change the prints to show other variables.

Try a " print run" with only imaging variables, a run with only data logging variables, and lastly a run using a variable or two from each.
 
Solution