-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfurnace.py
executable file
·541 lines (472 loc) · 16 KB
/
furnace.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#!/usr/bin/python3
import pigpio # http://abyz.co.uk/rpi/pigpio/python.html
import Adafruit_DHT
from w1thermsensor import W1ThermSensor, Sensor
import paho.mqtt.client as mqtt
import sys
import time
import yaml
import json
import uuid
'''
DHT Sensor Data-logging to MQTT Temperature channel
Requies a Mosquitto Server Install On the destination.
Copyright (c) 2014 Adafruit Industries
Author: Tony DiCola
MQTT Encahncements: David Cole (2016)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
<<<===============================<<<
MAX6675.py
2016-05-02
Public Domain
>>>===============================>>>
This script reads the temperature of a type K thermocouple
connected to a MAX6675 SPI chip.
Type K thermocouples are made of chromel (+ve) and alumel (-ve)
and are the commonest general purpose thermocouple with a
sensitivity of approximately 41 uV/C.
The MAX6675 returns a 12-bit reading in the range 0 - 4095 with
the units as 0.25 degrees centigrade. So the reported
temperature range is 0 - 1023.75 C.
Accuracy is about +/- 2 C between 0 - 700 C and +/- 5 C
between 700 - 1000 C.
The MAX6675 returns 16 bits as follows
F E D C B A 9 8 7 6 5 4 3 2 1 0
0 B11 B10 B9 B8 B7 B6 B5 B4 B3 B2 B1 B0 0 0 X
The reading is in B11 (most significant bit) to B0.
The conversion time is 0.22 seconds. If you try to read more
often the sensor will always return the last read value.
# pi.spi_open(0, 1000000, 0) # CE0, 1Mbps, main SPI
# pi.spi_open(1, 1000000, 0) # CE1, 1Mbps, main SPI
# pi.spi_open(0, 1000000, 256) # CE0, 1Mbps, auxiliary SPI
# pi.spi_open(1, 1000000, 256) # CE1, 1Mbps, auxiliary SPI
# pi.spi_open(2, 1000000, 256) # CE2, 1Mbps, auxiliary SPI
'''
# Subroutine look up 1 Wire temp(s)
def W1():
global temp
global sensor
global list
global count
tempC = 0.0
temp = 0.0
# sensor = W1ThermSensor()
sensor = W1ThermSensor(sensor_type=Sensor.DS18B20, sensor_id=list[count])
# Get the temp
tempC = sensor.get_temperature()
# Test the result. Make sure it is reasonable and not a glitch.
if tempC is None or tempC > 120.0 or tempC < 1.0:
return
# Conversion to F & round to .1
tF = round((9.0/5.0 * tempC + 32.0), 1)
# Use while Troubleshooting...
# print("{:.1f}".format(tF))
# Done
temp = tF
# Subroutine look up thermcouple temps
def thermocouple():
global temp
global pi
global list
global count
temp = 0.0
tempC = 0.0
# Check the thermocouple(s) on the Serial links.
sensor = pi.spi_open(list[count], 1000000, 0)
c, d = pi.spi_read(sensor, 2)
pi.spi_close(sensor)
if c == 2:
word = (d[0] << 8) | d[1]
if (word & 0x8006) == 0: # Bits 15, 2, and 1 should be zero.
tempC = (word >> 3)/4.0
# Test the result
if tempC is None or tempC > 1500.0 or tempC < 1.0:
return
# Conversion to F & round to .1
tF = round((9.0/5.0 * tempC + 32.0), 1)
# Use while Troubleshooting...
# print("{:.2f}".format(tF))
else:
print('bad reading {:b}'.format(word))
return
# Done
temp = tF
# Subroutine to look up temp/humid sensors
def tempHumid():
global temp
global humidity
temp = 0.0
tempC = 0.0
time.sleep(LOOP / 10) # Settling time
humidity, tempC = Adafruit_DHT.read_retry(
DHT_TYPE, list[count], retries=8, delay_seconds=.85)
# Skip to the next reading if a valid measurement couldn't be taken.
# This might happen if the CPU is under a lot of load and the sensor
# can't be reliably read (timing is critical to read the sensor).
if humidity is None or humidity > 100.0 or tempC is None or tempC > 150.0:
print('bad reading {0} {1}'.format(tempC, humidity))
return
temp = round((9.0/5.0 * tempC + 32.0), 1) # Conversion to F & round to .1
humidity = round(humidity, 1) # Round to .1
# Use while Troubleshooting...
# print('Temp: {0:0.1f}F Humd: {1:0.1f}%'.format(temp, humidity))
# Subroutine to send results to MQTT
def mqttSend():
global temp
global humidity
global mqttc
global count
global state_topic
if temp == 0.0:
return
try:
payloadOut = {
"temperature": temp,
"humidity": humidity}
OutState = state_topic[count]
print('Updating {0} {1}'.format(OutState,json.dumps(payloadOut) ) )
(result1,mid) = mqttc.publish(OutState, json.dumps(payloadOut), 1, True)
currentdate = time.strftime('%Y-%m-%d %H:%M:%S')
print('Date Time: {0}'.format(currentdate))
print('MQTT Update result {0}'.format(result1))
if result1 == 1:
raise ValueError('Result message from MQTT was not 0')
except Exception as e:
# Error appending data, most likely because credentials are stale.
# disconnect and re-connect...
print('MQTT error, trying re-connect: ' + str(e))
mqttc.publish(LWT, 'Offline', 0, True)
time.sleep(2)
mqttc.loop_stop()
mqttc.disconnect()
time.sleep(1)
mqttConnect()
pass
def mqttConnect():
print('Connecting to MQTT on {0} {1}'.format(HOST,PORT))
mqttc.connect(HOST, PORT, 60)
mqttc.loop_start()
mqttc.publish(LWT, "Online", 1, True)
mqttc.publish(CONFIGH_TH1, json.dumps(payloadH_TH1config), 1, True)
mqttc.publish(CONFIGT_TH1, json.dumps(payloadT_TH1config), 1, True)
mqttc.publish(CONFIGH_TH2, json.dumps(payloadH_TH2config), 1, True)
mqttc.publish(CONFIGT_TH2, json.dumps(payloadT_TH2config), 1, True)
mqttc.publish(CONFIG_W13, json.dumps(payload_W13config), 1, True)
mqttc.publish(CONFIG_W14, json.dumps(payload_W14config), 1, True)
mqttc.publish(CONFIG_TC5, json.dumps(payload_TC5config), 1, True)
mqttc.publish(CONFIG_TC6, json.dumps(payload_TC6config), 1, True)
temp = 0.0
humidity = 0.0
# set loop counter
count = 0
# Get the library for the thermocouples
pi = pigpio.pi()
# Check the library connection
if not pi.connected:
exit(0)
# Get the parameter file
with open("/opt/ThermoPI-Furnace/MYsecrets.yaml", "r") as ymlfile:
MYs = yaml.safe_load(ymlfile)
# Type of sensor, can be Adafruit_DHT.DHT11, Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302
DHT_TYPE = Adafruit_DHT.AM2302
# Example of sensor connected to Raspberry Pi pin 23
#DHT_PIN = 23
# Example of sensor connected to Beaglebone Black pin P8_11
#DHT_PIN = 'P8_11'
LOOP = MYs["MAIN"]["LOOP"]
HOST = MYs["MAIN"]["HOST"]
PORT = MYs["MAIN"]["PORT"]
USER = MYs["MAIN"]["USER"]
PWD = MYs["MAIN"]["PWD"]
AREA = MYs["MAIN"]["AREA"]
# Pulling the unique MAC SN section address using uuid and getnode() function
DEVICE_ID = (hex(uuid.getnode())[-6:]).upper()
TOPIC = "homeassistant/sensor/"
NAMED = MYs["MAIN"]["DEVICE_NAME"]
D_ID = DEVICE_ID + '_' + NAMED
LWT = TOPIC + D_ID + '/lwt'
PIN_TH1 = MYs["TEMP_HUMID"]["PIN_TH1"]
NAMEH_TH1 = MYs["TEMP_HUMID"]["NAMEH_TH1"]
H_TH1_ID = DEVICE_ID + '_' + MYs["TEMP_HUMID"]["H_TH1_ID"]
CONFIGH_TH1 = TOPIC + H_TH1_ID + '/config'
TH1_STATE = TOPIC + H_TH1_ID + '/state'
NAMET_TH1 = MYs["TEMP_HUMID"]["NAMET_TH1"]
T_TH1_ID = DEVICE_ID + '_' + MYs["TEMP_HUMID"]["T_TH1_ID"]
CONFIGT_TH1 = TOPIC + T_TH1_ID + '/config'
PIN_TH2 = MYs["TEMP_HUMID"]["PIN_TH2"]
NAMEH_TH2 = MYs["TEMP_HUMID"]["NAMEH_TH2"]
H_TH2_ID = DEVICE_ID + '_' + MYs["TEMP_HUMID"]["H_TH2_ID"]
CONFIGH_TH2 = TOPIC + H_TH2_ID + '/config'
TH2_STATE = TOPIC + H_TH2_ID + '/state'
NAMET_TH2 = MYs["TEMP_HUMID"]["NAMET_TH2"]
T_TH2_ID = DEVICE_ID + '_' + MYs["TEMP_HUMID"]["T_TH2_ID"]
CONFIGT_TH2 = TOPIC + T_TH2_ID + '/config'
ADDR_W13 = MYs["W1"]["ADDR_W13"]
NAME_W13 = MYs["W1"]["NAME_W13"]
W13_ID = DEVICE_ID + '_' + MYs["W1"]["W13_ID"]
CONFIG_W13 = TOPIC + W13_ID + '/config'
W13_STATE = TOPIC + W13_ID + '/state'
ADDR_W14 = MYs["W1"]["ADDR_W14"]
NAME_W14 = MYs["W1"]["NAME_W14"]
W14_ID = DEVICE_ID + '_' + MYs["W1"]["W14_ID"]
CONFIG_W14 = TOPIC + W14_ID + '/config'
W14_STATE = TOPIC + W14_ID + '/state'
SEN_TC5 = MYs["THERMOCOUPLE"]["SEN_TC5"]
NAME_TC5 = MYs["THERMOCOUPLE"]["NAME_TC5"]
TC5_ID = DEVICE_ID + '_' + MYs["THERMOCOUPLE"]["TC5_ID"]
CONFIG_TC5 = TOPIC + TC5_ID + '/config'
TC5_STATE = TOPIC + TC5_ID + '/state'
SEN_TC6 = MYs["THERMOCOUPLE"]["SEN_TC6"]
NAME_TC6 = MYs["THERMOCOUPLE"]["NAME_TC6"]
TC6_ID = DEVICE_ID + '_' + MYs["THERMOCOUPLE"]["TC6_ID"]
CONFIG_TC6 = TOPIC + TC6_ID + '/config'
TC6_STATE = TOPIC + TC6_ID + '/state'
# These are the GPIO's / SP ports / s/ns used for the temp/humid sensors.
list = [999, PIN_TH1, PIN_TH2, ADDR_W13, ADDR_W14, SEN_TC5, SEN_TC6, 999, 999 ]
# These are the STATE Topics
state_topic = ["", TH1_STATE, TH2_STATE, W13_STATE, W14_STATE, TC5_STATE, TC6_STATE, "", "" ]
payloadH_TH1config = {
"name": NAMEH_TH1,
"stat_t": TH1_STATE,
"avty_t": LWT,
"pl_avail": "Online",
"pl_not_avail": "Offline",
"uniq_id": H_TH1_ID,
"dev": {
"ids": [
D_ID,
DEVICE_ID
],
"name": "ThermoPI Furnace",
'sa': AREA,
"mf": "SirGoodenough",
"mdl": "HomeAssistant Discovery for ThermoPI Furnace",
"sw": "https://github.com/SirGoodenough/ThermoPI-Furnace",
"cu": "https://github.com/SirGoodenough/ThermoPI-Furnace/blob/main/README.md"
},
"unit_of_meas": "%",
"dev_cla":"humidity",
"frc_upd": True,
'exp_aft': 400,
"val_tpl": "{{ value_json.humidity }}"
}
payloadT_TH1config = {
"name": NAMET_TH1,
"stat_t": TH1_STATE,
"avty_t": LWT,
"pl_avail": "Online",
"pl_not_avail": "Offline",
"uniq_id": T_TH1_ID,
"dev": {
"ids": [
D_ID,
DEVICE_ID
],
"name": "ThermoPI Furnace",
'sa': AREA,
"mf": "SirGoodenough",
"mdl": "HomeAssistant Discovery for ThermoPI Furnace",
"sw": "https://github.com/SirGoodenough/ThermoPI-Furnace",
"cu": "https://github.com/SirGoodenough/ThermoPI-Furnace/blob/main/README.md"
},
"unit_of_meas":"°F",
"dev_cla":"temperature",
"frc_upd": True,
'exp_aft': 400,
"val_tpl": "{{ value_json.temperature }}"
}
payloadH_TH2config = {
"name": NAMEH_TH2,
"stat_t": TH2_STATE,
"avty_t": LWT,
"pl_avail": "Online",
"pl_not_avail": "Offline",
"uniq_id": H_TH2_ID,
"dev": {
"ids": [
D_ID,
DEVICE_ID
],
"name": "ThermoPI Furnace",
'sa': AREA,
"mf": "SirGoodenough",
"mdl": "HomeAssistant Discovery for ThermoPI Furnace",
"sw": "https://github.com/SirGoodenough/ThermoPI-Furnace",
"cu": "https://github.com/SirGoodenough/ThermoPI-Furnace/blob/main/README.md"
},
"unit_of_meas": "%",
"dev_cla":"humidity",
"frc_upd": True,
'exp_aft': 400,
"val_tpl": "{{ value_json.humidity }}"
}
payloadT_TH2config = {
"name": NAMET_TH2,
"stat_t": TH2_STATE,
"avty_t": LWT,
"pl_avail": "Online",
"pl_not_avail": "Offline",
"uniq_id": T_TH2_ID,
"dev": {
"ids": [
D_ID,
DEVICE_ID
],
"name": "ThermoPI Furnace",
'sa': AREA,
"mf": "SirGoodenough",
"mdl": "HomeAssistant Discovery for ThermoPI Furnace",
"sw": "https://github.com/SirGoodenough/ThermoPI-Furnace",
"cu": "https://github.com/SirGoodenough/ThermoPI-Furnace/blob/main/README.md"
},
"unit_of_meas":"°F",
"dev_cla":"temperature",
"frc_upd": True,
'exp_aft': 400,
"val_tpl": "{{ value_json.temperature }}"
}
payload_W13config = {
"name": NAME_W13,
"stat_t": W13_STATE,
"avty_t": LWT,
"pl_avail": "Online",
"pl_not_avail": "Offline",
"uniq_id": W13_ID,
"dev": {
"ids": [
D_ID,
DEVICE_ID
],
"name": "ThermoPI Furnace",
'sa': AREA,
"mf": "SirGoodenough",
"mdl": "HomeAssistant Discovery for ThermoPI Furnace",
"sw": "https://github.com/SirGoodenough/ThermoPI-Furnace",
"cu": "https://github.com/SirGoodenough/ThermoPI-Furnace/blob/main/README.md"
},
"unit_of_meas":"°F",
"dev_cla":"temperature",
"frc_upd": True,
'exp_aft': 400,
"val_tpl": "{{ value_json.temperature }}"
}
payload_W14config = {
"name": NAME_W14,
"stat_t": W14_STATE,
"avty_t": LWT,
"pl_avail": "Online",
"pl_not_avail": "Offline",
"uniq_id": W14_ID,
"dev": {
"ids": [
D_ID,
DEVICE_ID
],
"name": "ThermoPI Furnace",
'sa': AREA,
"mf": "SirGoodenough",
"mdl": "HomeAssistant Discovery for ThermoPI Furnace",
"sw": "https://github.com/SirGoodenough/ThermoPI-Furnace",
"cu": "https://github.com/SirGoodenough/ThermoPI-Furnace/blob/main/README.md"
},
"unit_of_meas":"°F",
"dev_cla":"temperature",
"frc_upd": True,
'exp_aft': 400,
"val_tpl": "{{ value_json.temperature }}"
}
payload_TC5config = {
"name": NAME_TC5,
"stat_t": TC5_STATE,
"avty_t": LWT,
"pl_avail": "Online",
"pl_not_avail": "Offline",
"uniq_id": TC5_ID,
"dev": {
"ids": [
D_ID,
DEVICE_ID
],
"name": "ThermoPI Furnace",
'sa': AREA,
"mf": "SirGoodenough",
"mdl": "HomeAssistant Discovery for ThermoPI Furnace",
"sw": "https://github.com/SirGoodenough/ThermoPI-Furnace",
"cu": "https://github.com/SirGoodenough/ThermoPI-Furnace/blob/main/README.md"
},
"unit_of_meas":"°F",
"dev_cla":"temperature",
"frc_upd": True,
'exp_aft': 400,
"val_tpl": "{{ value_json.temperature }}"
}
payload_TC6config = {
"name": NAME_TC6,
"stat_t": TC6_STATE,
"avty_t": LWT,
"pl_avail": "Online",
"pl_not_avail": "Offline",
"uniq_id": TC6_ID,
"dev": {
"ids": [
D_ID,
DEVICE_ID
],
"name": "ThermoPI Furnace",
'sa': AREA,
"mf": "SirGoodenough",
"mdl": "HomeAssistant Discovery for ThermoPI Furnace",
"sw": "https://github.com/SirGoodenough/ThermoPI-Furnace",
"cu": "https://github.com/SirGoodenough/ThermoPI-Furnace/blob/main/README.md"
},
"unit_of_meas":"°F",
"dev_cla":"temperature",
"frc_upd": True,
'exp_aft': 400,
"val_tpl": "{{ value_json.temperature }}"
}
#Log Message to start
print('Logging {0} sensor measurements every {1} seconds.'.format(D_ID, LOOP))
print('Press Ctrl-C to quit.')
mqttc = mqtt.Client(D_ID, 'False', 'MQTTv311')
mqttc.disable_logger() # Saves wear on SD card Memory. Remove as needed for troubleshooting
mqttc.username_pw_set(USER, PWD) # deactivate if not needed
mqttConnect()
try:
count = 0
while count < 7:
if count > 5: # Reset the loop
count = 0
count += 1
print('Updating loop %s.' % count)
temp = 0.0
humidity = 0.0
if count > 4:
thermocouple()
elif count > 2:
W1()
else:
tempHumid()
mqttSend()
time.sleep(LOOP)
except KeyboardInterrupt:
print(' Keyboard Interrupt. Closing MQTT.')
mqttc.publish(LWT, 'Offline', 1, True)
time.sleep(1)
mqttc.loop_stop()
mqttc.disconnect()
sys.exit()