-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
268 lines (192 loc) · 6.78 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from machine import Pin ,I2C , WDT , reset
from umqtt.simple import MQTTClient
import ubinascii
import neopixel
import tm1637
import time
import network
import CCS811
import bme280
####config start#######
#config Wifi
wifi_enable = False #True or False for using Wifi
wifi_ssid = "yourSSID"
wifi_password = "yourWifiPassword"
#config MQTT
mqtt_client = "yourMqttClientName"
mqtt_broker = "yourMqttBrokerIP"
mqtt_user = "yourMqttUser"
mqtt_password = "yourMqttPassword"
mqtt_port = 1883
#def MQTT Topic
topic_co2 = b"home/livingroom/co2"
topic_tvoc = b"home/livingroom/tvoc"
topic_temp = b"home/livingroom/temperature"
topic_humidity = b"home/livingroom/humidity"
topic_press = b"home/livingroom/pressure"
####config end#######
#init Watchdog 10 sec
wdt = WDT(timeout=60000)
print("init sw watchdog ")
#NeoPixel def
pin = Pin(19,Pin.OUT)
np = neopixel.NeoPixel(pin, 8)
#7Segment def
tm = tm1637.TM1637(clk=Pin(17), dio=Pin(18))
#CCS811 def
i2c = I2C(scl=Pin(13), sda=Pin(5))
# Adafruit sensor breakout has i2c addr: 90; Sparkfun: 91
s = CCS811.CCS811(i2c=i2c, addr=90)
#BME280 def
bme = bme280.BME280(i2c=i2c)
# clear NeoPixel
def np_clear():
for i in range(8):
np[i] = (0, 0, 0)
np.write()
np_clear() # clear
#def state for first run
state = 0
#def for use wifi
def connect_wifi():
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
if not sta_if.isconnected():
print("connecting to wlan...")
sta_if.connect(wifi_ssid, wifi_password)
while not sta_if.isconnected():
time.sleep(10)
pass
print(sta_if.ifconfig())
def check_wifi():
sta_if = network.WLAN(network.STA_IF)
if sta_if.isconnected() == True:
return True
else:
return False
def restart_and_reconnect():
try:
print("failed to connect to MQTT broker . Reconnecting...")
time.sleep(10)
reset()
except OSError as e:
restart_and_reconnect()
#create MQTT client
client = MQTTClient(mqtt_client, mqtt_broker ,user=mqtt_user, password=mqtt_password, port=mqtt_port)
def check_co2():
try:
if s.data_ready():
data_eCO2_tVOC = [s.eCO2, s.tVOC]
return data_eCO2_tVOC
time.sleep(1)
except:
print ("Error read CCS811")
def sub_neopixel_room_ambient(eCO2 , tVOC):
global state
co2_value = int(eCO2)
print("eCO2_value: %s , tVOC_value: %s " % (eCO2,tVOC) )
if co2_value >= 1500:
#print("open the windows!")
for i in range(8):
np[i] = (16,0,0)
#red alert
np.write()
state = 1
elif co2_value >= 900:
#print("mid air quailty")
for i in range(8):
np[i] = (32,16,0)
#yellow
np.write()
state = 1
elif co2_value < 900:
#print("good air quality")
for i in range(8):
np[i] = (0,16,0)
#green
np.write()
#tm.show(str(co2_value))
state = 1
elif state == 0:
print("no data aviable")
n=np.n
# bounce
for i in range(2 * n):
for j in range(n):
np[j] = (8, 0, 16)
if (i // n) % 2 == 0:
np[i % n] = (0, 0, 0)
else:
np[n - 1 - (i % n)] = (0, 0, 0)
np.write()
time.sleep_ms(60)
def sub_display_7digit( temp): #display data on 7Segment
print("Bme280 Temp: %s " % temp)
temp_value = temp
temp_value = str(temp_value).replace(".","C")
tm.show(temp_value)
#print((temp_value))
# let's go
if state == 0:
tm.show("Test")
n=np.n
# bounce for test neopixel
for i in range(2 * n):
for j in range(n):
np[j] = (0, 0, 8)
if (i // n) % 2 == 0:
np[i % n] = (0, 0, 0)
else:
np[n - 1 - (i % n)] = (0, 0, 0)
np.write()
time.sleep_ms(30)
connect_wifi()
measure_time = time.ticks_ms()
send_data_time = time.ticks_ms()
while 1:
try:
#15 min = 900000 millisec
#1 min = 60000 millisec
delta_measure = time.ticks_diff(time.ticks_ms(), measure_time)
delta_send_data = time.ticks_diff(time.ticks_ms(), send_data_time)
if delta_measure >= 60000 or state == 0: # 1 min = 60000 millisec or if state == 0 while first run then check the sensors
#measure bme280
r = bme.read_compensated_data()
t = r[0]/100
t = t - 2 #offset TempSensor
p = r[1]/25600
h = r[2]/1024
s.put_envdata(humidity=h,temp=t)
#measure ccs811
air_data = check_co2()
if air_data is not None :
# neopixel write
sub_neopixel_room_ambient(air_data[0],air_data[1])
# 7 digit write
sub_display_7digit(t)
measure_time = time.ticks_ms()
if wifi_enable == True:
if check_wifi() == True and delta_send_data >= 900000 : # 15 min = 900000 millisec
try:
client.connect()
client.publish(topic_co2, str(air_data[0]))
time.sleep(0.5)
client.publish(topic_tvoc, str(air_data[1]))
time.sleep(0.5)
client.publish(topic_temp, str(t))
time.sleep(0.5)
client.publish(topic_humidity,str(h))
time.sleep(0.5)
client.publish(topic_press,str(p))
time.sleep(0.5)
send_data_time = time.ticks_ms()
client.disconnect()
except:
restart_and_reconnect()
elif delta_send_data >= 900000:
send_data_time = time.ticks_ms()
else :
pass
wdt.feed()
except OSError as e:
print("Error while loop")