forked from adamzki99/rasp_pi_workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
74 lines (58 loc) · 1.51 KB
/
__init__.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
import RPi.GPIO as GPIO
import adafruit_dht # Importing a library used for the DHT22 which is the same as our AM2302
#import botbook_mcp3002 as mcp # For MQ-2 smoke sensor
LED_PIN = 21
AM2302_PIN = 2
PIR_MOTION_PIN = 8
DHT_SENSOR = adafruit_dht.DHT22(AM2302_PIN)
GPIO.setwarnings(False)
def connect():
while True:
try:
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
break
except RuntimeError as error:
print("Could not establish a connection to the Raspberry Pi")
_count = 0
def count():
global _count
_count += 1
return _count
def read_sensor_temperature():
"""
Returns the temperature as celsius
"""
temperature = 0
try:
temperature = DHT_SENSOR.temperature
except:
temperature = -1
if temperature == None:
temperature = -1
return temperature
def read_sensor_humidity():
"""
Returns the humidity as relative humidity
"""
humidity = 0
try:
humidity = DHT_SENSOR.humidity
except:
humidity = -1
if humidity == None:
humidity = -1
return humidity
def read_sensor_motion():
"""
Returns motion as True for ~1 second
"""
GPIO.setup(PIR_MOTION_PIN, GPIO.IN)
motion = GPIO.input(PIR_MOTION_PIN)
return motion
def led_on():
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.LOW) # Turn on LED
def led_off():
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.HIGH) # Turn off LED