Commit c748fe27 authored by Tom Hartley's avatar Tom Hartley

modular sensors

parent 5838bc61
[AirPi]
Watchdog = False
I2CBus = 0
LCD = True
LCD = False
Debug = True
NetRestart = True
NetRetries = 15
Frequency = 2
Frequency = 5
[Cosm]
Enabled = False
APIKEY = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
FEEDID = xxxxxx
Enabled = True
APIKEY = xgDrTMnzVPy4otPauOVQqByN6n-SAKxHV1F0VzduMS90az0g
FEEDID = 130835
[ADC]
ADC = MCP3008
......@@ -25,11 +25,11 @@ DHTPin = 4
[BMP085]
Enabled = True
Altitude = 35
Altitude = 43
MSLP = True
[UVI-01]
Enabled = True
Enabled = False
ADCPin = 4
[LDR]
......@@ -37,18 +37,18 @@ Enabled = True
ADCPin = 0
[TGS2600]
Enabled = True
Enabled = False
PullUp = 22000
ADCPin = 1
[MICS-2710]
Enabled = True
Enabled = False
ADCPin = 2
PullUp = 10000
Resistance = 90000
[MICS-5525]
Enabled = True
Enabled = False
ADCPin = 3
PullUp = 100000
Resistance = 190000
......@@ -13,6 +13,8 @@ from interfaces.DHT22 import DHT22
from interfaces.BMP085 import BMP085
from interfaces.MCP3008 import MCP3008, MCP3208, AQSensor, LightSensor
from interfaces.PiPlate import Adafruit_CharLCDPlate
import thinkspeak
import curses
class DataPoint():
......@@ -139,6 +141,7 @@ def mainUpload(stdscr):
API_URL = '/v2/feeds/{feednum}.xml' .format(feednum=FEED)
failCount = 0
currentDisplay = 0
ts = thinkspeak.thinkspeak()
# Continuously append data
while(True):
......@@ -184,16 +187,19 @@ def mainUpload(stdscr):
if LOGGER:
#Attempt to submit the data to cosm
try:
pac = eeml.datastream.Cosm(API_URL, API_KEY)
for dp in datas:
if dp.uploadID!=-1:
pac.update([eeml.Data(dp.uploadID, dp.roundedValue())])
pac.put()
# pd: handle thinkspeak stuff as well
#ts.send(datas)
ts.pisend(datas)
#pac = eeml.datastream.Cosm(API_URL, API_KEY)
#for dp in datas:
# if dp.uploadID!=-1:
# pac.update([eeml.Data(dp.uploadID, dp.roundedValue())])
#pac.put()
if stdscr == None:
print "Uploaded data at " + str(datetime.datetime.now())
GPIO.output(22, True)
if LCDENABLED:
lcd.backlight(lcd.GREEN)
#if LCDENABLED:
#lcd.backlight(lcd.GREEN)
failCount = 0
except KeyboardInterrupt:
raise
......@@ -206,6 +212,7 @@ def mainUpload(stdscr):
failCount+=1
if failCount>NETRETRIES:
subprocess.Popen(["sudo", "/etc/init.d/networking", "restart"])
subprocess.Popen(["sudo", "/sbin/dhclient", "eth0"])
failCount=0
if LCDENABLED:
......@@ -214,9 +221,12 @@ def mainUpload(stdscr):
if a.sName!=None:
usedValues.append(a)
data1 = usedValues[currentDisplay*2]
data2 = usedValues[currentDisplay*2 + 1]
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)
GPIO.output(22, False)
GPIO.output(21, False)
......
......@@ -2,32 +2,88 @@
import ConfigParser
import time
import importlib
import inspect
import os
from sys import exit
from sensors import sensor
def get_subclasses(mod,cls):
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
sensorPlugins = []
if not os.path.isfile('sensors.cfg'):
print "Unable to access config file: sensors.cfg"
exit(1)
sensorConfig = ConfigParser.SafeConfigParser()
sensorConfig.read('sensors.cfg')
sensorNames = sensorConfig.sections()
sensorPlugins = []
for i in sensorNames:
filename = sensorConfig.get(i,"filename")
enabled = sensorConfig.getboolean(i,"enabled")
#if enabled, load the plugin
try:
mod = importlib.import_module(filename,"sensors")
except: #add specific exception for missing module
print("Could not import module " + filename")
try:
try:
filename = sensorConfig.get(i,"filename")
except:
print("Error: no filename config option found for " + i)
raise
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
requiredData = ["Metric","I2CPort"]
optionalData = ["Altitude","MLSP",]
class Bmp085(Sensor):
def __init__(data):
class Bmp085(sensor.Sensor):
requiredData = ["measurement","i2cport"]
optionalData = ["altitude","mslp","unit"]
def __init__(self,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
def getVal():
def getVal(self):
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