0

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!!

Sayse
  • 42,633
  • 14
  • 77
  • 146

1 Answers1

1

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()

nickrl21
  • 96
  • 6
  • I was hopeful, but it didn't work. – speedee_doge Nov 18 '21 at 21:21
  • what kind of errors or issues occurred when you tried it? – nickrl21 Nov 19 '21 at 09:39
  • 1
    The big error that came up was "if __name__ == '__main__': " But you gave me an idea of how to combine the two so I'm making my own functions to execute it. Thank you for your help! – speedee_doge Nov 21 '21 at 19:09
  • Here is a good post about "if __name__ == '__main__'" and what it does in python and why it is used https://stackoverflow.com/questions/419163/what-does-if-name-main-do It may not be the right thing to use for this application – nickrl21 Nov 22 '21 at 18:02