Commit 64c8625b authored by Tom Hartley's avatar Tom Hartley

Added UV sensor

parent 8b861380
#!/usr/bin/python
import time
from time import sleep
import datetime
import eeml
import subprocess, os, sys
......@@ -8,13 +8,20 @@ import RPi.GPIO as GPIO
from interfaces.DHT22 import DHT22
from interfaces.BMP085 import BMP085
from interfaces.MCP3008 import MCP3008, AQSensor, LightSensor
from interfaces.PiPlate import Adafruit_CharLCDPlate
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]))
DEBUG = 1
LOGGER = 1
DHTPin = 4
GPIO.setmode(GPIO.BCM)
......@@ -32,20 +39,24 @@ AQADC = 1
LightADC = 0
NO2ADC = 2
COADC = 3
UVADC = 4
dht = DHT22.DHT22(DHTPin)
bmp = BMP085.BMP085()
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()
......@@ -53,6 +64,7 @@ while(True):
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()
......@@ -62,24 +74,26 @@ while(True):
COres = coSensor.get_quality()
if DEBUG:
if temp!=False:
#print "Temperature: %.1f C" % temp
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 "Temperature: %.1f C" % bmptemp
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
aq = "%.1f" % aq
CO = "%.3f" % CO
NO2 = "%.3f" % NO2
NO2res = str(int(NO2res))
COres = str(int(COres))
lightlevels = "%.2f" % lightlevels
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
......@@ -88,27 +102,43 @@ while(True):
pac.update([eeml.Data(0, bmptemp)])
if temp!=False:
pac.update([eeml.Data(1, humidity)])
pac.update([eeml.Data(2, aq)])
pac.update([eeml.Data(3, lightlevels)])
pac.update([eeml.Data(2, aqfor)])
pac.update([eeml.Data(3, lightlevelsfor)])
pac.update([eeml.Data(4, pressure)])
pac.update([eeml.Data(5, CO)])
pac.update([eeml.Data(6, NO2)])
pac.update([eeml.Data(7, COres)])
pac.update([eeml.Data(8, NO2res)])
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
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>2:
if failCount>23:
subprocess.call(["sudo", "/etc/init.d/networking", "restart"])
failCount=0
time.sleep(1)
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)
time.sleep(0)
currentDisplay+=1
if currentDisplay == 4:
currentDisplay = 0
#for a in range(0,5):
# time.sleep(0.2)
# GPIO.output(22, False)
......
......@@ -12,7 +12,10 @@ class AQSensor:
def get_quality(self):
result = float(self.adc.readADC(self.adcPin))
resistance = (1550/result - 1)*self.pullup
if result == 0:
resistance = 0
else:
resistance = (1550/result - 1)*self.pullup
return resistance
def get_NO2(self):
......
......@@ -13,3 +13,12 @@ class LightSensor:
vout = float(result)/1023 * 3.3
rs = ((3.3 - vout) / vout) * 5.6
return abs(rs)
def get_uv_level(self):
result = self.adc.readADC(self.adcPin)
vout = float(result)/1023 * 3.3
sensorVoltage = vout / 471
millivolts = sensorVoltage * 1000
UVI = millivolts * (5.25/20)
return UVI
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