-
Notifications
You must be signed in to change notification settings - Fork 2
/
grovepi-zero-dht22-air-initialstate.py
71 lines (61 loc) · 2.37 KB
/
grovepi-zero-dht22-air-initialstate.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
from grovepi import grovepi
import os
import time
from ISStreamer.Streamer import Streamer
# --------- User Settings ---------
# The DHT_SENSOR_TYPE below may need to be changed depending on which DHT sensor you have:
# 0 - DHT11 - blue one - comes with the GrovePi+ Starter Kit
# 1 - DHT22 - white one, aka DHT Pro or AM2302
# 2 - DHT21 - black one, aka AM2301
DHT_SENSOR_TYPE = 1
# Connect the DHT sensor to one of the digital pins (i.e. 2, 3, 4, 7, or 8)
DHT_SENSOR_PIN = 3
# Connect the Grove Air Quality Sensor to one of the analog pins (i.e. 0, 1, 2)
AIR_SENSOR_PIN = 0
# Initial State settings
BUCKET_NAME = ":partly_sunny: Indoor Environment"
BUCKET_KEY = "GPZa"
ACCESS_KEY = "PLACE YOUR INITIAL STATE ACCESS KEY HERE"
# Set the time between sensor reads
MINUTES_BETWEEN_READS = 1
METRIC_UNITS = False
# ---------------------------------
def isFloat(string):
try:
float(string)
return True
except ValueError:
return False
streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY)
while True:
try:
[temp_c,hum] = grovepi.dht(DHT_SENSOR_PIN,DHT_SENSOR_TYPE)
airSensorVal = grovepi.analogRead(AIR_SENSOR_PIN)
if isFloat(temp_c):
if (METRIC_UNITS):
# print "Temperature(C) = " + str(temp_c)
streamer.log("Temperature(C)",temp_c)
else:
temp_f = temp_c * 9.0 / 5.0 + 32.0
temp_f = float("{0:.2f}".format(temp_f))
# print "Temperature(F) = " + str(temp_f)
streamer.log("Temperature(F)",temp_f)
if ((isFloat(hum)) and (hum >= 0)):
# print "Humidity(%) = " + str(hum)
streamer.log(":sweat_drops: Humidity(%)",hum)
if isFloat(airSensorVal):
if airSensorVal > 700:
# print "High pollution"
streamer.log("Air Quality", ":fog: :bangbang:")
elif airSensorVal > 300:
# print "Low pollution"
streamer.log("Air Quality", ":foggy: :exclamation:")
else:
# print "Air fresh"
streamer.log("Air Quality", ":rainbow:")
# print "Air Quality =", airSensorVal
streamer.log("Air Quality Sensor", airSensorVal)
streamer.flush()
except IOError:
print ("Error")
time.sleep(60*MINUTES_BETWEEN_READS)