Issue
we are sending a weather ballon to space with a raspberry pi sense hat and rasp pi camera V2 we have the code written to save to file
This is the code to take pictures
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
camera.start_preview(alpha=200)
camera.rotation = 180
for i in range(3):
sleep(5)
camera.capture('/home/pi/Desktop/image%s.jpg' % i)
camera.stop_preview()
and this is the code to sense the enviroment
from sense_hat import SenseHat
import time
sense=SenseHat()
file = open("Datafile.csv","a")
file.write("Time, Humidity, Temperature, Pressure")
print ("Time, Humidity, Temperature, Pressure")
for n in range(60 ):
humidity = sense.get_humidity()
humidity = round(humidity, 2)
file.write(time.strftime('%X'))
file.write(",")
file.write(str(humidity))
file.write("/n")
temperature = sense.get_temperature()
temperature = round(temperature, 2)
print (time.strftime('%X'),humidity)
file.write(time.strftime('%X'))
file.write(",")
file.write(str(temperature))
file.write("/n")
temperature = sense.get_temperature()
temperature = round(temperature, 2)
print (time.strftime('%X'),temperature)
pressure = sense.get_pressure()
pressure = round(pressure, 2)
file.write(time.strftime('%X'))
file.write(",")
file.write(str(pressure))
file.write("\n")
print (time.strftime('%X'),pressure)
time.sleep(1)
file.close()
Both work we are just now trying to combine them. Please help!!
Solution
What I would do is take the code for each action taking a photo and collecting sensor data and create a function for each. Then you could use a single loop that calls each function. I quickly put together an example of how this could look with your code that uses a single main loop to collect a photo about every 5 seconds and sensor data about every second.
from picamera import PiCamera
from sense_hat import SenseHat
import time
def initialize_camera():
camera = PiCamera()
camera.start_preview()
camera.start_preview(alpha=200)
camera.rotation = 180
return camera
def capture_picture(camera, img_number):
camera.capture('/home/pi/Desktop/image%s.jpg' % img_number)
def capture_sensor_data(sense, file):
humidity = sense.get_humidity()
humidity = round(humidity, 2)
file.write(time.strftime('%X'))
file.write(",")
file.write(str(humidity))
file.write("/n")
temperature = sense.get_temperature()
temperature = round(temperature, 2)
print(time.strftime('%X'), humidity)
file.write(time.strftime('%X'))
file.write(",")
file.write(str(temperature))
file.write("/n")
temperature = sense.get_temperature()
temperature = round(temperature, 2)
print(time.strftime('%X'), temperature)
pressure = sense.get_pressure()
pressure = round(pressure, 2)
file.write(time.strftime('%X'))
file.write(",")
file.write(str(pressure))
file.write("\n")
print(time.strftime('%X'), pressure)
if __name__ == '__main__':
camera = initialize_camera()
sense = SenseHat()
with open("Datafile.csv", "a") as file:
file.write("Time, Humidity, Temperature, Pressure")
print("Time, Humidity, Temperature, Pressure")
photo_num = 1
for i in range(60):
if i % 5 == 0:
capture_picture(camera, photo_num)
photo_num += 1
capture_sensor_data(sense, file)
time.sleep(1)
camera.stop_preview()
Answered By - nickrl21 Answer Checked By - Marilyn (WPSolving Volunteer)