From 7e83be2adca6ed60277bc0d576c298e2c2a05991 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 8 Jul 2020 11:10:10 +0100 Subject: [PATCH] Get datalogger working --- Data Logger/Adafruit_BMP085.py | 259 ++++++++++++++++++ Data Logger/Adafruit_BMP085.pyc | Bin 0 -> 7579 bytes .../Adafruit_BMP085/Adafruit_BMP085.py | 259 ++++++++++++++++++ .../Adafruit_BMP085_example.py | 35 +++ .../Adafruit_BMP085_googledocs_ex.py | 69 +++++ Data Logger/Adafruit_BMP085/Adafruit_I2C.py | 1 + Data Logger/Adafruit_BMP085/DEPRECATED.txt | 5 + Data Logger/Adafruit_I2C.py | 161 +++++++++++ Data Logger/Adafruit_I2C.pyc | Bin 0 -> 6035 bytes Data Logger/DataLog.py | 3 +- Data Logger/data.txt | 1 + 11 files changed, 791 insertions(+), 2 deletions(-) create mode 100755 Data Logger/Adafruit_BMP085.py create mode 100644 Data Logger/Adafruit_BMP085.pyc create mode 100755 Data Logger/Adafruit_BMP085/Adafruit_BMP085.py create mode 100755 Data Logger/Adafruit_BMP085/Adafruit_BMP085_example.py create mode 100755 Data Logger/Adafruit_BMP085/Adafruit_BMP085_googledocs_ex.py create mode 120000 Data Logger/Adafruit_BMP085/Adafruit_I2C.py create mode 100644 Data Logger/Adafruit_BMP085/DEPRECATED.txt create mode 100755 Data Logger/Adafruit_I2C.py create mode 100644 Data Logger/Adafruit_I2C.pyc create mode 100644 Data Logger/data.txt diff --git a/Data Logger/Adafruit_BMP085.py b/Data Logger/Adafruit_BMP085.py new file mode 100755 index 0000000..e8d0e31 --- /dev/null +++ b/Data Logger/Adafruit_BMP085.py @@ -0,0 +1,259 @@ +#!/usr/bin/python + +import time +from Adafruit_I2C import Adafruit_I2C + +# =========================================================================== +# BMP085 Class +# =========================================================================== + +class BMP085 : + i2c = None + + # Operating Modes + __BMP085_ULTRALOWPOWER = 0 + __BMP085_STANDARD = 1 + __BMP085_HIGHRES = 2 + __BMP085_ULTRAHIGHRES = 3 + + # BMP085 Registers + __BMP085_CAL_AC1 = 0xAA # R Calibration data (16 bits) + __BMP085_CAL_AC2 = 0xAC # R Calibration data (16 bits) + __BMP085_CAL_AC3 = 0xAE # R Calibration data (16 bits) + __BMP085_CAL_AC4 = 0xB0 # R Calibration data (16 bits) + __BMP085_CAL_AC5 = 0xB2 # R Calibration data (16 bits) + __BMP085_CAL_AC6 = 0xB4 # R Calibration data (16 bits) + __BMP085_CAL_B1 = 0xB6 # R Calibration data (16 bits) + __BMP085_CAL_B2 = 0xB8 # R Calibration data (16 bits) + __BMP085_CAL_MB = 0xBA # R Calibration data (16 bits) + __BMP085_CAL_MC = 0xBC # R Calibration data (16 bits) + __BMP085_CAL_MD = 0xBE # R Calibration data (16 bits) + __BMP085_CONTROL = 0xF4 + __BMP085_TEMPDATA = 0xF6 + __BMP085_PRESSUREDATA = 0xF6 + __BMP085_READTEMPCMD = 0x2E + __BMP085_READPRESSURECMD = 0x34 + + # Private Fields + _cal_AC1 = 0 + _cal_AC2 = 0 + _cal_AC3 = 0 + _cal_AC4 = 0 + _cal_AC5 = 0 + _cal_AC6 = 0 + _cal_B1 = 0 + _cal_B2 = 0 + _cal_MB = 0 + _cal_MC = 0 + _cal_MD = 0 + + # Constructor + def __init__(self, address=0x77, mode=1, debug=False): + self.i2c = Adafruit_I2C(address) + + self.address = address + self.debug = debug + # Make sure the specified mode is in the appropriate range + if ((mode < 0) | (mode > 3)): + if (self.debug): + print "Invalid Mode: Using STANDARD by default" + self.mode = self.__BMP085_STANDARD + else: + self.mode = mode + # Read the calibration data + self.readCalibrationData() + + def readS16(self, register): + "Reads a signed 16-bit value" + hi = self.i2c.readS8(register) + lo = self.i2c.readU8(register+1) + return (hi << 8) + lo + + def readU16(self, register): + "Reads an unsigned 16-bit value" + hi = self.i2c.readU8(register) + lo = self.i2c.readU8(register+1) + return (hi << 8) + lo + + def readCalibrationData(self): + "Reads the calibration data from the IC" + self._cal_AC1 = self.readS16(self.__BMP085_CAL_AC1) # INT16 + self._cal_AC2 = self.readS16(self.__BMP085_CAL_AC2) # INT16 + self._cal_AC3 = self.readS16(self.__BMP085_CAL_AC3) # INT16 + self._cal_AC4 = self.readU16(self.__BMP085_CAL_AC4) # UINT16 + self._cal_AC5 = self.readU16(self.__BMP085_CAL_AC5) # UINT16 + self._cal_AC6 = self.readU16(self.__BMP085_CAL_AC6) # UINT16 + self._cal_B1 = self.readS16(self.__BMP085_CAL_B1) # INT16 + self._cal_B2 = self.readS16(self.__BMP085_CAL_B2) # INT16 + self._cal_MB = self.readS16(self.__BMP085_CAL_MB) # INT16 + self._cal_MC = self.readS16(self.__BMP085_CAL_MC) # INT16 + self._cal_MD = self.readS16(self.__BMP085_CAL_MD) # INT16 + if (self.debug): + self.showCalibrationData() + + def showCalibrationData(self): + "Displays the calibration values for debugging purposes" + print "DBG: AC1 = %6d" % (self._cal_AC1) + print "DBG: AC2 = %6d" % (self._cal_AC2) + print "DBG: AC3 = %6d" % (self._cal_AC3) + print "DBG: AC4 = %6d" % (self._cal_AC4) + print "DBG: AC5 = %6d" % (self._cal_AC5) + print "DBG: AC6 = %6d" % (self._cal_AC6) + print "DBG: B1 = %6d" % (self._cal_B1) + print "DBG: B2 = %6d" % (self._cal_B2) + print "DBG: MB = %6d" % (self._cal_MB) + print "DBG: MC = %6d" % (self._cal_MC) + print "DBG: MD = %6d" % (self._cal_MD) + + def readRawTemp(self): + "Reads the raw (uncompensated) temperature from the sensor" + self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READTEMPCMD) + time.sleep(0.005) # Wait 5ms + raw = self.readU16(self.__BMP085_TEMPDATA) + if (self.debug): + print "DBG: Raw Temp: 0x%04X (%d)" % (raw & 0xFFFF, raw) + return raw + + def readRawPressure(self): + "Reads the raw (uncompensated) pressure level from the sensor" + self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READPRESSURECMD + (self.mode << 6)) + if (self.mode == self.__BMP085_ULTRALOWPOWER): + time.sleep(0.005) + elif (self.mode == self.__BMP085_HIGHRES): + time.sleep(0.014) + elif (self.mode == self.__BMP085_ULTRAHIGHRES): + time.sleep(0.026) + else: + time.sleep(0.008) + msb = self.i2c.readU8(self.__BMP085_PRESSUREDATA) + lsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA+1) + xlsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA+2) + raw = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - self.mode) + if (self.debug): + print "DBG: Raw Pressure: 0x%04X (%d)" % (raw & 0xFFFF, raw) + return raw + + def readTemperature(self): + "Gets the compensated temperature in degrees celcius" + UT = 0 + X1 = 0 + X2 = 0 + B5 = 0 + temp = 0.0 + + # Read raw temp before aligning it with the calibration values + UT = self.readRawTemp() + X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15 + X2 = (self._cal_MC << 11) / (X1 + self._cal_MD) + B5 = X1 + X2 + temp = ((B5 + 8) >> 4) / 10.0 + if (self.debug): + print "DBG: Calibrated temperature = %f C" % temp + return temp + + def readPressure(self): + "Gets the compensated pressure in pascal" + UT = 0 + UP = 0 + B3 = 0 + B5 = 0 + B6 = 0 + X1 = 0 + X2 = 0 + X3 = 0 + p = 0 + B4 = 0 + B7 = 0 + + UT = self.readRawTemp() + UP = self.readRawPressure() + + # You can use the datasheet values to test the conversion results + # dsValues = True + dsValues = False + + if (dsValues): + UT = 27898 + UP = 23843 + self._cal_AC6 = 23153 + self._cal_AC5 = 32757 + self._cal_MB = -32768; + self._cal_MC = -8711 + self._cal_MD = 2868 + self._cal_B1 = 6190 + self._cal_B2 = 4 + self._cal_AC3 = -14383 + self._cal_AC2 = -72 + self._cal_AC1 = 408 + self._cal_AC4 = 32741 + self.mode = self.__BMP085_ULTRALOWPOWER + if (self.debug): + self.showCalibrationData() + + # True Temperature Calculations + X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15 + X2 = (self._cal_MC << 11) / (X1 + self._cal_MD) + B5 = X1 + X2 + if (self.debug): + print "DBG: X1 = %d" % (X1) + print "DBG: X2 = %d" % (X2) + print "DBG: B5 = %d" % (B5) + print "DBG: True Temperature = %.2f C" % (((B5 + 8) >> 4) / 10.0) + + # Pressure Calculations + B6 = B5 - 4000 + X1 = (self._cal_B2 * (B6 * B6) >> 12) >> 11 + X2 = (self._cal_AC2 * B6) >> 11 + X3 = X1 + X2 + B3 = (((self._cal_AC1 * 4 + X3) << self.mode) + 2) / 4 + if (self.debug): + print "DBG: B6 = %d" % (B6) + print "DBG: X1 = %d" % (X1) + print "DBG: X2 = %d" % (X2) + print "DBG: X3 = %d" % (X3) + print "DBG: B3 = %d" % (B3) + + X1 = (self._cal_AC3 * B6) >> 13 + X2 = (self._cal_B1 * ((B6 * B6) >> 12)) >> 16 + X3 = ((X1 + X2) + 2) >> 2 + B4 = (self._cal_AC4 * (X3 + 32768)) >> 15 + B7 = (UP - B3) * (50000 >> self.mode) + if (self.debug): + print "DBG: X1 = %d" % (X1) + print "DBG: X2 = %d" % (X2) + print "DBG: X3 = %d" % (X3) + print "DBG: B4 = %d" % (B4) + print "DBG: B7 = %d" % (B7) + + if (B7 < 0x80000000): + p = (B7 * 2) / B4 + else: + p = (B7 / B4) * 2 + + if (self.debug): + print "DBG: X1 = %d" % (X1) + + X1 = (p >> 8) * (p >> 8) + X1 = (X1 * 3038) >> 16 + X2 = (-7357 * p) >> 16 + if (self.debug): + print "DBG: p = %d" % (p) + print "DBG: X1 = %d" % (X1) + print "DBG: X2 = %d" % (X2) + + p = p + ((X1 + X2 + 3791) >> 4) + if (self.debug): + print "DBG: Pressure = %d Pa" % (p) + + return p + + def readAltitude(self, seaLevelPressure=101325): + "Calculates the altitude in meters" + altitude = 0.0 + pressure = float(self.readPressure()) + altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.1903)) + if (self.debug): + print "DBG: Altitude = %d" % (altitude) + return altitude + + return 0 diff --git a/Data Logger/Adafruit_BMP085.pyc b/Data Logger/Adafruit_BMP085.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1e52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H{Xy z5?Upj#bMbvWX2P~B0*I`Q?jXXvCdA(u6M`~r{5zNalA$$>TjT6X6-gk+Ashov~h}i z1RwL&ULI)Gr-cupVaj}>F$wicrD1Ye9M6q6)u2Lmfnp{Fonw#o%aG9`hC-f$YCT8w z4$Wrkrh@5x&)#lU2V~up=m%weM?5|3@*%l{u=jxtB7|qbGyqGiVxOTbJdg+il>!-m zY|5b4VK)OZUcY6z%h=+>SS=R?^axoROEg1#D&-v|c~dQ#LF+c*9W&cJ)Re!N@^bZ$ z4eMNv`AyIEHVtcygp8QBS`OKTfM0?KD35|ep9grU2=I@u0?4Zm9|r&W4ZvB%w%{ac zI1O#VuYL~NdpM>JzJ+Qj_=wDn5U4el#=D!^u6PN8HY*82UYiiFnabyDYrfX1adnH} zcybisQ3GourUu`ExPUc?p4m1ftyBkNr860+Y<7eJw z^k6t+2fujJlEw*WqgA+uIWsvS3BmW_&6<-~mLuyR1Er{UgeR0U14u{c8iuotDu(Cx z2+ED_&qhVkPE{NLK&7y-Rza_4>XLFL3Zga7ci^;Wt^8V8_k?j}47;6(gAZCL%?9JO;LiF_5tkA7*fY74AenQuQnkG7mr7A5D5Vnq!mw81 zBUdo9QuWQru{w!X&ZD5a{1brNN5DTuxO7&R^}D-^Ku_&i`nzltTyBYbh~NkTBdmLZ zfPWispCmX<@X3xe+;M)JCSVZNep!#c)RRp~$Uxy>2pold8Bru)-hG_z#bG>sAvQK^q(`nvtknGL^4 M86o#PWA(QE7ias9mjD0& literal 0 HcmV?d00001 diff --git a/Data Logger/Adafruit_BMP085/Adafruit_BMP085.py b/Data Logger/Adafruit_BMP085/Adafruit_BMP085.py new file mode 100755 index 0000000..e8d0e31 --- /dev/null +++ b/Data Logger/Adafruit_BMP085/Adafruit_BMP085.py @@ -0,0 +1,259 @@ +#!/usr/bin/python + +import time +from Adafruit_I2C import Adafruit_I2C + +# =========================================================================== +# BMP085 Class +# =========================================================================== + +class BMP085 : + i2c = None + + # Operating Modes + __BMP085_ULTRALOWPOWER = 0 + __BMP085_STANDARD = 1 + __BMP085_HIGHRES = 2 + __BMP085_ULTRAHIGHRES = 3 + + # BMP085 Registers + __BMP085_CAL_AC1 = 0xAA # R Calibration data (16 bits) + __BMP085_CAL_AC2 = 0xAC # R Calibration data (16 bits) + __BMP085_CAL_AC3 = 0xAE # R Calibration data (16 bits) + __BMP085_CAL_AC4 = 0xB0 # R Calibration data (16 bits) + __BMP085_CAL_AC5 = 0xB2 # R Calibration data (16 bits) + __BMP085_CAL_AC6 = 0xB4 # R Calibration data (16 bits) + __BMP085_CAL_B1 = 0xB6 # R Calibration data (16 bits) + __BMP085_CAL_B2 = 0xB8 # R Calibration data (16 bits) + __BMP085_CAL_MB = 0xBA # R Calibration data (16 bits) + __BMP085_CAL_MC = 0xBC # R Calibration data (16 bits) + __BMP085_CAL_MD = 0xBE # R Calibration data (16 bits) + __BMP085_CONTROL = 0xF4 + __BMP085_TEMPDATA = 0xF6 + __BMP085_PRESSUREDATA = 0xF6 + __BMP085_READTEMPCMD = 0x2E + __BMP085_READPRESSURECMD = 0x34 + + # Private Fields + _cal_AC1 = 0 + _cal_AC2 = 0 + _cal_AC3 = 0 + _cal_AC4 = 0 + _cal_AC5 = 0 + _cal_AC6 = 0 + _cal_B1 = 0 + _cal_B2 = 0 + _cal_MB = 0 + _cal_MC = 0 + _cal_MD = 0 + + # Constructor + def __init__(self, address=0x77, mode=1, debug=False): + self.i2c = Adafruit_I2C(address) + + self.address = address + self.debug = debug + # Make sure the specified mode is in the appropriate range + if ((mode < 0) | (mode > 3)): + if (self.debug): + print "Invalid Mode: Using STANDARD by default" + self.mode = self.__BMP085_STANDARD + else: + self.mode = mode + # Read the calibration data + self.readCalibrationData() + + def readS16(self, register): + "Reads a signed 16-bit value" + hi = self.i2c.readS8(register) + lo = self.i2c.readU8(register+1) + return (hi << 8) + lo + + def readU16(self, register): + "Reads an unsigned 16-bit value" + hi = self.i2c.readU8(register) + lo = self.i2c.readU8(register+1) + return (hi << 8) + lo + + def readCalibrationData(self): + "Reads the calibration data from the IC" + self._cal_AC1 = self.readS16(self.__BMP085_CAL_AC1) # INT16 + self._cal_AC2 = self.readS16(self.__BMP085_CAL_AC2) # INT16 + self._cal_AC3 = self.readS16(self.__BMP085_CAL_AC3) # INT16 + self._cal_AC4 = self.readU16(self.__BMP085_CAL_AC4) # UINT16 + self._cal_AC5 = self.readU16(self.__BMP085_CAL_AC5) # UINT16 + self._cal_AC6 = self.readU16(self.__BMP085_CAL_AC6) # UINT16 + self._cal_B1 = self.readS16(self.__BMP085_CAL_B1) # INT16 + self._cal_B2 = self.readS16(self.__BMP085_CAL_B2) # INT16 + self._cal_MB = self.readS16(self.__BMP085_CAL_MB) # INT16 + self._cal_MC = self.readS16(self.__BMP085_CAL_MC) # INT16 + self._cal_MD = self.readS16(self.__BMP085_CAL_MD) # INT16 + if (self.debug): + self.showCalibrationData() + + def showCalibrationData(self): + "Displays the calibration values for debugging purposes" + print "DBG: AC1 = %6d" % (self._cal_AC1) + print "DBG: AC2 = %6d" % (self._cal_AC2) + print "DBG: AC3 = %6d" % (self._cal_AC3) + print "DBG: AC4 = %6d" % (self._cal_AC4) + print "DBG: AC5 = %6d" % (self._cal_AC5) + print "DBG: AC6 = %6d" % (self._cal_AC6) + print "DBG: B1 = %6d" % (self._cal_B1) + print "DBG: B2 = %6d" % (self._cal_B2) + print "DBG: MB = %6d" % (self._cal_MB) + print "DBG: MC = %6d" % (self._cal_MC) + print "DBG: MD = %6d" % (self._cal_MD) + + def readRawTemp(self): + "Reads the raw (uncompensated) temperature from the sensor" + self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READTEMPCMD) + time.sleep(0.005) # Wait 5ms + raw = self.readU16(self.__BMP085_TEMPDATA) + if (self.debug): + print "DBG: Raw Temp: 0x%04X (%d)" % (raw & 0xFFFF, raw) + return raw + + def readRawPressure(self): + "Reads the raw (uncompensated) pressure level from the sensor" + self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READPRESSURECMD + (self.mode << 6)) + if (self.mode == self.__BMP085_ULTRALOWPOWER): + time.sleep(0.005) + elif (self.mode == self.__BMP085_HIGHRES): + time.sleep(0.014) + elif (self.mode == self.__BMP085_ULTRAHIGHRES): + time.sleep(0.026) + else: + time.sleep(0.008) + msb = self.i2c.readU8(self.__BMP085_PRESSUREDATA) + lsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA+1) + xlsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA+2) + raw = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - self.mode) + if (self.debug): + print "DBG: Raw Pressure: 0x%04X (%d)" % (raw & 0xFFFF, raw) + return raw + + def readTemperature(self): + "Gets the compensated temperature in degrees celcius" + UT = 0 + X1 = 0 + X2 = 0 + B5 = 0 + temp = 0.0 + + # Read raw temp before aligning it with the calibration values + UT = self.readRawTemp() + X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15 + X2 = (self._cal_MC << 11) / (X1 + self._cal_MD) + B5 = X1 + X2 + temp = ((B5 + 8) >> 4) / 10.0 + if (self.debug): + print "DBG: Calibrated temperature = %f C" % temp + return temp + + def readPressure(self): + "Gets the compensated pressure in pascal" + UT = 0 + UP = 0 + B3 = 0 + B5 = 0 + B6 = 0 + X1 = 0 + X2 = 0 + X3 = 0 + p = 0 + B4 = 0 + B7 = 0 + + UT = self.readRawTemp() + UP = self.readRawPressure() + + # You can use the datasheet values to test the conversion results + # dsValues = True + dsValues = False + + if (dsValues): + UT = 27898 + UP = 23843 + self._cal_AC6 = 23153 + self._cal_AC5 = 32757 + self._cal_MB = -32768; + self._cal_MC = -8711 + self._cal_MD = 2868 + self._cal_B1 = 6190 + self._cal_B2 = 4 + self._cal_AC3 = -14383 + self._cal_AC2 = -72 + self._cal_AC1 = 408 + self._cal_AC4 = 32741 + self.mode = self.__BMP085_ULTRALOWPOWER + if (self.debug): + self.showCalibrationData() + + # True Temperature Calculations + X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15 + X2 = (self._cal_MC << 11) / (X1 + self._cal_MD) + B5 = X1 + X2 + if (self.debug): + print "DBG: X1 = %d" % (X1) + print "DBG: X2 = %d" % (X2) + print "DBG: B5 = %d" % (B5) + print "DBG: True Temperature = %.2f C" % (((B5 + 8) >> 4) / 10.0) + + # Pressure Calculations + B6 = B5 - 4000 + X1 = (self._cal_B2 * (B6 * B6) >> 12) >> 11 + X2 = (self._cal_AC2 * B6) >> 11 + X3 = X1 + X2 + B3 = (((self._cal_AC1 * 4 + X3) << self.mode) + 2) / 4 + if (self.debug): + print "DBG: B6 = %d" % (B6) + print "DBG: X1 = %d" % (X1) + print "DBG: X2 = %d" % (X2) + print "DBG: X3 = %d" % (X3) + print "DBG: B3 = %d" % (B3) + + X1 = (self._cal_AC3 * B6) >> 13 + X2 = (self._cal_B1 * ((B6 * B6) >> 12)) >> 16 + X3 = ((X1 + X2) + 2) >> 2 + B4 = (self._cal_AC4 * (X3 + 32768)) >> 15 + B7 = (UP - B3) * (50000 >> self.mode) + if (self.debug): + print "DBG: X1 = %d" % (X1) + print "DBG: X2 = %d" % (X2) + print "DBG: X3 = %d" % (X3) + print "DBG: B4 = %d" % (B4) + print "DBG: B7 = %d" % (B7) + + if (B7 < 0x80000000): + p = (B7 * 2) / B4 + else: + p = (B7 / B4) * 2 + + if (self.debug): + print "DBG: X1 = %d" % (X1) + + X1 = (p >> 8) * (p >> 8) + X1 = (X1 * 3038) >> 16 + X2 = (-7357 * p) >> 16 + if (self.debug): + print "DBG: p = %d" % (p) + print "DBG: X1 = %d" % (X1) + print "DBG: X2 = %d" % (X2) + + p = p + ((X1 + X2 + 3791) >> 4) + if (self.debug): + print "DBG: Pressure = %d Pa" % (p) + + return p + + def readAltitude(self, seaLevelPressure=101325): + "Calculates the altitude in meters" + altitude = 0.0 + pressure = float(self.readPressure()) + altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.1903)) + if (self.debug): + print "DBG: Altitude = %d" % (altitude) + return altitude + + return 0 diff --git a/Data Logger/Adafruit_BMP085/Adafruit_BMP085_example.py b/Data Logger/Adafruit_BMP085/Adafruit_BMP085_example.py new file mode 100755 index 0000000..e7143c8 --- /dev/null +++ b/Data Logger/Adafruit_BMP085/Adafruit_BMP085_example.py @@ -0,0 +1,35 @@ +#!/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() + +# Read the current barometric pressure level +pressure = bmp.readPressure() + +# To calculate altitude based on an estimated mean sea level pressure +# (1013.25 hPa) call the function as follows, but this won't be very accurate +altitude = bmp.readAltitude() + +# To specify a more accurate altitude, enter the correct mean sea level +# pressure level. For example, if the current pressure level is 1023.50 hPa +# enter 102350 since we include two decimal places in the integer value +# altitude = bmp.readAltitude(102350) + +print "Temperature: %.2f C" % temp +print "Pressure: %.2f hPa" % (pressure / 100.0) +print "Altitude: %.2f" % altitude diff --git a/Data Logger/Adafruit_BMP085/Adafruit_BMP085_googledocs_ex.py b/Data Logger/Adafruit_BMP085/Adafruit_BMP085_googledocs_ex.py new file mode 100755 index 0000000..4d62cd0 --- /dev/null +++ b/Data Logger/Adafruit_BMP085/Adafruit_BMP085_googledocs_ex.py @@ -0,0 +1,69 @@ +#!/usr/bin/python + +import sys +import time +import datetime +import gspread +from Adafruit_BMP085 import BMP085 + +# =========================================================================== +# Google Account Details +# =========================================================================== + +# Account details for google docs +email = 'you@somewhere.com' +password = '$hhh!' +spreadsheet = 'SpreadsheetName' + +# =========================================================================== +# 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 + +# Login with your Google account +try: + gc = gspread.login(email, password) +except: + print "Unable to log in. Check your email address/password" + sys.exit() + +# Open a worksheet from your spreadsheet using the filename +try: + worksheet = gc.open(spreadsheet).sheet1 + # Alternatively, open a spreadsheet using the spreadsheet's key + # worksheet = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE') +except: + print "Unable to open the spreadsheet. Check your filename: %s" % spreadsheet + sys.exit() + +# Continuously append data +while(True): + 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 + + # Append the data in the spreadsheet, including a timestamp + try: + values = [datetime.datetime.now(), temp, pressure, altitude] + worksheet.append_row(values) + except: + print "Unable to append data. Check your connection?" + sys.exit() + + # Wait 5 seconds before continuing + print "Wrote a row to %s" % spreadsheet + time.sleep(5) + diff --git a/Data Logger/Adafruit_BMP085/Adafruit_I2C.py b/Data Logger/Adafruit_BMP085/Adafruit_I2C.py new file mode 120000 index 0000000..77f0616 --- /dev/null +++ b/Data Logger/Adafruit_BMP085/Adafruit_I2C.py @@ -0,0 +1 @@ +../Adafruit_I2C/Adafruit_I2C.py \ No newline at end of file diff --git a/Data Logger/Adafruit_BMP085/DEPRECATED.txt b/Data Logger/Adafruit_BMP085/DEPRECATED.txt new file mode 100644 index 0000000..4e7dadf --- /dev/null +++ b/Data Logger/Adafruit_BMP085/DEPRECATED.txt @@ -0,0 +1,5 @@ +This BMP085 sensor library has been deprecated in favor of a new version that supports both the Raspberry Pi and Beaglebone Black. + +You can find the new library with installation instructions at this github repository: https://github.com/adafruit/Adafruit_Python_BMP + +You can also find a tutorial on using this library at: https://learn.adafruit.com/using-the-bmp085-with-raspberry-pi/using-the-adafruit-bmp-python-library diff --git a/Data Logger/Adafruit_I2C.py b/Data Logger/Adafruit_I2C.py new file mode 100755 index 0000000..8bed4ed --- /dev/null +++ b/Data Logger/Adafruit_I2C.py @@ -0,0 +1,161 @@ +#!/usr/bin/python +import re +import smbus + +# =========================================================================== +# Adafruit_I2C Class +# =========================================================================== + +class Adafruit_I2C(object): + + @staticmethod + def getPiRevision(): + "Gets the version number of the Raspberry Pi board" + # Revision list available at: http://elinux.org/RPi_HardwareHistory#Board_Revision_History + try: + with open('/proc/cpuinfo', 'r') as infile: + for line in infile: + # Match a line of the form "Revision : 0002" while ignoring extra + # info in front of the revsion (like 1000 when the Pi was over-volted). + match = re.match('Revision\s+:\s+.*(\w{4})$', line) + if match and match.group(1) in ['0000', '0002', '0003']: + # Return revision 1 if revision ends with 0000, 0002 or 0003. + return 1 + elif match: + # Assume revision 2 if revision ends with any other 4 chars. + return 2 + # Couldn't find the revision, assume revision 0 like older code for compatibility. + return 0 + except: + return 0 + + @staticmethod + def getPiI2CBusNumber(): + # Gets the I2C bus number /dev/i2c# + return 1 if Adafruit_I2C.getPiRevision() > 1 else 0 + + def __init__(self, address, busnum=-1, debug=False): + self.address = address + # By default, the correct I2C bus is auto-detected using /proc/cpuinfo + # Alternatively, you can hard-code the bus version below: + # self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's) + # self.bus = smbus.SMBus(1); # Force I2C1 (512MB Pi's) + self.bus = smbus.SMBus(busnum if busnum >= 0 else Adafruit_I2C.getPiI2CBusNumber()) + self.debug = debug + + def reverseByteOrder(self, data): + "Reverses the byte order of an int (16-bit) or long (32-bit) value" + # Courtesy Vishal Sapre + byteCount = len(hex(data)[2:].replace('L','')[::2]) + val = 0 + for i in range(byteCount): + val = (val << 8) | (data & 0xff) + data >>= 8 + return val + + def errMsg(self): + print ("Error accessing 0x%02X: Check your I2C address" % self.address) + return -1 + + def write8(self, reg, value): + "Writes an 8-bit value to the specified register/address" + try: + self.bus.write_byte_data(self.address, reg, value) + if self.debug: + print ("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg)) + except IOError, err: + return self.errMsg() + + def write16(self, reg, value): + "Writes a 16-bit value to the specified register/address pair" + try: + self.bus.write_word_data(self.address, reg, value) + if self.debug: + print ("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" % + (value, reg, reg+1)) + except IOError, err: + return self.errMsg() + + def writeRaw8(self, value): + "Writes an 8-bit value on the bus" + try: + self.bus.write_byte(self.address, value) + if self.debug: + print ("I2C: Wrote 0x%02X" % value) + except IOError, err: + return self.errMsg() + + def writeList(self, reg, list): + "Writes an array of bytes using I2C format" + try: + if self.debug: + print ("I2C: Writing list to register 0x%02X:" % reg) + print list + self.bus.write_i2c_block_data(self.address, reg, list) + except IOError, err: + return self.errMsg() + + def readList(self, reg, length): + "Read a list of bytes from the I2C device" + try: + results = self.bus.read_i2c_block_data(self.address, reg, length) + if self.debug: + print ("I2C: Device 0x%02X returned the following from reg 0x%02X" % + (self.address, reg)) + print results + return results + except IOError, err: + return self.errMsg() + + def readU8(self, reg): + "Read an unsigned byte from the I2C device" + try: + result = self.bus.read_byte_data(self.address, reg) + if self.debug: + print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % + (self.address, result & 0xFF, reg)) + return result + except IOError, err: + return self.errMsg() + + def readS8(self, reg): + "Reads a signed byte from the I2C device" + try: + result = self.bus.read_byte_data(self.address, reg) + if result > 127: result -= 256 + if self.debug: + print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % + (self.address, result & 0xFF, reg)) + return result + except IOError, err: + return self.errMsg() + + def readU16(self, reg, little_endian=True): + "Reads an unsigned 16-bit value from the I2C device" + try: + result = self.bus.read_word_data(self.address,reg) + # Swap bytes if using big endian because read_word_data assumes little + # endian on ARM (little endian) systems. + if not little_endian: + result = ((result << 8) & 0xFF00) + (result >> 8) + if (self.debug): + print ("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)) + return result + except IOError, err: + return self.errMsg() + + def readS16(self, reg, little_endian=True): + "Reads a signed 16-bit value from the I2C device" + try: + result = self.readU16(reg,little_endian) + if result > 32767: result -= 65536 + return result + except IOError, err: + return self.errMsg() + +if __name__ == '__main__': + try: + bus = Adafruit_I2C(address=0) + print ("Default I2C bus is accessible") + except: + print ("Error accessing default I2C bus") diff --git a/Data Logger/Adafruit_I2C.pyc b/Data Logger/Adafruit_I2C.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e1e8490016bdd4163937b1d8b928e164e2ba6ff2 GIT binary patch literal 6035 zcmb_g?|0PJ5go18#%tC#A*}-hPShl{3oRHM3a(EM0RsU}h@o0~Y}}+FPk&7C_lPxgPOCjWD!P-{s3v+?%^ zF0+Bg$A3%OBI|wGl66b`f&>L=La^m~k?%yFtlJV4#V^WMK@wlOCnT`N9}|BZkF9kG zN969k)u+B#mgP@ChPW2#0@X?hi-oW7TS?0hZ~9&{>4a(R{=%(2nk`eapU?!*$)XZa zks3fPD1ck5cMD~pCDw(C1z8`bZWy_Kgj!LUpl%dO)Q!TVKrsqO#V@H)7JpKODe;f0 zFfD#rg&7Hsi9aRZW9j!)o>uv|xYf!GR8+-jPHyfNF7qv#Zd+2_G_?D!Apc&KU6B{} z<)z5&Z*I!2C8;Hw*oZAJEsoeN$V-^C%61bbEpoF^W^0_CxUKT>X{ug*dRkVYd36<9 z|9NdSnowh-V+|eE3QcR~(cBGE7V{j>@X zsH*BKYA7{CMNPm7T&^_GU8P^Jf%h#Z?KenVf3N|M^L33pWbapai1gx5j<*#vvz3M<*R2t>-p^Zg_+M!_@N z^-d;Lzk+Tq9*avS#l#}LXMRxcvv-n1JAqP8x%&V+=)n_mf$$ZY%+54_8mjpX)uaWUD@uF5xp*`H=Xq0b0Sd=o5KHx z3q|G{mRL=~6g~!zTBPeZ>N!ptYjQ9NyBW^NqwDc*&#mYmfjhHGY~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