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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
| -> 0
import sys, os, time # sys нужен для передачи argv в QApplication
import Fedya # Это наш конвертированный файл дизайна
import RPi.GPIO as GPIO
import Adafruit_DHT as dht
import threading
from threading import Thread
from datetime import datetime
from PyQt5 import QtWidgets, QtCore
#from PyQt5.QtCore import Qt, QTimer
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setup(14, GPIO.IN)
class ExampleApp(QtWidgets.QFrame, Fedya.Ui_Frame, threading.Thread):
def __init__(self):
super().__init__()
self.setupUi(self)
self.lcd()
self.text()
self.motor()
#self.valuechange()
def thread(my_func):
def wrapper(*args, **kwargs):
my_thread = threading.Thread(target=my_func, args=args, kwargs=kwargs)
my_thread.start()
return wrapper
def motor(self, vSl=0, parent=None):
#super(ExampleApp, self).motor(parent)
self.motorSlider.setMinimum(0)
self.motorSlider.setMaximum(100)
self.motorSlider.setValue(vSl)
self.motorSlider.valueChanged[int].connect(self.valuechange)
print("-> ", vSl)
def valuechange(self, value):
self.motor(value)
@thread
def lcd(self):
class MAX31855(object):
def __init__(self, cs_pin, clock_pin, data_pin, units = "c", board = GPIO.BCM):
self.cs_pin = cs_pin
self.clock_pin = clock_pin
self.data_pin = data_pin
self.units = units
self.data = None
self.board = board
GPIO.setmode(self.board)
GPIO.setup(self.cs_pin, GPIO.OUT)
GPIO.setup(self.clock_pin, GPIO.OUT)
GPIO.setup(self.data_pin, GPIO.IN)
GPIO.output(self.cs_pin, GPIO.HIGH)
def get(self):
self.read()
#self.checkErrors()
return getattr(self, "to_" + self.units)(self.data_to_tc_temperature())
def get_rj(self):
self.read()
return getattr(self, "to_" + self.units)(self.data_to_rj_temperature())
def read(self):
bytesin = 0
GPIO.output(self.cs_pin, GPIO.LOW)
for i in range(32):
GPIO.output(self.clock_pin, GPIO.LOW)
bytesin = bytesin << 1
if (GPIO.input(self.data_pin)):
bytesin = bytesin | 1
GPIO.output(self.clock_pin, GPIO.HIGH)
GPIO.output(self.cs_pin, GPIO.HIGH)
self.data = bytesin
def data_to_tc_temperature(self, data_32 = None):
if data_32 is None:
data_32 = self.data
tc_data = ((data_32 >> 18) & 0x3FFF)
return self.convert_tc_data(tc_data)
def data_to_rj_temperature(self, data_32 = None):
if data_32 is None:
data_32 = self.data
rj_data = ((data_32 >> 4) & 0xFFF)
return self.convert_rj_data(rj_data)
def convert_tc_data(self, tc_data):
if tc_data & 0x2000:
without_resolution = ~tc_data & 0x1FFF
without_resolution += 1
without_resolution *= -1
else:
without_resolution = tc_data & 0x1FFF
return without_resolution * 0.25
def convert_rj_data(self, rj_data):
if rj_data & 0x800:
without_resolution = ~rj_data & 0x7FF
without_resolution += 1
without_resolution *= -1
else:
without_resolution = rj_data & 0x7FF
return without_resolution * 0.0625
def to_c(self, celsius):
return celsius
def to_k(self, celsius):
return celsius + 273.15
def to_f(self, celsius):
return celsius * 9.0/5.0 + 32
def cleanup(self):
GPIO.setup(self.cs_pin, GPIO.IN)
GPIO.setup(self.clock_pin, GPIO.IN)
class MAX31855Error(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
if __name__ == "__main__":
import time
sensor = 22
pin = 14
cs_pins = [24]
clock_pin = 23
data_pin = 22
units = "c"
thermocouples = []
for cs_pin in cs_pins:
thermocouples.append(MAX31855(cs_pin, clock_pin, data_pin, units))
while True:
nowtime = str(datetime.strftime(datetime.now(), "%d.%m %H:%M:%S"))
self.DateTimelabel.setText(str(nowtime))
for thermocouple in thermocouples:
rj = thermocouple.get_rj()
try:
tc = thermocouple.get()
except MAX31855Error as e:
tc = e.value
#print('potok 1')
kot = '{0:0.1f}'.format(tc)
if kot == '0.0':
kot = 'Error'
self.lcdNumber_kot.display(str(kot))
else:
self.lcdNumber_kot.display(str(kot))
GPIO.setmode(GPIO.BCM)
if GPIO.input(14) == True:
humidity, temperature = dht.read_retry(sensor, pin)
t = '{0:0.1f}'.format(temperature)
h = '{0:0.1f}'.format(humidity)
self.lcdNumber_ho.display(str(t))
self.lcdNumber_vlag.display(str(h))
else:
t = 'Error'
h = 'Error'
self.lcdNumber_ho.display(str(t))
self.lcdNumber_vlag.display(str(h))
time.sleep(1.0)
for thermocouple in thermocouples:
thermocouple.cleanup()
@thread
def text(self):
class MAX31855(object):
def __init__(self, cs_pin, clock_pin, data_pin, units = "c", board = GPIO.BCM):
self.cs_pin = cs_pin
self.clock_pin = clock_pin
self.data_pin = data_pin
self.units = units
self.data = None
self.board = board
GPIO.setmode(self.board)
GPIO.setup(self.cs_pin, GPIO.OUT)
GPIO.setup(self.clock_pin, GPIO.OUT)
GPIO.setup(self.data_pin, GPIO.IN)
GPIO.output(self.cs_pin, GPIO.HIGH)
def get(self):
self.read()
#self.checkErrors()
return getattr(self, "to_" + self.units)(self.data_to_tc_temperature())
def get_rj(self):
self.read()
return getattr(self, "to_" + self.units)(self.data_to_rj_temperature())
def read(self):
bytesin = 0
GPIO.output(self.cs_pin, GPIO.LOW)
for i in range(32):
GPIO.output(self.clock_pin, GPIO.LOW)
bytesin = bytesin << 1
if (GPIO.input(self.data_pin)):
bytesin = bytesin | 1
GPIO.output(self.clock_pin, GPIO.HIGH)
GPIO.output(self.cs_pin, GPIO.HIGH)
self.data = bytesin
def data_to_tc_temperature(self, data_32 = None):
if data_32 is None:
data_32 = self.data
tc_data = ((data_32 >> 18) & 0x3FFF)
return self.convert_tc_data(tc_data)
def data_to_rj_temperature(self, data_32 = None):
if data_32 is None:
data_32 = self.data
rj_data = ((data_32 >> 4) & 0xFFF)
return self.convert_rj_data(rj_data)
def convert_tc_data(self, tc_data):
if tc_data & 0x2000:
without_resolution = ~tc_data & 0x1FFF
without_resolution += 1
without_resolution *= -1
else:
without_resolution = tc_data & 0x1FFF
return without_resolution * 0.25
def convert_rj_data(self, rj_data):
if rj_data & 0x800:
without_resolution = ~rj_data & 0x7FF
without_resolution += 1
without_resolution *= -1
else:
without_resolution = rj_data & 0x7FF
return without_resolution * 0.0625
def to_c(self, celsius):
return celsius
def to_k(self, celsius):
return celsius + 273.15
def to_f(self, celsius):
return celsius * 9.0/5.0 + 32
def cleanup(self):
GPIO.setup(self.cs_pin, GPIO.IN)
GPIO.setup(self.clock_pin, GPIO.IN)
class MAX31855Error(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
if __name__ == "__main__":
import time
sensor = 22
pin = 14
cs_pins = [24]
clock_pin = 23
data_pin = 22
units = "c"
thermocouples = []
for cs_pin in cs_pins:
thermocouples.append(MAX31855(cs_pin, clock_pin, data_pin, units))
while True:
nowtime = str(datetime.strftime(datetime.now(), "%d.%m %H:%M:%S"))
c = str(time.strftime("%M", time.localtime()))
d = '00'
#print('potok 2')
if c == d :
for thermocouple in thermocouples:
rj = thermocouple.get_rj()
try:
tc = thermocouple.get()
except MAX31855Error as e:
tc = e.value
kot = '{0:0.1f}'.format(tc)
if kot == '0.0':
tp = (nowtime + ' Термопара не подключена')
self.textBrowser.append (tp)
else:
tp = (nowtime + ' Термопара ' + kot)
self.textBrowser.append(tp)
if GPIO.input(14) == True:
humidity, temperature = dht.read_retry(sensor, pin)
if (humidity is not None) and (temperature is not None):
nowtime = str(datetime.strftime(datetime.now(), "%d.%m %H:%M:%S"))
ntth = str(nowtime + ' Температура {0:0.1f}C Влажность {1:0.1f}%'.format(temperature, humidity))
self.textBrowser.append(ntth)
else:
ntth = (nowtime + ' Датчик не подключен к порту № 14')
self.textBrowser.append(ntth)
f = open('text.txt', 'a')
f.writelines (ntth + '\n' + tp +'\n')
f.close()
time.sleep(60.0)
for thermocouple in thermocouples:
thermocouple.cleanup()
def main():
app = QtWidgets.QApplication(sys.argv) # Новый экземпляр QApplication
window = ExampleApp() # Создаём объект класса ExampleApp
window.show() # Показываем окно
sys.exit(app.exec_()) # и запускаем приложение
if __name__ == '__main__': # Если мы запускаем файл напрямую, а не импортируем
main() # то запускаем функцию main() |