forked from nebhead/PiFire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
executable file
·599 lines (492 loc) · 17.8 KB
/
common.py
File metadata and controls
executable file
·599 lines (492 loc) · 17.8 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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import time
import datetime
import os
import json
def DefaultSettings():
settings = {}
settings['history_page'] = {
'minutes' : 60, # Sets default number of items to show in history
'clearhistoryonstart' : True, # Clear history when StartUp Mode selected
'autorefresh' : 'on', # Sets history graph to autorefresh ('live' graph)
'datapoints' : 60 # Number of datapoints to show on the history chart
}
settings['probe_settings'] = {
'probe_profiles' : DefaultProbeProfiles(),
'probes_enabled' : [1,1,1]
}
settings['globals'] = {
'grill_name' : '',
'debug_mode' : False,
'page_theme' : 'light',
}
settings['ifttt'] = {
'APIKey': '', # API Key for WebMaker IFTTT App notification
}
settings['pushover'] = {
'APIKey': '', # API Key for Pushover notifications
'UserKeys': '', # Comma-separated list of user keys
'PublicURL': '', # Used in Pushover notifications
}
settings['probe_types'] = {
'grill0type' : 'PT1000-00',
'probe1type' : 'TWPS00',
'probe2type' : 'TWPS00',
}
settings['outpins'] = {
'power' : 4,
'auger' : 14,
'fan' : 15,
'igniter' : 18
}
settings['inpins'] = { 'selector' : 17 }
#PID controller based on proportional band in standard PID form https://en.wikipedia.org/wiki/PID_controller#Ideal_versus_standard_PID_form
# u = Kp (e(t)+ 1/Ti INT + Td de/dt)
# PB = Proportional Band
# Ti = Goal of eliminating in Ti seconds
# Td = Predicts error value at Td in seconds
settings['cycle_data'] = {
'PB' : 60.0,
'Ti' : 180.0,
'Td' : 45.0,
'HoldCycleTime' : 20,
'SmokeCycleTime' : 15,
'PMode' : 2, # http://tipsforbbq.com/Definition/Traeger-P-Setting
'u_min' : 0.15,
'u_max' : 1.0
}
settings['smoke_plus'] = {
'enabled' : False, # Sets default Enable/Disable (True = Enabled, False = Disabled)
'min_temp' : 160, # Minimum temperature to cycle fan on/off
'max_temp' : 220, # Maximum temperature to cycle fan on/off
'cycle' : 10, # Number of seconds to cycle the fan on/off
'frequency' : 1, # For PWM, if implemented (Currently not used)
'duty_cycle' : 50 # For PWM, if implemented (Currently not used)
}
settings['safety'] = {
'minstartuptemp' : 75, # User Defined. Minimum temperature allowed for startup.
'maxstartuptemp' : 100, # User Defined. Take this value if the startup temp is higher than maxstartuptemp
'maxtemp' : 500, # User Defined. If temp exceeds this value in any mode, shut off. (including monitor mode)
'reigniteretries' : 1, # Number of tries to reignite the grill if it has gone below the safe temperature (set to 0 to disable)
}
settings['modules'] = {
'grillplat' : 'pifire', # Grill Platform (PiFire - Raspberry Pi GPIOs)
'adc' : 'ads1115', # Analog to Digital Converter Default is the ADS1115
'display' : 'ssd1306', # Default display is the SSD1306
'dist' : 'prototype' # Default distance sensor is none
}
return settings
def DefaultControl():
settings = ReadSettings()
control = {}
control['updated'] = True
control['mode'] = 'Stop'
if(settings['smoke_plus']['enabled'] == True):
control['s_plus'] = True # Smoke-Plus Feature Enable/Disable
else:
control['s_plus'] = False # Smoke-Plus Feature Enable/Disable
control['hopper_check'] = False # Trigger an synchronous hopper level check
control['recipe'] = ''
control['status'] = ''
control['probe_profile_update'] = False
control['setpoints'] = {
'grill' : 0,
'probe1' : 0,
'probe2' : 0
}
control['notify_req'] = {
'grill' : False,
'probe1' : False,
'probe2' : False,
'timer' : False,
}
control['notify_data'] = {
'hopper_low' : False,
'p1_shutdown' : False,
'p2_shutdown' : False,
'timer_shutdown' : False,
}
control['timer'] = {
'start' : 0,
'paused' : 0,
'end' : 0,
'shutdown' : False
}
control['manual'] = {
'change' : False,
'output' : '',
'state' : '',
'current' : {
'fan' : 1,
'auger' : 1,
'igniter' : 1,
'power' : 1
}
}
control['safety'] = {
'startuptemp' : 0, # Set by control function at startup
'afterstarttemp' : 0, # Set by control function during startup
'reigniteretries' : settings['safety']['reigniteretries'], # Set by user to attempt a re-ignite when the grill drops below a certain temp
'reignitelaststate' : 'Smoke' # Set by control function to remember the last state we were in when the temp dropped below safety levels
}
return(control)
def DefaultRecipes():
recipes = {}
recipes['321ribs'] = {
'metadata': {
'display_name': '3-2-1 Baby Back Ribs',
'image': ''
},
'steps' : {
'step_00': {
'smoke' : True, # Start with smoke temp for grill
'timer' : 180, # Go for three hours (180 mins)
'notify' : True,
'desciption' : 'Set grill to smoke at 165F.'
},
'step_01': {
'grill_temp' : 275,
'notify' : True,
'timer' : 120, # Go for two hours (120 mins)
'desciption' : 'Wrap ribs and increase grill temp to 275F'
},
'step_02': {
'grill_temp' : 300,
'timer' : 60,
'notify' : True,
'desciption' : 'Un-wrap ribs and increase grill temp to 300F'
}
}
}
return recipes
def DefaultPellets():
pelletdb = {}
now = str(datetime.datetime.now())
now = now[0:19] # Truncate the microseconds
ID = ''.join(filter(str.isalnum, str(datetime.datetime.now())))
pelletdb['current'] = {
'pelletid' : ID, # Pellet ID for the profile currently loaded
'hopper_level' : 100, # Percentage of pellets remaining
'date_loaded' : now, # Date that current pellets loaded
}
pelletdb['empty'] = 30 # Number of centimeters from the sensor that indicates empty
pelletdb['woods'] = ['Alder', 'Almond', 'Apple', 'Apricot', 'Blend', 'Competition', 'Cherry', 'Chestnut', 'Hickory', 'Lemon', 'Maple', 'Mesquite', 'Mulberry', 'Nectarine', 'Oak', 'Orange', 'Peach', 'Pear', 'Plum', 'Walnut' ]
pelletdb['brands'] = ['Generic', 'Custom']
pelletdb['archive'] = {
ID : {
'id' : ID,
'brand' : 'Generic',
'wood' : 'Alder',
'rating' : 4,
'comments' : 'This is a placeholder profile. Alder is generic and used in almost all pellets, regardless of the wood type indicated on the packaging. It tends to burn consistantly and produces a mild smoke.'
}
}
pelletdb['log'] = {
now : ID
}
return pelletdb
def DefaultProbeProfiles():
probe_profiles = {}
probe_profiles['TWPS00'] = {
'Vs' : 3.28, # Vs = Voltage Source input to resistor divider
'Rd' : 10000, # Divider Resistance Ohms (Default 10k Ohm)
'A' : 7.3431401e-4, # Coefficient A for SHH # from HeaterMeter?
'B' : 2.1574370e-4, # Coefficient B for SHH
'C' : 9.5156860e-8, # Coefficient C for SHH
'name' : 'Thermoworks-Pro-Series-HeaterMeter'
}
probe_profiles['ET73-HM'] = {
'Vs' : 3.28, # Vs = Voltage Source input to resistor divider
'Rd' : 10000, # Divider Resistance Ohms (Default 10k Ohm)
'A' : 2.4723753e-04, # Coefficient A for SHH # from HeaterMeter?
'B' : 2.3402251e-04, # Coefficient B for SHH
'C' : 1.3879768e-07, # Coefficient C for SHH
'name' : 'ET-73-Heatermeter'
}
probe_profiles['iGrill-HM'] = {
'Vs' : 3.28, # Vs = Voltage Source input to resistor divider
'Rd' : 10000, # Divider Resistance Ohms (Default 10k Ohm)
'A' : 0.7739251279e-3, # Coefficient A for SHH # from HeaterMeter?
'B' : 2.088025997e-4, # Coefficient B for SHH
'C' : 1.154400438e-7, # Coefficient C for SHH
'name' : 'iGrill-Heatermeter'
}
probe_profiles["PT1000-00"] = {
"Vs": 3.28,
"Rd": 10000,
"A": 0.04136906456,
"B": -0.00677987613,
"C": 2.760294589e-05,
"name": "PT-1000-OEM-RTD"
}
probe_profiles['ET73-SP'] = {
'Vs' : 3.28, # Vs = Voltage Source input to resistor divider
'Rd' : 10000, # Divider Resistance Ohms (Default 10k Ohm)
'A' : 2.3067434E-4, # from: https://github.com/skyeperry1/Maverick-ET-73-Meat-Probe-Arduino-Library/blob/master/ET73.h
'B' : 2.3696596E-4,
'C' : 1.2636414E-7,
'name' : 'ET-73-skyeperry1'
}
return probe_profiles
def ReadControl():
# *****************************************
# Read Control From JSON File
# *****************************************
try:
json_data_file = os.fdopen(os.open('/tmp/control.json', os.O_RDONLY))
#json_data_file = open("/tmp/control.json", "r")
json_data_string = json_data_file.read()
control = json.loads(json_data_string)
json_data_file.close()
except(IOError, OSError):
# Issue with reading file, so create one/write new one
control = DefaultControl()
WriteControl(control)
return(control)
except(ValueError):
# A ValueError Exception occurs when multiple accesses collide, this code attempts a retry.
event = 'ERROR: Value Error Exception - JSONDecodeError reading control.json'
WriteLog(event)
json_data_file.close()
# Retry Reading Control
control = ReadControl()
return(control)
def WriteControl(control):
# *****************************************
# Write all control states to JSON file
# *****************************************
json_data_string = json.dumps(control)
with open("/tmp/control.json", 'w') as control_file:
control_file.write(json_data_string)
def ReadSettings():
# *****************************************
# Read Settings from file
# *****************************************
# Get latest settings format
settings = DefaultSettings()
try:
json_data_file = os.fdopen(os.open('settings.json', os.O_RDONLY))
#json_data_file = open("settings.json", "r")
json_data_string = json_data_file.read()
settings_struct = json.loads(json_data_string)
json_data_file.close()
except(IOError, OSError):
# Default settings
settings = DefaultSettings()
# Issue with reading states JSON, so create one/write new one
WriteSettings(settings)
return(settings)
except(ValueError):
# A ValueError Exception occurs when multiple accesses collide, this code attempts a retry.
event = 'ERROR: Value Error Exception - JSONDecodeError reading control.json'
WriteLog(event)
json_data_file.close()
# Retry Reading Settings
settings_struct = ReadSettings()
# Overlay the read values over the top of the default settings
# This ensures that any NEW fields are captured.
for key in settings.keys():
settings[key].update(settings_struct.get(key, {}))
return(settings)
def WriteSettings(settings):
# *****************************************
# Write all settings to JSON file
# *****************************************
json_data_string = json.dumps(settings)
with open("settings.json", 'w') as settings_file:
settings_file.write(json_data_string)
def ReadRecipes():
# *****************************************
# Read RecipeDB from File
# *****************************************
# Read all lines of states.json into an list(array)
try:
json_data_file = os.fdopen(os.open('recipes.json', os.O_RDONLY))
#json_data_file = open("recipes.json", "r")
json_data_string = json_data_file.read()
recipes = json.loads(json_data_string)
json_data_file.close()
except(IOError, OSError):
# Issue with reading JSON, so create one/write new one
recipes = DefaultRecipes()
WriteRecipes(recipes)
return(recipes)
def WriteRecipes(recipes):
# *****************************************
# Write RecipeDB to JSON file
# *****************************************
json_data_string = json.dumps(recipes)
with open("recipes.json", 'w') as recipes_file:
recipes_file.write(json_data_string)
def ReadPelletDB():
# *****************************************
# Read Pellet DataBase from file
# *****************************************
# Read all lines of states.json into an list(array)
try:
json_data_file = os.fdopen(os.open('pelletdb.json', os.O_RDONLY))
#json_data_file = open("pelletdb.json", "r")
json_data_string = json_data_file.read()
pelletdb = json.loads(json_data_string)
json_data_file.close()
except(IOError, OSError):
# Issue with reading JSON, so create one/write new one
pelletdb = DefaultPellets()
WritePelletDB(pelletdb)
return(pelletdb)
def WritePelletDB(pelletdb):
# *****************************************
# Write Pellet DataBase to JSON file
# *****************************************
json_data_string = json.dumps(pelletdb)
with open("pelletdb.json", 'w') as json_file:
json_file.write(json_data_string)
def ReadLog():
# *****************************************
# Function: ReadLog
# Input: none
# Output:
# Description: Read event.log and populate
# an array of events.
# *****************************************
# Read all lines of events.log into an list(array)
try:
with open('/tmp/events.log') as event_file:
event_lines = event_file.readlines()
event_file.close()
# If file not found error, then create events.log file
except(IOError, OSError):
event_file = open('/tmp/events.log', "w")
event_file.close()
event_lines = []
# Initialize event_list list
event_list = []
# Get number of events
num_events = len(event_lines)
for x in range(num_events):
event_list.append(event_lines[x].split(" ",2))
# Error handling if number of events is less than 10, fill array with empty
if (num_events < 10):
for line in range((10-num_events)):
event_list.append(["--------","--:--:--","---"])
num_events = 10
return(event_list, num_events)
def WriteLog(event):
# *****************************************
# Function: WriteLog
# Input: str event
# Description: Write event to event.log
# Event should be a string.
# *****************************************
now = str(datetime.datetime.now())
now = now[0:19] # Truncate the microseconds
logfile = open("/tmp/events.log", "a")
logfile.write(now + ' ' + event + '\n')
logfile.close()
def ReadHistory(num_items=0):
# *****************************************
# Function: ReadHistory
# Input: num_items (items from end of the history)
# Output: data_list
# Description: Read history.log and populate
# a list of data
# *****************************************
# Read all lines of history.log into a list(array)
try:
if(num_items == 0):
with open('/tmp/history.log') as history_file:
history_lines = history_file.readlines()
history_file.close()
else:
command = 'tail -n ' + str(num_items) + ' /tmp/history.log'
history_file = os.popen(command)
history_lines = history_file.readlines()
history_file.close()
# If file not found error, then create history.log file
except(IOError, OSError):
data_list = []
WriteLog('WARNING: Issue reading /tmp/history.log')
return(data_list)
# Initialize data list
data_list = []
for index in range(len(history_lines)):
data_line = history_lines[index].rsplit(' ', 1)[0] # Strips off the '\n' from the line
data_list.append(data_line.split(' ',6)) # Splits out each of the values into seperate list items
return(data_list)
def ReadCurrent():
# *****************************************
# Function: ReadCurrent
# Input: none
# Output: cur_probe_temps []
# Description: Read current.log and populate
# a list of data
# *****************************************
try:
with open('/tmp/current.log') as current_file:
current_line = current_file.readline()
current_file.close()
# If file not found error, then return 0'd data
except(IOError, OSError):
cur_probe_temps = [0,0,0]
WriteLog('WARNING: Issue reading /tmp/current.log')
timenow = datetime.datetime.now()
timestr = timenow.strftime('%H:%M:%S') # Truncate the microseconds
curfile = open("/tmp/current.log", "w") # Write current data to current.log file
curfile.write(timestr + ' 0 0 0 0 0 0' )
curfile.close()
return(cur_probe_temps)
# Initialize data list
data_list = []
data_list = current_line.split(' ',6) # Splits out each of the values into seperate list items
cur_probe_temps = [0, 0, 0]
cur_probe_temps[0] = int(data_list[1])
cur_probe_temps[1] = int(data_list[3])
cur_probe_temps[2] = int(data_list[5])
return(cur_probe_temps)
def WriteHistory(TempStruct, maxsizelines=28800):
# *****************************************
# Function: WriteHistory
# Input: TempStruct
# Description: Write event to history.log AND current.log
# Event should be a string.
# *****************************************
timenow = datetime.datetime.now()
timestr = timenow.strftime('%H:%M:%S') # Truncate the microseconds
event = str(int(TempStruct['GrillTemp'])) + ' ' + str(TempStruct['GrillSetPoint']) + ' ' + str(int(TempStruct['Probe1Temp'])) + ' ' + str(TempStruct['Probe1SetPoint']) + ' ' + str(int(TempStruct['Probe2Temp'])) + ' ' + str(TempStruct['Probe2SetPoint'])
logfile = open("/tmp/history.log", "a") # Append current data to history.log file
logfile.write(timestr + ' ' + event + ' \n')
logfile.close()
curfile = open("/tmp/current.log", "w") # Write current data to current.log file
curfile.write(timestr + ' ' + event)
curfile.close()
tr_values = str(int(TempStruct['GrillTr'])) + ' ' + str(int(TempStruct['Probe1Tr'])) + ' ' + str(int(TempStruct['Probe2Tr']))
trfile = open("/tmp/tr.log", "w") # Write current data to current.log file
trfile.write(tr_values)
trfile.close()
command = 'wc -l /tmp/history.log' # Use the Word Count CLI tool to get number of lines
history_file = os.popen(command)
history_lines = history_file.readlines()
history_file.close()
temp_array = history_lines[0].split(' ') # Split result line into parts
if(int(temp_array[0]) >= maxsizelines):
WriteLog('File: history.log at maximum set size, removing an hour of data from beginning.')
os.system('tail -n ' + str(maxsizelines - 1200) + ' /tmp/history.log > /tmp/history.bak')
os.system('rm /tmp/history.log && mv /tmp/history.bak /tmp/history.log')
def ReadTr():
# *****************************************
# Function: ReadTr
# Input: none
# Output: cur_probe_tr []
# Description: Read tr.log and populate
# a list of data
# *****************************************
try:
with open('/tmp/tr.log') as tr_file:
tr_line = tr_file.readline()
tr_file.close()
# If file not found error, then return 0'd data
except(IOError, OSError):
cur_probe_tr = [0,0,0]
WriteLog('WARNING: Issue reading /tmp/tr.log')
return(cur_probe_tr)
cur_probe_tr = tr_line.split(' ',2) # Splits out each of the values into seperate list items
return(cur_probe_tr)