-
Notifications
You must be signed in to change notification settings - Fork 1
/
Skripsie2.ino
416 lines (338 loc) · 7.86 KB
/
Skripsie2.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
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
#include <Arduino.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
// Special characters
#define CR (13)
#define HORIZONTAL_LINE ("==================================================================")
// Custom pin definitions
#define baseTempADCPin (A0)
#define steamTempADCPin (A1)
#define currentSenseADCPin (A8)
#define SPI_SCK (52)
#define SPI_MOSI (51)
#define SPI_MISO (50)
#define SPI_SS (53)
// Custom baud rates
#define MEGA_BAUD_RATE (9600)
#define GSM_BAUD_RATE (9600)
// External class intantisations
Adafruit_PN532 nfc(SCK, MISO, MOSI, SS);
// Variables
volatile boolean primary = true;
volatile double primary_frac = 0;
volatile double second_frac = 0;
int current_duty_cycle = 0;
int8_t answer = 0;
int prev_baseTemp = 0;
int prev_steamTemp = 0;
char dummy = 0;
char main_menu_command = 0;
char debug_command = 0;
String inputString = "";
boolean stringComplete = false;
char platform_response[200];
char value_array[4];
void wdt_init()
{
}
void setup()
{
stopTRIACpulse();
Serial.begin(MEGA_BAUD_RATE);
Serial1.begin(GSM_BAUD_RATE);
answer = sendATcommand("AT", "OK", 2000);
answer += sendATcommand("AT+CREG?", "+CREG: 0,1", 2000);
if(answer == 0)
{
Serial.print(F("ERROR: Init"));
}
Serial.println(HORIZONTAL_LINE);
Serial.println(F("\tCoffee Percolator Monitor "));
Serial.println(F("\tType 'd' to enter debug mode"));
Serial.println(HORIZONTAL_LINE);
inputString.reserve(500);
Serial1.flush();
sendATcommand("AT+AWTDA=c*d*", "OK", 2000);
Serial1.print("AT+AWTDA=d,\"Coffee.misc\",1,\"currentState,INT32,2");
if(sendATcommand("\"", "OK", 2000))
{
Serial.println("Operating state: Idle");
}
Serial1.print("AT+AWTDA=a,\"Coffee\",1032831");
Serial1.write(CR);
delay(200);
while(Serial1.available() > 0)
{
dummy = (char)Serial1.read();
}
Serial.flush();
}
void loop()
{
serial1_event();
if(stringComplete == true)
{
Serial.print(inputString);
// Wait for an unsolicited response from the platform
if (inputString.startsWith("+AWTDA:"))
{
unpack_response(inputString.substring(8));
}
else
{
// Do something else
}
stringComplete = false;
inputString = "";
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer1,
unsigned int timeout)
{
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Serial1.available() > 0) Serial1.read(); // Clean the input buffer
Serial1.print(ATcommand); // Send the AT command
Serial1.write(CR);
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial1.available() != 0){
response[x] = Serial1.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
}
// Waits for the answer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
int getTemperature(int analogPin, int offset)
{
int i = 0;
double temp = 0;
double rawADC = 0;
// Read the raw voltage 5 times to reduce the noise
for(i = 0; i < 5; i++)
{
rawADC += analogRead(analogPin);
}
// Get the mean value
rawADC /= 5;
// Convert the voltage to a temperature
temp = rawADC/4 - offset;
// Return it as an integer
return (int)(temp);
}
void serial1_event()
{
while(Serial1.available() > 0)
{
char inChar = (char)Serial1.read();
inputString += inChar;
if(inChar == '\n')
stringComplete = true;
}
}
void stopTRIACpulse()
{
cli();
TCCR3A = 0;
TCCR3B = 0;
OCR3A=0;
sei();
}
void startTRIACpulse (int duty_cycle)
{
current_duty_cycle = duty_cycle;
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
cli(); // disable global interrupts
TCCR3A = 0;
TCCR3B = 0;
// set compare match register to desired timer count
OCR3A = 1;
// turn on CTC mode
TCCR3B |= _BV(WGM32);
// set CS30 and CS32 for 1024 prescalar
TCCR3B |= _BV(CS30)|_BV(CS32);
// enable timer compare interrupt
TIMSK3 |= _BV(OCIE3A);
sei(); // enable global interrupts
}
char get_array_length(char *arr)
{
int k = 0;
char *seek = arr;
while('\n' != *seek++)
{
k++;
}
return k;
}
void unpack_response(String content)
{
int8_t i = 0;
int duty_cycle = 0;
int content_length = 0;
String type;
String ticketID;
String asset;
String command;
String data_type;
String value;
String crc;
char *str;
char *p;
// Convert String to char array
content.toCharArray(platform_response, 200);
// Calculate the content length to save time
content_length = get_array_length(platform_response);
//Serial.print("Length: ");
//Serial.println(content_length, DEC);
// Seperate the string into values delimited by a comma (',')
for( str = strtok_r(platform_response, ",", &p);
str;
str = strtok_r(NULL, ",", &p))
{
switch(i)
{
case 0:
type = str;
break;
case 1:
ticketID = str;
break;
case 2:
asset = str;
break;
case 3:
command = str;
break;
case 4:
data_type = str;
break;
case 5:
value = str;
break;
case 6:
crc = str;
break;
default:
Serial.print("Unknown value: ");
Serial.println(str);
break;
}
i++;
}
value.toCharArray(value_array,4);
// for debugging purposes
if(true){
Serial.println(HORIZONTAL_LINE);
Serial.println(F("\tAWTDA Response Content"));
Serial.println(HORIZONTAL_LINE);
Serial.print("Response Type:\t");
Serial.println(type);
Serial.print("Ticket ID:\t");
Serial.println(ticketID);
Serial.print("Asset:\t\t");
Serial.println(asset);
Serial.print("Command:\t");
Serial.println(command);
Serial.print("Data Type:\t");
Serial.println(data_type);
Serial.print("Value:\t\t");
Serial.println(atoi(value_array));
Serial.print("CRC:\t\t");
Serial.println(crc);
Serial.println(HORIZONTAL_LINE);
}
if(command == "TriggerPulse")
{
duty_cycle = atoi(value_array);
if(duty_cycle > 0)
{
startTRIACpulse(duty_cycle);
if(duty_cycle != 100)
{
Serial.print(F("INFO: Percolator is given "));Serial.print(duty_cycle);Serial.print("%");Serial.println(" of maximum power");
}
else
{
Serial.print(F("INFO: Percolator is given maximum power"));
}
}
else
{
Serial.print(F("INFO: Percolator is switched off"));
stopTRIACpulse();
}
if(sendAck(ticketID))
{
Serial.print(F("SUCCESS: "));Serial.print(F("Ticket ID [ ")); Serial.print(ticketID);Serial.print(F(" ] was acknowledged."));
}
else
{
Serial.print(F("FAILURE: "));Serial.print(F("Ticket ID [ ")); Serial.print(ticketID);Serial.print(F(" ]was NOT acknowledged."));
}
}
type = NULL;
ticketID = NULL;
asset = NULL;
command = NULL;
data_type = NULL;
value = NULL;
crc = NULL;
}
void clean_serial1_buffer()
{
while(Serial1.available() > 0)
{
dummy = Serial1.read();
}
}
int8_t sendAck(String ticketID)
{
int8_t success = 0;
clean_serial1_buffer();
Serial1.print("AT+AWTDA=a,\"Coffee\",");
Serial1.print(ticketID);
success = sendATcommand("", "OK", 2000);
return success;
}
ISR(TIMER3_COMPA_vect)
{
if(current_duty_cycle == 100)
{
digitalWrite(2, HIGH);
}
else
{
primary_frac = 15624*(current_duty_cycle/100.0);
second_frac = 15624*(1 - (current_duty_cycle/100.0));
if(primary)
{
// on time
digitalWrite(2, HIGH);
OCR3A = primary_frac;
primary = false;
}
else
{
digitalWrite(2, LOW);
// off time (the larger OCR3A is here the shorter is the off time)
OCR3A = second_frac;
primary = true;
}
}
}