1

I'm working on a code for a High Altitude Weather Balloon and I wanted the camera to start recording after a certain Altitude. The first output of information is correct but then it spits out wrong information after. The camera also records but it pauses every second after and there isn't any time.sleep()

Here's my python code.

import datetime
import picamera
import time
import smbus2

bus = smbus2.SMBus(1)
# MPL3115A2 address, 0x60(96)
# Select control register, 0x26(38)
#       0xB9(185)   Active mode, OSR = 128, Altimeter mode
bus.write_byte_data(0x60, 0x26, 0xB9)
# MPL3115A2 address, 0x60(96)
# Select data configuration register, 0x13(19)
#       0x07(07)    Data ready event enabled for altitude, pressure, temperature
bus.write_byte_data(0x60, 0x13, 0x07)
# MPL3115A2 address, 0x60(96)
# Select control register, 0x26(38)
#       0xB9(185)   Active mode, OSR = 128, Altimeter mode
bus.write_byte_data(0x60, 0x26, 0xB9)

time.sleep(1)

camera = picamera.PiCamera()
camera.resolution = (640, 480)
timeStopRec = None
while True:

data = bus.read_i2c_block_data(0x60, 0x00, 6)
tHeight = ((data[1]*65536)+(data[2]*256)+(data[3]&0xF0))/16
temp = ((data[4]*256)+(data[5]&0xF0))/16
altitude = tHeight / 16.0
cTemp = temp / 16.0
fTemp = cTemp * 1.8 + 32
bus.write_byte_data(0x60, 0x26, 0x39)
time.sleep(1)

data = bus.read_i2c_block_data(0x60, 0x00, 4)

pres = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16
pressure = (pres / 4.0) / 1000.0

print "Pressure : %.2f kPa" %pressure
print "Altitude : %.2f m" %altitude
print "Temperature in Celsius  : %.2f C" %cTemp
print "Temperature in Fahrenheit  : %.2f F" %fTemp

if tHeight > 100 and not camera.recording:
    print('start recording')
    camera.start_recording(datetime.datetime.now().strftime('%Y%m%d-%H%M%S.h264'))
    timeStopRec = datetime.datetime.now() + datetime.timedelta(minutes=210)

if camera.recording and timeStopRec != None and datetime.datetime.now() > timeStopRec:
    print('stop recording')
    camera.stop_recording()
  • You need to edit the question and format the code correctly. The spacing in Python is part of the syntax. – joan Mar 23 at 20:55
  • In your While True: loop you set the mode to barometer mode, but never set it back to altimeter mode - I would suggest moving lines 15 - 20, i.e. the second time you set the mode to altimeter mode before the loop, somewhere either at the top or bottom of the loop – Jaromanda X Mar 24 at 1:29
  • @JaromandaX I've got nothing, I tried putting it before and after the while loops, spent maybe around 5 hours figuring it out but it's not working. It will always print 65,000m for altitude. – AJCoder Mar 27 at 19:53
  • Oh, I obviously wasn't clear enough ... at the top or bottom INSIDE the loop – Jaromanda X Mar 27 at 20:33
  • @JaromandaX Sorry, I guess I deleted that part when I copied and pasted from my program.. I have that on my program, turns out the numbers were still displaying inaccurate results. We've come to a conclusion that the sensor itself might have been been damaged. Thank you so much! I will update if it still does not work. – AJCoder Mar 31 at 3:01
1

You need to move the second instance of setting to Altimeter mode INSIDE the loop

import datetime
import picamera
import time
import smbus2

bus = smbus2.SMBus(1)
# MPL3115A2 address, 0x60(96)
# Select control register, 0x26(38)
#       0xB9(185)   Active mode, OSR = 128, Altimeter mode
bus.write_byte_data(0x60, 0x26, 0xB9)
# MPL3115A2 address, 0x60(96)
# Select data configuration register, 0x13(19)
#       0x07(07)    Data ready event enabled for altitude, pressure, temperature
bus.write_byte_data(0x60, 0x13, 0x07)
# MPL3115A2 address, 0x60(96)
# Select control register, 0x26(38)
#       0xB9(185)   Active mode, OSR = 128, Altimeter mode

camera = picamera.PiCamera()
camera.resolution = (640, 480)
timeStopRec = None

while True:
    # Altimeter mode (code moved from above)
    bus.write_byte_data(0x60, 0x26, 0xB9)
    time.sleep(1)

    data = bus.read_i2c_block_data(0x60, 0x00, 6)
    tHeight = ((data[1]*65536)+(data[2]*256)+(data[3]&0xF0))/16
    temp = ((data[4]*256)+(data[5]&0xF0))/16
    altitude = tHeight / 16.0
    cTemp = temp / 16.0
    fTemp = cTemp * 1.8 + 32

    # Pressure mode
    bus.write_byte_data(0x60, 0x26, 0x39)
    time.sleep(1)

    data = bus.read_i2c_block_data(0x60, 0x00, 4)

    pres = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16
    pressure = (pres / 4.0) / 1000.0

    print "Pressure : %.2f kPa" %pressure
    print "Altitude : %.2f m" %altitude
    print "Temperature in Celsius  : %.2f C" %cTemp
    print "Temperature in Fahrenheit  : %.2f F" %fTemp

    if tHeight > 100 and not camera.recording:
        print('start recording')
        camera.start_recording(datetime.datetime.now().strftime('%Y%m%d-%H%M%S.h264'))
        timeStopRec = datetime.datetime.now() + datetime.timedelta(minutes=210)

    if camera.recording and timeStopRec != None and datetime.datetime.now() > timeStopRec:
        print('stop recording')
        camera.stop_recording()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.