-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermoRegulator.ino
326 lines (275 loc) · 6.43 KB
/
thermoRegulator.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
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
#include <SPI.h>
#include <Ethernet.h>
#include <avr/pgmspace.h>
#include <string.h>
//Used to store large strings in progmem.
char p_buffer[80];
#define P(str) (strcpy_P(p_buffer, PSTR(str)), p_buffer)
// MAC address and IP address.
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0, 51);
// Initialize the Ethernet server library on port 80
EthernetServer server(80);
//Request read buffer
#define bufferMax 64
int bufferSize;
char buffer[bufferMax];
/*
Analog pin 0 used for reading temperature
*/
int thermistorPin = A0;
/*
Capacity of time series
*/
const int CAPACITY = 12;
/*
Length of a period in milliseconds (5 min)
*/
const long PERIOD = 5000;//5*60000;
/*
The set temperature.
*/
const float SET_POINT = 20.0;
/*
The time series backing array
*/
float timeSeries[CAPACITY];
/*
Two dimensional time series array used for presentation and calculation purposes.
*/
float readableTs[CAPACITY][2];
/*
Pointer used for time series storage.
*/
int pointer = 0;
/*
JSONP callback function name.
*/
char callbackFunction[20];
/*
System setup
*/
void setup(){
initTimeSeries();
Serial.begin(9600);
initEthernet();
}
/*
Main processing loop
*/
void loop(){
static unsigned long before = 0;
if(millis()>before+PERIOD){
put(readTemperature());
printForecast();
before=millis();
}
serveWebRequests();
delay(500);
}
/*
Web server request - response loop
*/
void serveWebRequests()
{
// Listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println(P("new client"));
bufferRequest(client);
parseReceivedRequest();
writeResponse(client);
// Give the web browser time to receive the data
delay(1);
// Close the connection:
client.stop();
Serial.println(P("client disonnected"));
}
}
/*
Buffer the request into local memory
*/
void bufferRequest(EthernetClient client)
{
bufferSize = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n')
break;
else
if (bufferSize < bufferMax)
buffer[bufferSize++] = c;
else
break;
}
}
}
void parseReceivedRequest()
{
char* callbackString = strstr(buffer, "?callback=") + 10; // Look for question mark
char* space = strstr(callbackString, " ") + 1; // space after command
// strncpy does not automatically add terminating zero, but strncat does! So start with blank string and concatenate.
callbackFunction[0] = 0;
//copy function name into callback function
strncat(callbackFunction, callbackString, space-callbackString-1);
}
/*
Dispatch to all requested commands.
*/
void writeResponse(EthernetClient client)
{
writeHeader(client);
writeWebPage(client);
}
void writeHeader(EthernetClient client){
client.println(P("HTTP/1.1 200 OK"));
client.println(P("Content-Type: application/json"));
client.println(P("Connection: close"));
client.println();
}
/*
Read the temperature from pin 1 and convert it according to Steinhart-Hart formula
*/
double readTemperature(){
int thermistorReading = analogRead(thermistorPin);
double r = 6963200.0/thermistorReading - 6800.0;
r = r/10000;
double temp = 3.354016E-03 + 2.569850E-04 *log(r) + (2.620131E-06*pow(log(r),2)) + (6.383091E-08*pow(log(r),3));
temp = (1/temp)-272.15;// Convert Kelvin to Celcius
Serial.println(temp);
return temp;
}
void printForecast()
{
readTimeSeries();
float temp = readableTs[CAPACITY-1][1];
float m = calculateM();
float n = calculateN(m);
float fCast = forecastTime(m,n);
Serial.print(P("T="));
Serial.print(temp);
Serial.print(P(" y="));
Serial.print(m);
Serial.print(P("x+"));
Serial.print(n);
Serial.print(P(" "));
if(fCast > 0){
Serial.print(fCast/1000);
Serial.print(P("s"));
}
Serial.println("");
}
/*
Prints the current time series without modifying its contents.
*/
void writeWebPage(EthernetClient client)
{
readTimeSeries();
float m = calculateM();
float n = calculateN(m);
client.print(callbackFunction);
client.print(P("({\"tempHisto\":"));
printTempHisto(client);
client.print(P(", \"tempNow\":"));
client.print(readTemperature());
client.print(P(",\"setPoint\":"));
client.print(SET_POINT);
client.print(P(",\"period\":"));
client.print(PERIOD);
client.print(P(",\"capacity\":"));
client.print(CAPACITY);
client.print(P(",\"m\":"));
client.print(m);
client.print(P(",\"n\":"));
client.print(n);
client.print(P("});"));
}
String printTempHisto(EthernetClient client){
client.print(P("["));
for(int i = 0; i < CAPACITY-1; i++){
client.print(readableTs[i][1]);
client.print(P(","));
}
client.print(readableTs[CAPACITY-1][1]);
client.print(P("]"));
}
/*
Initialises time series arrays.
*/
void initTimeSeries(){
float temp = readTemperature();
for(int i = 0; i < CAPACITY; i++){
timeSeries[i] = temp;
readableTs[i][0] = 0.0;
readableTs[i][1] = temp;
}
}
/*
Start the Ethernet connection and the web server.
*/
void initEthernet()
{
Ethernet.begin(mac, ip);
server.begin();
Serial.print(P("server is at "));
Serial.println(Ethernet.localIP());
}
/*
Put value in round-robin fashion into the time series array
*/
void put (float value)
{
pointer++;
if(pointer % CAPACITY == 0){
pointer = 0;
}
timeSeries[pointer] = value;
}
/*
Copies the current time series backing array into a presentable array.
*/
void readTimeSeries(){
for(int i = 0; i < CAPACITY; i++){
int index = pointer + i +1;
if (index > CAPACITY-1) {
index = index -CAPACITY;
}
readableTs[i][0] = i-CAPACITY;
readableTs[i][1] = timeSeries[index];
}
}
/*
Calculate slope of tendency using least square method.
*/
float calculateM(){
float xAverage = average(0);
float yAverage = average(1);
float sumUp = 0.0;
float sumBottom = 0.0;
for(int i = 0; i < CAPACITY; i++){
sumUp = sumUp + ((readableTs[i][0] - xAverage)*(readableTs[i][1]-yAverage));
sumBottom = sumBottom + ((readableTs[i][0] - xAverage)*(readableTs[i][0] - xAverage));
}
return sumUp/sumBottom;
}
float calculateN(float m)
{
return average(1) - m* average(0);
}
/*
Calculates the average of x or y row of readable time series.
*/
float average(int rowId)
{
float sum = 0;
for (int i = 0; i < CAPACITY; i++){
sum += readableTs[i][rowId];
}
return sum / CAPACITY;
}
float forecastTime(float m, float n)
{
return (SET_POINT - n) / m * PERIOD;
}