-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (57 loc) · 1.76 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
from pyb import LED
import sys, os, pyb
import machine
from machine import Pin
import time
import utime
from scd30 import SCD30
from machine import I2C
errled = LED(1)
led = LED(2)
# Get Sensor, should be scannable here...
i2c = I2C(1)
scd30 = SCD30(i2c, 0x61)
# Make sure we have a SD card!
sd = pyb.SDCard()
if sd.present() != 1:
errled.on()
machine.deepsleep()
# Power off the board...
sd.power(0)
en_3v3 = Pin('EN_3V3')
en_3v3.value(0)
while True:
data = { }
en_3v3.value(1)
led.on()
sd.power(1)
os.mount(pyb.SDCard(), '/sd')
# Get local time when taking measurement
for i in range(30):
t = utime.localtime()
while scd30.get_status_ready()[0] != 1:
time.sleep_ms(200)
time.sleep_ms(200)
try:
measurement = scd30.read_measurement()
data['co2'] = measurement[0][0]
data['temperature'] = measurement[1][0]
data['humidity'] = measurement[2][0]
print('co2: {}, temperature: {}, humidity: {}'.format(data['co2'], data['temperature'],
data['humidity']))
except SCD30.CRCException:
print('CRC exception during measurement reading SCD30')
errled.on()
pass
# Create logfile per day...
datetimeformat = '{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}'
isodatetime = datetimeformat.format(t[0], t[1], t[2], t[3], t[4], t[5])
name = 'sensordata_{:04d}_{:02d}_{:02d}.log'.format(t[0], t[1], t[2])
with open("/sd/" + name, "a") as f:
f.write("{};{};{};{};\n".format(isodatetime, data['co2'], data['temperature'], data['humidity']))
os.umount('/sd')
sd.power(0)
en_3v3.value(0)
led.off()
# Sleep for 300 seconds...
time.sleep(300)