1

I'm sending up a weather balloon controlled by a Raspberry Pi and BMP085 Sensor, and I'm using some Python Code via I2C to run it. I want to modify the Adafruit BMP085 Example Code that is runs it every half an hour:

#!/usr/bin/python

from Adafruit_BMP085 import BMP085

# ===========================================================================
# Example Code
# ===========================================================================

# Initialise the BMP085 and use STANDARD mode (default value)
# bmp = BMP085(0x77, debug=True)
bmp = BMP085(0x77)

# To specify a different operating mode, uncomment one of the following:
# bmp = BMP085(0x77, 0)  # ULTRALOWPOWER Mode
# bmp = BMP085(0x77, 1)  # STANDARD Mode
# bmp = BMP085(0x77, 2)  # HIRES Mode
# bmp = BMP085(0x77, 3)  # ULTRAHIRES Mode

temp = bmp.readTemperature()
pressure = bmp.readPressure()
altitude = bmp.readAltitude()

print "Temperature: %.2f C" % temp
print "Pressure:    %.2f hPa" % (pressure / 100.0)
print "Altitude:    %.2f" % altitude

I was planning to run it using a shell script, but it feels too much power on shell,

#!/bin/bash

while true; do
    /gryphon7i/alt/altitude_ai >> measurements.log

    sleep 1800 
done

What can I do to modify the code to record it every half and hour? Should I just stick the shell script? Thank you for any help in advance.

sharksfan98
  • 547
  • 3
  • 9
  • 19
  • 1
    Your shell script doesn't run it every half hour - it's every "half hour + the time the measurement takes to run" – John La Rooy Feb 28 '13 at 00:12

2 Answers2

2

Check out the following answers posted on that page

You might want to consider the Reactor Pattern implemented with Twisted. It's pretty easy to execute a command every 30 minutes.

In order for that to work you have to install the twisted library at first. You can do that by typing

sudo easy_install twisted

or

sudo pip install twisted

into your console. After that you'll be able to use that library inside your code.

#!/usr/bin/python

from Adafruit_BMP085 import BMP085
from twisted.internet import task
from twisted.internet import reactor

bmp = BMP085(0x77)    

timeout = 60.0 * 30 # 30 minutes

def doWork():
    temp = bmp.readTemperature()
    pressure = bmp.readPressure()
    altitude = bmp.readAltitude()

    # write log
    f = open('measurements.log', 'a') 
    f.write("Temperature: %.2f C" % temp)
    f.write("Pressure: %.2f hPa" % (pressure / 100.0))
    f.write("Altitude: %.2f" % altitude)
    f.close


l = task.LoopingCall(doWork)
l.start(timeout) # call every sixty seconds

reactor.run()

I wrote that from my memory. You might have to experiment a little with the code.

Community
  • 1
  • 1
Dennis
  • 1,805
  • 14
  • 17
1
import time

while True:
    # sleep up until then next 1/2 hour
    time.sleep(1800-time.time()%1800)

    temp = bmp.readTemperature()
    pressure = bmp.readPressure()
    altitude = bmp.readAltitude()

    print "Temperature: %.2f C" % temp
    print "Pressure:    %.2f hPa" % (pressure / 100.0)
    print "Altitude:    %.2f" % altitude
John La Rooy
  • 295,403
  • 53
  • 369
  • 502