-
Notifications
You must be signed in to change notification settings - Fork 8
/
PitmasterPi-v2.py
executable file
·146 lines (129 loc) · 4.77 KB
/
PitmasterPi-v2.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
#!/usr/bin/python
import smbus
import time
from time import sleep
import datetime
from subprocess import call
import sys
import xively
import dweepy
import requests
# Variables for your Xively, Dweet, Email - MUST SETUP
FEED_ID = ''
API_KEY = ''
DWEET_KEY = ''
DWEET_NAME = ''
alert_email = ''
#Preset Variables
api = xively.XivelyAPIClient(API_KEY)
bus = smbus.SMBus(1)
#I2C address
address = 0x4d
isHeating = True
P = 3
I = .05
B = 0
#xively function to return a datastream object. If one exists it returns it, if not, it creates it.
def get_datastream(feed):
try:
datastream = feed.datastreams.get("BBQPitTemp")
return datastream
except:
datastream = feed.datastreams.create("BBQPitTemp", tags="temperature")
return datastream
def update_graphs_lite(current_temp):
feed = api.feeds.get(FEED_ID)
datastream = get_datastream(feed)
datastream.max_value = None
datastream.min_value = None
datastream.current_value = current_temp
datastream.at = datetime.datetime.utcnow()
try:
datastream.update()
except requests.HTTPError as e:
print "HTTPError({0}): {1}]".format(e.errno, e.strerror)
def update_dweet_logging(current_temp,target_temp):
try:
# print dweepy.dweet_for(DWEET_NAME, {'key':DWEET_KEY,'pit_temp':current_temp,'target_temp':target_temp})
dweepy.dweet_for(DWEET_NAME, {'key':DWEET_KEY,'pit_temp':current_temp,'target_temp':target_temp})
except:
pass
def set_cooking_alert(target_temp): #Using Requests here until dweepy adds in alert/lock functionality
alert_url = 'https://dweet.io:443/alert/'+alert_email+'/when/'+DWEET_NAME+'/if(dweet.pit_temp%20%3C%3D%20'+str(target_temp-30)+')%20return%20%22Too%20LOW%22%3B%20else%20if(dweet.pit_temp%20%3E%3D%20'+str(target_temp+30)+')%20return%20%22Too%20HIGH%22%3B?key='+DWEET_KEY
print alert_url
requests.get(alert_url)
def get_current_temp():
data = bus.read_i2c_block_data(address, 1,2)
val = (data[0] << 8) + data[1]
return val/5.00*9.00/5.00+32.00
def get_current_temp_celcius():
data = bus.read_i2c_block_data(address, 1,2)
val = (data[0] << 8) + data[1]
return val/5.00
def turn_heat_on():
print "Turning Fan On"
call(["temp_relay_on", "hot"])
def turn_heat_off():
print "Turning Fan Off"
call(["temp_relay_off", "hot"])
def PID_Control_Loop(target_temp):
interror = 0
heater_state = "off"
cooking_temp_met = False
print "Now entering PID Control Loop Function"
print target_temp
previous_temp = int(get_current_temp())
#Trying to prevent misreadings from groundloops from messing up temp swings
while True:
current_temp = int(get_current_temp())
if current_temp > (109) and current_temp < (111):
current_temp = previous_temp
#Allowing for initial ramp up before setting the alerting
if cooking_temp_met == False and current_temp >= target_temp:
cooking_temp_met = True
set_cooking_alert(target_temp)
print 'setting up alert'
# update_graphs_lite(current_temp)
update_dweet_logging(current_temp,target_temp)
print "You are in PID timer-while loop: Current Temp is %d and target temp is %d and INTERROR is %d" % (current_temp, target_temp, interror)
error = (target_temp) - (current_temp)
# Trying to prevent INTERROR from jumping too much when lid lifts, etc. #autotune
if error < 10 and error > 0:
interror = interror + error
power = B + ((P * error) + ((I * interror)))
print "power is %d" % power
previous_temp = current_temp
for x in range(1,10):
print x
if (power > x**2): #May need tuning, still overshoots by power of 30, holds steady after that though
if (heater_state=="off"):
heater_state = "on"
print "State = ON"
print current_temp
turn_heat_on()
else:
print "Leaving the Heat ON"
else:
if (heater_state=="on"):
heater_state="off"
print "State is OFF"
turn_heat_off()
sleep(1)
if (power < 10):
sleep(10)
#if prev_power < power - 1000 OR prev_power > power + 1000: #lid must be off??
#sleep(180)
def main(argv):
if len (sys.argv) < 2 :
print "Usage: temperature_hold [temperature] "
sys.exit (1)
target_temp=sys.argv[1]
try:
target_temp=float(sys.argv[1])
except ValueError:
print "the argument is not a number"
sys.exit (1)
print "target temp =" , target_temp
PID_Control_Loop(target_temp)
if __name__ == "__main__":
main(sys.argv[1:])