Newer
Older
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
"""
This code is designed to run on an ESP32 for grabbing temperature data from
ds18x20 sensors via the onewire protocol and sending them via serial to the
connected host. It should be trivial to port it to different platforms.
The Protocol is dead simple:
ONEWIRE-ID1 TEMPERATURE1
ONEWIRE-ID2 TEMPERATURE2
...
<Empty Line>
When a sensor has problems reading, it sends as temperature 9001.
New sensors are only detected upon powerup, so you have to reboot in order to
extend the sensor network.
The sensors have a parasitic-power-mode which is NOT TO BE USED here.
Please connect all three pins, and multiplex as you please.
"""
import machine
import time
import onewire, ds18x20
import ubinascii
class reader:
def __init__(self):
self.di = machine.Pin(13)
self.ds = ds18x20.DS18X20(onewire.OneWire(self.di))
#scan for sensors
self.roms = self.ds.scan()
def run(self):
while 1:
time.sleep_ms(240)
self.ds.convert_temp()
time.sleep_ms(750)
for rom in self.roms:
print(ubinascii.hexlify(rom).decode('utf-8'), end=' ')
try:
print(self.ds.read_temp(rom))
except:
print(9001)
print()