Commit 8183487a authored by Tom Hartley's avatar Tom Hartley

Fixed Xively, BMP085 plugins. Also added an analogue.py plugin which can be...

Fixed Xively, BMP085 plugins. Also added an analogue.py plugin which can be instantiated for analogue sensors (LDR, gas sensors, etc.)
parent c9aec073
......@@ -120,7 +120,6 @@ for i in outputNames:
except:
print("Error: could not find a subclass of output.Output in module " + filename)
raise
try:
reqd = outputClass.requiredData
except:
......@@ -129,7 +128,12 @@ for i in outputNames:
opt = outputClass.optionalData
except:
opt = []
if outputConfig.has_option(i,"async"):
async = outputConfig.getbool(i,"async")
else:
async = False
pluginData = {}
class MissingField(Exception): pass
......@@ -144,6 +148,7 @@ for i in outputNames:
if outputConfig.has_option(i,optionalField):
pluginData[optionalField]=outputConfig.get(i,optionalField)
instClass = outputClass(pluginData)
instClass.async = async
outputPlugins.append(instClass)
print ("Success: Loaded output plugin " + i)
except Exception as e: #add specific exception for missing module
......@@ -168,7 +173,7 @@ while True:
for i in sensorPlugins:
dataDict = {}
val = i.getVal()
if val==False: #this means it has no data to upload.
if val==None: #this means it has no data to upload.
continue
dataDict["value"] = i.getVal()
dataDict["unit"] = i.valUnit
......
......@@ -6,4 +6,4 @@ enabled=on
filename=xively
enabled=on
APIKey=NrhKmfED1QR8kMaPOfHPR7Rw84gT1eZdixknEotpfy7ckH2Z
FeedID=1530795102
\ No newline at end of file
FeedID=1530795102
......@@ -7,7 +7,8 @@ class Print(output.Output):
def __init__(self,data):
pass
def outputData(self,dataPoints):
print ""
print "Time: " + str(datetime.datetime.now())
for i in dataPoints:
print i["name"] + ": " + str(i["value"]) + " " + i["symbol"]
return True
\ No newline at end of file
return True
......@@ -14,7 +14,10 @@ class Xively(output.Output):
arr.append({"id":i["name"],"current_value":i["value"]})
a = json.dumps({"version":"1.0.0","datastreams":arr})
try:
requests.put("https://api.xively.com/v2/feeds/"+self.FeedID+".json",headers={"X-ApiKey":self.APIKey},data=a)
z = requests.put("https://api.xively.com/v2/feeds/"+self.FeedID+".json",headers={"X-ApiKey":self.APIKey},data=a)
if z.text!="":
print "Xively Error: " + z.text
return False
except ConnectionError, e:
return False
return True
\ No newline at end of file
return True
......@@ -10,4 +10,24 @@ enabled=on
measurement=pres
mslp=on
i2cbus = 1
altitude=40
\ No newline at end of file
altitude=40
[MCP3008]
filename=mcp3008
enabled=on
[LDR]
filename=analogue
enabled=on
pullUpResistance=2200
measurement=Light_Level
adcPin = 0
sensorName = LDR
[MQ2]
filename=analogue
enabled=on
pullDownResistance=22000
measurement=Smoke_Level
adcPin=1
sensorName=MQ2
import mcp3008
import sensor
class Analogue(sensor.Sensor):
requiredData = ["adcPin","measurement","sensorName"]
optionalData = ["pullUpResistance","pullDownResistance"]
def __init__(self, data):
self.adc = mcp3008.MCP3008.sharedClass
self.adcPin = int(data["adcPin"])
self.valName = data["measurement"]
self.sensorName = data["sensorName"]
self.pullUp, self.pullDown = None, None
if "pullUpResistance" in data:
self.pullUp = int(data["pullUpResistance"])
if "pullDownResistance" in data:
self.pullDown = int(data["pullDownResistance"])
class ConfigError(Exception): pass
if self.pullUp!=None and self.pullDown!=None:
print "Please choose whether there is a pull up or pull down resistor for the " + self.valName + " measurement by only entering one of them into the settings file"
raise ConfigError
if self.pullUp==None and self.pullDown==None:
print "Please enter a pull up or pull down resistor for the " + self.valName + " measurement by adding either a 'pullUpResistance' or a 'pullDownResistance' config setting."
raise ConfigError
self.valUnit = "Ohms"
self.valSymbol = "Ohms"
def getVal(self):
result = self.adc.readADC(self.adcPin)
if result==0:
print "Check wiring for the " + self.valName + " measurement, no input voltage detected on ADC input " + str(self.adcPin)
return None
if result == 1023:
print "Check wiring for the " + self.valName + " measurement, full input voltage detected on ADC input " + str(self.adcPin)
return None
vin = 3.3
vout = float(result)/1023 * vin
if self.pullDown!=None:
#Its a pull down resistor
resOut = (self.pullDown*vin)/vout - self.pullDown
else:
resOut = self.pullUp/((vin/vout)-1)
return resOut
import sensor
import bmpBackend
class Bmp085(sensor.Sensor):
class BMP085(sensor.Sensor):
bmpClass = None
requiredData = ["measurement","i2cbus"]
optionalData = ["altitude","mslp","unit"]
......@@ -11,15 +12,6 @@ class Bmp085(sensor.Sensor):
self.valName = "Temperature"
self.valUnit = "Celsius"
self.valSymbol = "C"
self.mslp = False
if "mslp" in data:
self.mslp = data["mslp"]
if self.mslp:
if "altitude" in data:
self.altitude = data["altitude"]
else:
print "To calculate MSLP, please provide an 'altitude' config setting for the BMP085 (in m)"
self.mslp = False
if "unit" in data:
if data["unit"]=="F":
self.valUnit = "Farenheight"
......@@ -28,23 +20,28 @@ class Bmp085(sensor.Sensor):
self.valName = "Pressure"
self.valSymbol = "hPa"
self.valUnit = "Hectopascal"
self._altitude = 0
self.altitude = 0
self.mslp = False
if "mslp" in data:
if data["mslp"].lower in ["on","true","1","yes"]:
self.mslp = True
if "altitude" in data:
self.altitude=data["altitude"]
if (!bmpClass):
bmpClass = bmpBackend.BMP085(bus=bus)
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"]))
return
def getVal(self):
if self.valName = "Temperature":
temp = bmpClass.readTemperature()
if self.valUnit = "Fahrenheit":
if self.valName == "Temperature":
temp = BMP085.bmpClass.readTemperature()
if self.valUnit == "Fahrenheit":
temp = temp * 1.8 + 32
return temp
elif self.valName = "Pressure":
elif self.valName == "Pressure":
if self.mslp:
return self.bmpClass.readMSLPressure(self.altitude) * 0.01 #to convert to Hectopascals
return BMP085.bmpClass.readMSLPressure(self.altitude) * 0.01 #to convert to Hectopascals
else:
return self.bmpClass.readPressure() * 0.01 #to convert to Hectopascals
\ No newline at end of file
return BMP085.bmpClass.readPressure() * 0.01 #to convert to Hectopascals
#!/usr/bin/python
import RPi.GPIO as GPIO
import sensor
class MCP3008(sensor.Sensor):
requiredData = []
optionalData = ["mosiPin","misoPin","csPin","clkPin"]
sharedClass = None
def __init__(self, data):
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
self.SPIMOSI = 23
self.SPIMISO = 24
self.SPICLK = 18
self.SPICS = 25
if "mosiPin" in data:
self.SPIMOSI = data["mosiPin"]
if "misoPin" in data:
self.SPIMISO = data["misoPin"]
if "clkPin" in data:
self.SPICLK = data["clkPin"]
if "csPin" in data:
self.SPICS = data["csPin"]
GPIO.setup(self.SPIMOSI, GPIO.OUT)
GPIO.setup(self.SPIMISO, GPIO.IN)
GPIO.setup(self.SPICLK, GPIO.OUT)
GPIO.setup(self.SPICS, GPIO.OUT)
if MCP3008.sharedClass == None:
MCP3008.sharedClass = self
#read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readADC(self,adcnum):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(self.SPICS, True)
GPIO.output(self.SPICLK, False) # start clock low
GPIO.output(self.SPICS, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(self.SPIMOSI, True)
else:
GPIO.output(self.SPIMOSI, False)
commandout <<= 1
GPIO.output(self.SPICLK, True)
GPIO.output(self.SPICLK, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(11):
GPIO.output(self.SPICLK, True)
GPIO.output(self.SPICLK, False)
adcout <<= 1
if (GPIO.input(self.SPIMISO)):
adcout |= 0x1
GPIO.output(self.SPICS, True)
return adcout
def getVal(self):
return None #not that kind of plugin, this is to be used by other plugins
[Main]
uploadDelay = 5 ;how long to wait before uploading again
\ No newline at end of file
uploadDelay = 2 ;how long to wait before uploading again
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment