-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
83 lines (66 loc) · 1.81 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
import json
import errno
import _thread
from utime import sleep
from lib.dht import DHT11
from machine import Pin, ADC
sleep(1)
# Address the onboard LED
led = Pin("LED", Pin.OUT)
# Address the DHT22 Module
sensor = DHT11(Pin(15, Pin.IN, Pin.PULL_UP))
# Address the onboard temperature diode
sensor_diode = ADC(4)
# Reference voltage divided by ADC resolution
conversion_factor = 3.3 / (1 << 16)
# Synchronization control
baton = _thread.allocate_lock()
# Our Data dict
data = dict()
def measure_onboard() -> float:
"""Measures temperature using a diode on the RP2040.
The calculated temperature may fluctuate with the reference voltage.
For more information see section 4.9.5 of the RP2040 documentation.
"""
voltage = sensor_diode.read_u16() * conversion_factor
c = 27 - (voltage - 0.706) / 0.001721
return round(c)
def measure() -> dict:
"""Measures DHT and onboard temperature values.
In case of any I/O errors, the errno field will be set.
"""
led.on()
t = 0
h = 0
o = 0
err = 0
try:
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
o = measure_onboard()
except OSError as exc:
err = exc.errno
return {"temperature": t, "onboard": o, "humidity": h, "error": err}
def report():
"""Designed to run on the RP2040s second core.
Its job is to update the data dict at a periodic rate.
While measuring, the onboard LED will be turned on.
"""
global data
while True:
led.on()
baton.acquire()
data = measure()
baton.release()
led.off()
sleep(1)
# Sleep to ensure no race conditions, then start
sleep(1)
_thread.start_new_thread(report, ())
sleep(1)
while True:
sleep(0.1)
baton.acquire()
print(json.dumps(data))
baton.release()