-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombined_LF.py
129 lines (99 loc) · 2.46 KB
/
combined_LF.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
#Thingspeak
from urllib.request import urlopen
#Light
import RPi.GPIO as GPIO, time, os
import time
GPIO.setmode(GPIO.BCM)
#Fan
import smbus
#Temp
i2c_ch = 1
i2c_address = 0x48
reg_temp = 0x00
reg_config = 0x01
def twos_comp(val, bits):
if (val & (1 << (bits-1))) != 0:
val = val-(1<<bits)
return val
def read_temp():
val = bus.read_i2c_block_data(i2c_address, reg_temp, 2)
temp_c = (val[0] << 4) | (val[1] >> 5)
temp_c = twos_comp(temp_c, 12)
temp_c = temp_c * 0.0625
return temp_c
bus = smbus.SMBus(i2c_ch)
val = bus.read_i2c_block_data(i2c_address, reg_config, 2)
print ("Old CONFIG:", val)
val[1] = val[1] & 0b00111111
val[1] = val[1] | (0b10 << 6)
bus.write_i2c_block_data(i2c_address, reg_config,val)
val = bus.read_i2c_block_data(i2c_address, reg_config, 2)
print("New CONFIG:", val)
#set red,green and blue pins
redPin = 5
greenPin = 6
bluePin = 13
#set pins as outputs
GPIO.setup(redPin,GPIO.OUT)
GPIO.setup(greenPin,GPIO.OUT)
GPIO.setup(bluePin,GPIO.OUT)
#Select GPIO Mode
GPIO.setmode(GPIO.BCM)
#set red,green and blue pins
redPin = 5
greenPin = 6
bluePin = 13
#set pins as outputs
GPIO.setup(redPin,GPIO.OUT)
GPIO.setup(greenPin,GPIO.OUT)
GPIO.setup(bluePin,GPIO.OUT)
def turnOff():
GPIO.output(redPin,GPIO.HIGH)
GPIO.output(greenPin,GPIO.HIGH)
GPIO.output(bluePin,GPIO.HIGH)
def white():
GPIO.output(redPin,GPIO.LOW)
GPIO.output(greenPin,GPIO.LOW)
GPIO.output(bluePin,GPIO.LOW)
#Light
def RCtime (RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(0.5)
GPIO.setup(RCpin, GPIO.IN)
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
return reading
while True:
#Light
print (RCtime(17))
#Fan
temperature = read_temp()
print(round(temperature, 2), "C")
time.sleep(1)
if (RCtime(17)) > 6000:
#Light
white()
#Light
print("Led On")
else:
#Light
turnOff()
print("Led Off")
if temperature > 29.00:
#Fan
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
FAN_PIN = 20
GPIO.setup(FAN_PIN, GPIO.OUT)
GPIO.output(FAN_PIN, GPIO.LOW)
#Fan
time.sleep(0.5)
else:
#Fan
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
FAN_PIN = 20
GPIO.setup(FAN_PIN, GPIO.OUT)
GPIO.output(FAN_PIN, GPIO.HIGH)