-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (49 loc) · 1.52 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
from VZ89TE import VZ89TE
from ssd1306 import SSD1306_I2C
import utime
from machine import Pin, I2C
def mapvalue(x, in_min, in_max, out_min, out_max):
if (x > in_max):
x = inmax
if (x < in_min):
x = in_min
return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
led = Pin(19, Pin.OUT)
led.on()
# Pin 16 is SSD1306 Display Enable
pin16 = Pin(16, Pin.OUT)
pin16.on()
displayi2c = I2C(scl=Pin(15), sda=Pin(4))
display = SSD1306_I2C(128, 64, displayi2c)
display.fill(0)
sensori2c = I2C(1,scl=Pin(22), sda=Pin(21), freq = 100000)
sensor = VZ89TE(sensori2c)
print("Display Width: ", display.width)
print("Display Height: ", display.height)
print("I2C slave found at adr ", hex(sensori2c.scan()[0]) )
print("Revision: ", sensor.getRevision())
print("Year: ", sensor.getRevision()["Year"])
print("Month: ", sensor.getRevision()["Month"])
print("Day: ", sensor.getRevision()["Day"])
CO2DISPMAX = 1000
CO2DISPMIN = 400
co2 = 0
while True:
try:
co2 = sensor.getData()["CO2"]
except ValueError:
co2 = co2
display.fill(0)
w = int(mapvalue(co2, CO2DISPMIN, 1000, 0, display.width))
display.rect(0,0, display.width, display.height-12, 1)
display.fill_rect(0, 0, w, display.height-1-12, 1)
display.text("CO2: " + str(int(co2)) + " ppm", 0, display.height-1-8)
display.show()
try:
print(sensor.getData())
except ValueError:
print("oops! crc error!")
led.off()
utime.sleep_ms(500)
led.on()
utime.sleep(2)