Commit c748fe27 authored by Tom Hartley's avatar Tom Hartley

modular sensors

parent 5838bc61
[AirPi] [AirPi]
Watchdog = False Watchdog = False
I2CBus = 0 I2CBus = 0
LCD = True LCD = False
Debug = True Debug = True
NetRestart = True NetRestart = True
NetRetries = 15 NetRetries = 15
Frequency = 2 Frequency = 5
[Cosm] [Cosm]
Enabled = False Enabled = True
APIKEY = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx APIKEY = xgDrTMnzVPy4otPauOVQqByN6n-SAKxHV1F0VzduMS90az0g
FEEDID = xxxxxx FEEDID = 130835
[ADC] [ADC]
ADC = MCP3008 ADC = MCP3008
...@@ -25,11 +25,11 @@ DHTPin = 4 ...@@ -25,11 +25,11 @@ DHTPin = 4
[BMP085] [BMP085]
Enabled = True Enabled = True
Altitude = 35 Altitude = 43
MSLP = True MSLP = True
[UVI-01] [UVI-01]
Enabled = True Enabled = False
ADCPin = 4 ADCPin = 4
[LDR] [LDR]
...@@ -37,18 +37,18 @@ Enabled = True ...@@ -37,18 +37,18 @@ Enabled = True
ADCPin = 0 ADCPin = 0
[TGS2600] [TGS2600]
Enabled = True Enabled = False
PullUp = 22000 PullUp = 22000
ADCPin = 1 ADCPin = 1
[MICS-2710] [MICS-2710]
Enabled = True Enabled = False
ADCPin = 2 ADCPin = 2
PullUp = 10000 PullUp = 10000
Resistance = 90000 Resistance = 90000
[MICS-5525] [MICS-5525]
Enabled = True Enabled = False
ADCPin = 3 ADCPin = 3
PullUp = 100000 PullUp = 100000
Resistance = 190000 Resistance = 190000
...@@ -13,6 +13,8 @@ from interfaces.DHT22 import DHT22 ...@@ -13,6 +13,8 @@ from interfaces.DHT22 import DHT22
from interfaces.BMP085 import BMP085 from interfaces.BMP085 import BMP085
from interfaces.MCP3008 import MCP3008, MCP3208, AQSensor, LightSensor from interfaces.MCP3008 import MCP3008, MCP3208, AQSensor, LightSensor
from interfaces.PiPlate import Adafruit_CharLCDPlate from interfaces.PiPlate import Adafruit_CharLCDPlate
import thinkspeak
import curses import curses
class DataPoint(): class DataPoint():
...@@ -139,6 +141,7 @@ def mainUpload(stdscr): ...@@ -139,6 +141,7 @@ def mainUpload(stdscr):
API_URL = '/v2/feeds/{feednum}.xml' .format(feednum=FEED) API_URL = '/v2/feeds/{feednum}.xml' .format(feednum=FEED)
failCount = 0 failCount = 0
currentDisplay = 0 currentDisplay = 0
ts = thinkspeak.thinkspeak()
# Continuously append data # Continuously append data
while(True): while(True):
...@@ -184,16 +187,19 @@ def mainUpload(stdscr): ...@@ -184,16 +187,19 @@ def mainUpload(stdscr):
if LOGGER: if LOGGER:
#Attempt to submit the data to cosm #Attempt to submit the data to cosm
try: try:
pac = eeml.datastream.Cosm(API_URL, API_KEY) # pd: handle thinkspeak stuff as well
for dp in datas: #ts.send(datas)
if dp.uploadID!=-1: ts.pisend(datas)
pac.update([eeml.Data(dp.uploadID, dp.roundedValue())]) #pac = eeml.datastream.Cosm(API_URL, API_KEY)
pac.put() #for dp in datas:
# if dp.uploadID!=-1:
# pac.update([eeml.Data(dp.uploadID, dp.roundedValue())])
#pac.put()
if stdscr == None: if stdscr == None:
print "Uploaded data at " + str(datetime.datetime.now()) print "Uploaded data at " + str(datetime.datetime.now())
GPIO.output(22, True) GPIO.output(22, True)
if LCDENABLED: #if LCDENABLED:
lcd.backlight(lcd.GREEN) #lcd.backlight(lcd.GREEN)
failCount = 0 failCount = 0
except KeyboardInterrupt: except KeyboardInterrupt:
raise raise
...@@ -206,6 +212,7 @@ def mainUpload(stdscr): ...@@ -206,6 +212,7 @@ def mainUpload(stdscr):
failCount+=1 failCount+=1
if failCount>NETRETRIES: if failCount>NETRETRIES:
subprocess.Popen(["sudo", "/etc/init.d/networking", "restart"]) subprocess.Popen(["sudo", "/etc/init.d/networking", "restart"])
subprocess.Popen(["sudo", "/sbin/dhclient", "eth0"])
failCount=0 failCount=0
if LCDENABLED: if LCDENABLED:
...@@ -214,9 +221,12 @@ def mainUpload(stdscr): ...@@ -214,9 +221,12 @@ def mainUpload(stdscr):
if a.sName!=None: if a.sName!=None:
usedValues.append(a) usedValues.append(a)
data1 = usedValues[currentDisplay*2] data1 = usedValues[currentDisplay*2]
data2 = usedValues[currentDisplay*2 + 1]
lcd.clear() lcd.clear()
lcd.message(data1.sName + ": " + data1.roundedValue() + " " + data1.unit + "\n" + data2.sName + ": " + data2.roundedValue() + " " + data2.unit) if currentDisplay < 3:
data2 = usedValues[currentDisplay*2 + 1]
lcd.message(data1.sName + ": " + data1.roundedValue() + " " + data1.unit + "\n" + data2.sName + ": " + data2.roundedValue() + " " + data2.unit)
else:
lcd.message(data1.sName + ": " + data1.roundedValue() + " " + data1.unit + "\n")
sleep(FREQUENCY-1) sleep(FREQUENCY-1)
GPIO.output(22, False) GPIO.output(22, False)
GPIO.output(21, False) GPIO.output(21, False)
......
...@@ -2,32 +2,88 @@ ...@@ -2,32 +2,88 @@
import ConfigParser import ConfigParser
import time import time
import importlib
import inspect import inspect
import os
from sys import exit from sys import exit
from sensors import sensor
def get_subclasses(mod,cls): def get_subclasses(mod,cls):
for name, obj in inspect.getmembers(mod): for name, obj in inspect.getmembers(mod):
if hasattr(obj, "__bases__" and cls in obj.__bases__: if hasattr(obj, "__bases__") and cls in obj.__bases__:
return obj return obj
sensorPlugins = []
if not os.path.isfile('sensors.cfg'): if not os.path.isfile('sensors.cfg'):
print "Unable to access config file: sensors.cfg" print "Unable to access config file: sensors.cfg"
exit(1) exit(1)
sensorConfig = ConfigParser.SafeConfigParser() sensorConfig = ConfigParser.SafeConfigParser()
sensorConfig.read('sensors.cfg') sensorConfig.read('sensors.cfg')
sensorNames = sensorConfig.sections() sensorNames = sensorConfig.sections()
sensorPlugins = []
for i in sensorNames: for i in sensorNames:
filename = sensorConfig.get(i,"filename") try:
enabled = sensorConfig.getboolean(i,"enabled") try:
#if enabled, load the plugin filename = sensorConfig.get(i,"filename")
try: except:
mod = importlib.import_module(filename,"sensors") print("Error: no filename config option found for " + i)
except: #add specific exception for missing module raise
print("Could not import module " + filename") try:
enabled = sensorConfig.getboolean(i,"enabled")
except:
enabled = True
#if enabled, load the plugin
if enabled:
try:
mod = __import__('sensors.'+filename,fromlist=['a']) #Why does this work?
except:
print("Error: could not import module " + filename)
raise
try:
sensorClass = get_subclasses(mod,sensor.Sensor)
if sensorClass == None:
raise AttributeError
except:
print("Error: could not find a subclass of sensor.Sensor in module " + filename)
raise
try:
reqd = sensorClass.requiredData
except:
reqd = []
try:
opt = sensorClass.optionalData
except:
opt = []
pluginData = {}
class MissingField(Exception): pass
for requiredField in reqd:
if sensorConfig.has_option(i,requiredField):
pluginData[requiredField]=sensorConfig.get(i,requiredField)
else:
print "Error: Missing required field '" + requiredField + "' for sensor plugin " + i
raise MissingField
for optionalField in opt:
if sensorConfig.has_option(i,optionalField):
pluginData[optionalField]=sensorConfig.get(i,optionalField)
instClass = sensorClass(pluginData)
sensorPlugins.append(instClass)
print ("Success: Loaded sensor plugin " + i)
except Exception as e: #add specific exception for missing module
print("Error: Did not import sensor plugin " + i )
raise e
if not os.path.isfile("outputs.cfg"):
print "Unable to access config file: outputs.cfg"
outputConfig = ConfigParser.SafeConfigParser()
outputConfig.read("outputs.cfg")
[BMP085-temp]
filename=bmp085
enabled=on
measurement=temp
i2cport = 1
mslp=on
altitude=40
import sensor import sensor
requiredData = ["Metric","I2CPort"] class Bmp085(sensor.Sensor):
optionalData = ["Altitude","MLSP",] requiredData = ["measurement","i2cport"]
optionalData = ["altitude","mslp","unit"]
class Bmp085(Sensor): def __init__(self,data):
def __init__(data): print data
if "temp" in data["measurement"].lower():
self.valName = "Temperature"
self.valUnit = "C"
if "unit" in data:
if data["unit"]=="F":
self.valUnit = "F"
self.sigfigs = 3
elif "pres" in data["measurement"].lower():
self.valName = "Pressure"
self.valUnit = "hPa"
self.sigfigs = 3
self._altitude = 0
if "mslp" in data:
if data["mslp"].lower in ["on","true","1","yes"]:
if "altitude" in data:
self.altitude=data["altitude"]
return return
def getVal(): def getVal(self):
return 0 return 0
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