bmp085.py 1.42 KB
Newer Older
1
import sensor
2
import bmpBackend
3

4
class BMP085(sensor.Sensor):
5 6
	bmpClass = None
	requiredData = ["measurement","i2cbus"]
7 8 9 10 11
	optionalData = ["altitude","mslp","unit"]
	def __init__(self,data):
		self.sensorName = "BMP085"
		if "temp" in data["measurement"].lower():
			self.valName = "Temperature"
12 13
			self.valUnit = "Celsius"
			self.valSymbol = "C"
14 15
			if "unit" in data:
				if data["unit"]=="F":
Tom Hartley's avatar
Tom Hartley committed
16
					self.valUnit = "Fahrenheit"
17
					self.valSymbol = "F"
18 19
		elif "pres" in data["measurement"].lower():
			self.valName = "Pressure"
20 21
			self.valSymbol = "hPa"
			self.valUnit = "Hectopascal"
22 23
			self.altitude = 0
			self.mslp = False
24 25
			if "mslp" in data:
				if data["mslp"].lower in ["on","true","1","yes"]:
26
					self.mslp = True
27 28
					if "altitude" in data:
						self.altitude=data["altitude"]
29 30 31 32 33
					else:
						print "To calculate MSLP, please provide an 'altitude' config setting (in m) for the BMP085 pressure module"
						self.mslp = False
		if (BMP085.bmpClass==None):
			BMP085.bmpClass = bmpBackend.BMP085(bus=int(data["i2cbus"]))
34 35 36
		return

	def getVal(self):
37 38 39
		if self.valName == "Temperature":
			temp = BMP085.bmpClass.readTemperature()
			if self.valUnit == "Fahrenheit":
40 41
				temp = temp * 1.8 + 32
			return temp
42
		elif self.valName == "Pressure":
43
			if self.mslp:
44
				return BMP085.bmpClass.readMSLPressure(self.altitude) * 0.01 #to convert to Hectopascals
45
			else:
46
				return BMP085.bmpClass.readPressure() * 0.01 #to convert to Hectopascals