-
Notifications
You must be signed in to change notification settings - Fork 1
/
brewfactory.ino
151 lines (109 loc) · 3.34 KB
/
brewfactory.ino
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
// https://github.com/br3ttb/Arduino-PID-Library
#include "PID.h"
#include "PhaseRunner.h"
// https://github.com/krvarma/Dallas_DS18B20_SparkCore
#include "DS18B20.h"
#include "OneWire.h"
PhaseRunner *phaseRunner = new PhaseRunner();
double IDLE_POINT_TEMPERATURE = 5.0;
int EVENT_FREQUENCY = 1000;
int CALCULATE_FREQUENCY = 1000;
// Input pin where DS18B20 is connected
int ds18b20Pin = D1;
// Output pin where relay is connected
int relayPin = A6;
// SparkCloud variable outputs
char JSONStatus[64];
// Temperature device on the D1 pin
DS18B20 ds18b20 = DS18B20(ds18b20Pin);
// PID: variables
double pointTemperature, actualTemperature, pidOutput;
// PID: Specify the links and initial tuning parameters
PID myPID(&actualTemperature, &pidOutput, &pointTemperature, 25, 1000, 9, DIRECT);
// Timestamp of the last status events
unsigned long lastEventTimestamp, lastCalculateTimestamp;
// Setup
void setup() {
// Start serial on USB
Serial.begin(9600);
// SparkCloud: functions
Spark.function("setSchedule", setSchedule);
Spark.function("resetSchedule", resetSchedule);
// Set relay pin mode to output
pinMode(relayPin, OUTPUT);
// Point temperature default
pointTemperature = IDLE_POINT_TEMPERATURE;
//turn the PID on
myPID.SetMode(AUTOMATIC);
// Last event timestamp
lastEventTimestamp = 0;
// Last runner timestamp
lastCalculateTimestamp = 0;
}
// Loop
void loop() {
int timeEventDiff = millis() - lastEventTimestamp;
int timeCalculateDiff = millis() - lastCalculateTimestamp;
// Searching for the ds18b20 device
if(actualTemperature == 0 && !ds18b20.search()) {
// Log to the serial
Serial.println("No more addresses.");
Serial.println();
// Turn off the relay during the scan
digitalWrite(relayPin, LOW);
// Restart search
ds18b20.resetsearch();
delay(250);
return;
}
// Point temperature
if (timeCalculateDiff >= CALCULATE_FREQUENCY) {
// Actual temperature in celsius
actualTemperature = ds18b20.getTemperature();
// Compute PID
if(myPID.Compute()) {
// Control relay
analogWrite(relayPin, pidOutput);
}
// Step runner
phaseRunner->onTimeElapsed(timeCalculateDiff);
if(phaseRunner->getActualPhase()) {
pointTemperature = phaseRunner->getActualPhase()->getTemperature();
} else {
pointTemperature = IDLE_POINT_TEMPERATURE;
}
// Save timestamp of the event
lastCalculateTimestamp = millis();
}
// Send event to the SparkCloud
if (timeEventDiff >= EVENT_FREQUENCY) {
// Convert status info to JSON
sprintf(JSONStatus, "{ temp: %2.2f, pointTemp: %2.2f, pwm: %2.2f }",
actualTemperature, pointTemperature, pidOutput);
// Log to the serial
Serial.println(JSONStatus);
// SparkCore: publish
Spark.publish("status", JSONStatus);
// Save timestamp of the event
lastEventTimestamp = millis();
}
}
/*
* Set schedule
*/
int setSchedule(String schedule)
{
Serial.println("setSchedule called");
Serial.println(schedule);
phaseRunner.setSchedule(schedule);
return 1;
}
/*
* Reset schedule
*/
int resetSchedule()
{
Serial.println("resetSchedule called");
phaseRunner.resetSchedule();
return 1;
}