forked from thomaseichhorn/fhlthermorasp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pt100.py
99 lines (77 loc) · 2.1 KB
/
pt100.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
#!/usr/bin/env python3
from adafruit_blinka.microcontroller.bcm283x import pin
import board
import busio
import digitalio
import adafruit_max31865
from os.path import join, exists
from collections import namedtuple
PT100Result = namedtuple("PT100Result", ("sensor_name", "is_valid", "temp"))
import math
import time
import sys
class PT100:
def __init__(self, port, ch, wires=4):
self.wires = wires
self.port = port
self.ch = ch
self.cs_pin = self.chip_select_pin(port,ch)
if not self.cs_pin:
sys.exit()
# port 0 parameters
clk = 11
mosi = 10
miso = 9
if port == 1:
clk = 21
mosi = 20
miso = 19
self.clk = clk
self.mosi = mosi
self.miso = miso
self.spi = busio.SPI(self.clk,MOSI=self.mosi, MISO=self.miso)
self.cs = digitalio.DigitalInOut(self.cs_pin)
self.sensor = adafruit_max31865.MAX31865(self.spi, self.cs, wires=self.wires)
def read(self):
if self.sensor:
return PT100Result(self.get_sensor_name(), True, self.sensor.temperature)
def get_sensor_type_name(self):
return "PT100"
def get_sensor_name(self):
return "PT100_spi-%i_ch-%i" % (self.port, self.ch)
def get_sensor_fields(self):
return ["temp"]
def chip_select_pin(self,port, ch):
cs_pin = None
if port > 1:
print("port "+str(port)+" not valid")
elif port == 0:
if ch == 0:
cs_pin = board.D4
elif ch == 1:
cs_pin = board.D18
else:
print("channel "+str(ch)+" not valid")
else:
if ch == 0:
cs_pin = board.D12
elif ch == 1:
cs_pin = board.D13
elif ch == 2:
cs_pin = board.D16
else:
print("channel "+str(ch)+" not valid")
return cs_pin
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--wires', type = int, default = 4)
parser.add_argument('--port', type = int, default = 0)
parser.add_argument('--channel', type = int, default = 0)
args = parser.parse_args()
wires = args.wires
port = args.port
ch = args.channel
mydevice = PT100(port,ch,wires)
result = mydevice.read()
print("%s - is_valid:%r temperature:%.2f" % (result.sensor_name,result.is_valid, result.temp))