Commit 14221979 authored by Tom Hartley's avatar Tom Hartley

Added watchdog timer, preliminary curses support

parent 64c8625b
......@@ -5,142 +5,151 @@ import datetime
import eeml
import subprocess, os, sys
import RPi.GPIO as GPIO
from asciigraph import *
from interfaces.DHT22 import DHT22
from interfaces.BMP085 import BMP085
from interfaces.MCP3008 import MCP3008, AQSensor, LightSensor
from interfaces.PiPlate import Adafruit_CharLCDPlate
import curses
lcd = Adafruit_CharLCDPlate.Adafruit_CharLCDPlate()
lcd.clear()
lcd.backlight(lcd.ON)
lcd.message(" AirPi\n by Alyssa & Tom")
sleep(2)
lcd.clear()
lcd.message("Air Quality and \nWeather Station")
os.chdir(os.path.dirname(sys.argv[0]))
class DataPoint():
def __init__(self,value,name,unit,decimalplaces,uploadID,shortName=None):
self.value = value
self.name = name
self.unit = unit
self.decimals = decimalplaces
self.uploadID = uploadID
self.sName = shortName
def roundedValue(self):
formatString = '{0:.' + str(self.decimals) + 'f}'
return formatString.format(self.value)
DEBUG = 1
LOGGER = 1
DHTPin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(21,GPIO.OUT)
BMP085Address = 0x77
SPIMOSI = 23
SPIMISO = 24
SPICLK = 18
SPICS = 25
AQADC = 1
LightADC = 0
NO2ADC = 2
COADC = 3
UVADC = 4
dht = DHT22.DHT22(DHTPin)
bmp = BMP085.BMP085(bus=bus)
adc = MCP3008.MCP3008(SPIMOSI,SPIMISO,SPICLK,SPICS)
airSensor = AQSensor.AQSensor(adc,AQADC,22000)
lightSensor = LightSensor.LightSensor(adc,LightADC)
uvSensor = LightSensor.LightSensor(adc,UVADC)
no2Sensor = AQSensor.AQSensor(adc,NO2ADC,90000,10000)
coSensor = AQSensor.AQSensor(adc,COADC,190000,100000)
API_KEY = 'AaBeQoyPHcnC8rwEN2YJJbEKrJOSAKxBa0hEN08rblZUZz0g'
FEED = 85080
API_URL = '/v2/feeds/{feednum}.xml' .format(feednum=FEED)
failCount = 0
currentDisplay = 0
# Continuously append data
while(True):
dht.get_data()
temp = dht.temp()
humidity = dht.humidity()
aq = airSensor.get_quality()
lightlevels = lightSensor.get_light_level()
uvlevels = uvSensor.get_uv_level()
bmptemp = bmp.readTemperature()
pressure = bmp.readPressure()
altitude = bmp.readAltitude()
NO2 = no2Sensor.get_NO2()
CO = coSensor.get_CO()
NO2res = no2Sensor.get_quality()
COres = coSensor.get_quality()
if DEBUG:
if temp!=False:
print "Temp-DHT: %.1f C" % temp
print "Humidity: %.1f %%" % humidity
else: print "Failed to read DHT22"
print "Air Quality: %.2f Ohms" % aq
print "Light Level: %.2f Lux" % lightlevels
print "UV Level: %.2f UVI" % uvlevels
print "Pressure: %.1f Pa" % pressure
print "Temp-BMP: %.1f C" % bmptemp
print "Altitude: %.1f m" % altitude
print "NO2: %.3f ppm" % NO2
print "CO: %.3f ppm" % CO
print "NO2 ohms: %.1f Ohms" % NO2res
print "CO ohms: %.1f Ohms" % COres
aqfor = "%.1f" % aq
COfor = "%.3f" % CO
NO2for = "%.3f" % NO2
NO2resfor = str(int(NO2res))
COresfor = str(int(COres))
lightlevelsfor = "%.2f" % lightlevels
uvlevelsfor = "%.2f" % uvlevels
if LOGGER:
#Attempt to submit the data to cosm
def mainUpload(stdscr):
wdog = os.open('/dev/watchdog',os.O_RDWR)
try:
bus = 0
LCDENABLED = 1
DEBUG = 1
LOGGER = 1
if LCDENABLED:
lcd = Adafruit_CharLCDPlate.Adafruit_CharLCDPlate(busnum=bus)
lcd.clear()
lcd.backlight(lcd.ON)
lcd.message(" AirPi\n by Alyssa & Tom")
sleep(2)
lcd.clear()
lcd.message("Air Quality and \nWeather Station")
try:
pac = eeml.Pachube(API_URL, API_KEY)
pac.update([eeml.Data(0, bmptemp)])
if temp!=False:
pac.update([eeml.Data(1, humidity)])
pac.update([eeml.Data(2, aqfor)])
pac.update([eeml.Data(3, lightlevelsfor)])
pac.update([eeml.Data(4, pressure)])
pac.update([eeml.Data(5, COfor)])
pac.update([eeml.Data(6, NO2for)])
pac.update([eeml.Data(7, COresfor)])
pac.update([eeml.Data(8, NO2resfor)])
pac.update([eeml.Data(9, uvlevelsfor)])
pac.put()
print "Uploaded data at " + str(datetime.datetime.now())
GPIO.output(22, True)
lcd.backlight(lcd.GREEN)
failCount = 0
os.chdir(os.path.dirname(sys.argv[0]))
except:
GPIO.output(21, True)
lcd.backlight(lcd.ON)
print "Unable to upload data at " + str(datetime.datetime.now()) + ". Check your connection?"
failCount+=1
if failCount>23:
subprocess.call(["sudo", "/etc/init.d/networking", "restart"])
failCount=0
lcd.clear()
if (currentDisplay == 0):
lcd.message("Temp: %.1fC\nHumidity: %.1f%%" % (bmptemp,humidity))
elif (currentDisplay == 1):
lcd.message("Pres: %.2f hPa\nSmoke: %.2f"%(pressure/100.0,aq/1000))
elif (currentDisplay == 2):
lcd.message("UV: %.2f UVI\nLight: %.2f lx"%(uvlevels,lightlevels))
else:
lcd.message("NO2: %.2f ppm\nCO: %.2f ppm"%(NO2,CO))
#lcd.message("Temp: %.1fC"%bmptemp)
sleep(1.5)
GPIO.output(22, False)
GPIO.output(21, False)
currentDisplay+=1
if currentDisplay == 4:
pass
DHTPin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(21,GPIO.OUT)
SPIMOSI = 23
SPIMISO = 24
SPICLK = 18
SPICS = 25
AQADC = 1
LightADC = 0
NO2ADC = 2
COADC = 3
UVADC = 4
dht = DHT22.DHT22(DHTPin)
bmp = BMP085.BMP085()
adc = MCP3008.MCP3008(SPIMOSI,SPIMISO,SPICLK,SPICS)
airSensor = AQSensor.AQSensor(adc,AQADC,22000)
lightSensor = LightSensor.LightSensor(adc,LightADC)
uvSensor = LightSensor.LightSensor(adc,UVADC)
no2Sensor = AQSensor.AQSensor(adc,NO2ADC,90000,10000)
coSensor = AQSensor.AQSensor(adc,COADC,190000,100000)
API_KEY = 'AaBeQoyPHcnC8rwEN2YJJbEKrJOSAKxBa0hEN08rblZUZz0g'
FEED = 85080
API_URL = '/v2/feeds/{feednum}.xml' .format(feednum=FEED)
failCount = 0
currentDisplay = 0
#for a in range(0,5):
# time.sleep(0.2)
# GPIO.output(22, False)
# GPIO.output(21, False)
# #dht.get_data() #constantly get data from DHT, but only save it if it validates.
# Continuously append data
while(True):
os.write(wdog,"0")
datas = []
dht.get_data()
d = DataPoint(dht.temp(),"Temp-DHT","C",1,-1)
if d.value != False:
datas.append(d)
datas.append(DataPoint(dht.humidity(),"Humidity","%",1,1,"Humidity"))
datas.append(DataPoint(airSensor.get_quality(),"Air Quality"," ",2,2,"AQ"))
datas.append(DataPoint(lightSensor.get_light_level(),"Light Level","Lux",2,3,"Light"))
datas.append(DataPoint(uvSensor.get_uv_level(),"UV Level","UVI",2,9,"UV"))
datas.append(DataPoint(bmp.readTemperature(),"Temp-BMP","C",1,0,"Temp"))
datas.append(DataPoint(bmp.readPressure(),"Pressure","Pa",1,4,"Pres"))
datas.append(DataPoint(bmp.readAltitude(),"Altitude","m",1,-1))
datas.append(DataPoint(no2Sensor.get_NO2(),"NO2","ppm",3,6,"NO2"))
datas.append(DataPoint(coSensor.get_CO(),"CO","ppm",3,5,"CO"))
datas.append(DataPoint(no2Sensor.get_quality(),"NO2 ohms","ohms",1,8))
datas.append(DataPoint(coSensor.get_quality(),"CO ohms","ohms",1,7))
if DEBUG:
for dp in datas:
print dp.name + ":\t" + dp.roundedValue() + " " + dp.unit
if LOGGER:
#Attempt to submit the data to cosm
try:
pac = eeml.Pachube(API_URL, API_KEY)
for dp in datas:
if dp.uploadID!=-1:
pac.update([eeml.Data(dp.uploadID, dp.roundedValue())])
pac.put()
print "Uploaded data at " + str(datetime.datetime.now())
GPIO.output(22, True)
if LCDENABLED:
lcd.backlight(lcd.GREEN)
failCount = 0
except KeyboardInterrupt:
raise
except:
GPIO.output(21, True)
if LCDENABLED:
lcd.backlight(lcd.ON)
print "Unable to upload data at " + str(datetime.datetime.now()) + ". Check your connection?"
failCount+=1
if failCount>3:
subprocess.Popen(["sudo", "/etc/init.d/networking", "restart"])
failCount=0
if LCDENABLED:
usedValues = []
for a in datas:
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)
sleep(1.5)
GPIO.output(22, False)
GPIO.output(21, False)
currentDisplay+=1
if currentDisplay == 4:
currentDisplay = 0
except KeyboardInterrupt:
os.write(wdog,"V")
os.close(wdog)
raise
cursesEnabled = 0
if cursesEnabled:
curses.wrapper(mainUpload)
else:
mainUpload(None)
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