Repository navigation
Expand file tree
/
Copy pathTempHumSensor.py
More file actions
171 lines (127 loc) · 4.31 KB
/
Copy pathTempHumSensor.py
File metadata and controls
171 lines (127 loc) · 4.31 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#######################################################
# tTempHumSensor - class for reading and relaying inputs from our temp and humidity sensors
#
# This class only writes to the display, never reads
#
#################################################
# Modules used
#
from PySide6.QtCore import QObject
from ConfigInfo import *
# module used to talk over serial with the esp32
try:
import serial
except ModuleNotFoundError:
if PROJECT_CONFIG.bHasTempHumiditySensors:
raise
serial = None
##########################################################################################
##########################################################################################
##########################################################################################
# tMarquee
#
#
#
class tTempHumSensor(QObject):
#################################################
#
# CONFIGURATION
#
# BAUD_RATE = 115200
# Make the buffer big enough to hold 15 minutes of readings, in case we only read every 10 minutes.
# There are 30 readings per minute of 11 characters each (if there is a CR/LF after each)
# RX_BUFFER_SIZE = 15 * 30 * 11
# We use default 8N1 and no flow control
###############################################
# Constructor and destructor
#
# INPUTS:
#
def __init__(self, portName, parent=None):
super().__init__(parent)
self.port = None
self.portName = portName
self.OpenPort(False)
def __del__(self):
self.ClosePort()
###############################################
# ClosePort
#
# INPUTS:
#
def ClosePort(self):
if (self.port):
try:
self.port.close()
finally:
self.port = None
###############################################
# OpenPort
#
# INPUTS:
#
def OpenPort(self, bReopenAttempt):
if self.port:
print('tTempHumSensor: Port ', self.portName, ' already open')
return
try:
self.port = serial.Serial(self.portName, TEMP_HUM_BAUD_RATE, timeout=1)
except:
if not bReopenAttempt:
print('tTempHumSensor: Could not open port ', self.portName)
#sys.exit()
try:
self.port.set_buffer_size(rx_size = TEMP_HUM_RX_BUFFER_SIZE)
except AttributeError as err:
#print('PySerial::set_buffer_size not supported on this platform, continuing')
pass
if bReopenAttempt and self.port:
print('tTempHumSensor: Successfullly reopened port ', self.portName)
###############################################
# GetReading
#
# Returns the latest reading from the device.
# It's intended that you not call it any faster than its 2-second output rate
#
# It will flush all stale readings and only return the last
#
# If there is no data available, it will return None
#
def GetReading(self):
# If the port is not open try to reopen it.
if self.port is None:
self.OpenPort(True)
try:
if self.port is None or self.port.in_waiting == 0:
return None
except serial.SerialException as err:
print('tTempHumSensor: Port ', self.portName,' has gone offline')
self.ClosePort()
# If the port is not open try to reopen it.
if self.port is None:
self.OpenPort(True)
if self.port is None or self.port.in_waiting == 0:
return None
# Read lines from the port until there are no more
while self.port.in_waiting > 0:
line = self.port.readline().decode('utf-8').strip()
# At the beginning the Adafruit device returns some comment lines
if line[0] == "#":
return None
try:
# Split the comma-separated values. Our custom device emits Temp, Humidity
# The Adafruit Trinkey emits Sernum, temp, humidity, touch
values = line.split(',')
if len(values) == 2:
TempInC, Humidity = values
elif len(values) == 4:
# Convert to float and handle exceptions
SerNum, TempInC, Humidity, Touch = values
else:
raise ValueError("Unexpected number of values in line from temp/hum sensor")
TempInC = float(TempInC)
Humidity = float(Humidity)
except ValueError:
print('Invalid data received on temp/humidity sensor on port ', self.portName)
return None
return [ TempInC, Humidity ]