forked from tomhartley/AirPi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Upload.py
164 lines (147 loc) · 4.61 KB
/
Upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python
from time import sleep
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
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)
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:
os.chdir(os.path.dirname(sys.argv[0]))
except:
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
# 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 and (stdscr == None):
for dp in datas:
print dp.name + ":\t" + dp.roundedValue() + " " + dp.unit
if stdscr != None:
a = 0
for dp in datas:
if dp.uploadID != -1:
a+=1
stdscr.addstr(5 + (a * 2), 3, dp.name + ":\t" + dp.roundedValue() + " " + dp.unit)
stdscr.clrtoeol()
stdscr.refresh()
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()
if stdscr == None:
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)