-
Notifications
You must be signed in to change notification settings - Fork 0
/
PISI.IoT_ver_2.0.ino
389 lines (343 loc) · 9.02 KB
/
PISI.IoT_ver_2.0.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
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <Adafruit_MLX90614.h>
#include <Wire.h>
#include <Adafruit_Fingerprint.h>
#include <DS3231.h>
#define DonVi 1
#define Chuc 10
#define CauThoai 2
#define TempMin 0
#define TempMax 100
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
SoftwareSerial mySerial(2, 3);
DFRobotDFPlayerMini myDFPlayer;
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
DS3231 Clock;
double ambientTempC, objectTempC;
int btnPress = 6;
bool btnIsPressed = false;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
int users[] = {1, 7, 30, 70, 25, 10, 5, 64, 75, 82};
void setup() {
// put your setup code here, to run once:
Wire.begin();
mlx.begin();
Serial.begin(115200);
Serial.println("Initializing DFPlayer ... (May take 3~5 seconds)");
startConnectToPlayer();
myDFPlayer.volume(25); //Set volume value. From 0 to 30
startConnectToFingerprint();
pinMode(btnPress, INPUT);
}
void loop() {
int idUser = waitForDetectFingerprint();
objectTempC = mlx.readObjectTempC();
ambientTempC = mlx.readAmbientTempC();
showDateTimeNow();
Serial.print(objectTempC); Serial.print(" amb"); Serial.println(ambientTempC);
myDFPlayer.readState();
readNumber(objectTempC);
if (btnIsPressed){
duaRaLoiKhuyen(getTypeUser(idUser), objectTempC);
btnIsPressed = false;
}
}
void pressBtnEvent(){
if (digitalRead(btnPress)) btnIsPressed = true;
}
void readTempOnly(){
objectTempC = mlx.readObjectTempC();
myDFPlayer.readState();
readNumber(objectTempC);
}
void duaRaLoiKhuyen(int type, double objectTempC){
// switch(type){
// case 1:
// break;
// case 2:
// break;
// case 3:
// break;
// case 4 :
// break;
// }
if (objectTempC > 37){
if (objectTempC > 40) excuteRead(CauThoai, 8);
else if (objectTempC > 39.5) excuteRead(CauThoai, 6);
else if (objectTempC > 38) excuteRead(CauThoai, 7);
} else {
if (objectTempC < 34) excuteRead(CauThoai, 8);
else if (objectTempC < 35) excuteRead(CauThoai, 6);
else if (objectTempC < 36 ) excuteRead(CauThoai, 7);
}
}
int getTypeUser(int id){
int ageOfUser = users[id - 1];
Serial.print("getTypeUser: "); Serial.println(ageOfUser);
if (ageOfUser > 65) return 4;
else if (ageOfUser > 11) return 3;
else if (ageOfUser > 3) return 2;
else if (ageOfUser > 0) return 1;
}
int waitForDetectFingerprint(){
listenFingerprint();
int IdUser = -1;
while(IdUser == -1){
IdUser = getFingerprintIDez();
delay(50);
}
return IdUser;
}
void startConnectToPlayer(){
mySoftwareSerial.begin(9600);
while (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println("Unable to begin:");
Serial.println("1.Please recheck the connection!");
Serial.println("2.Please insert the SD card!");
delay(1000);
}
Serial.println("DFPlayer Mini online.");
}
void listenPlayer(){
mySoftwareSerial.listen();
}
void listenFingerprint(){
mySerial.listen();
}
void startConnectToFingerprint(){
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
finger.getTemplateCount();
Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
Serial.println("Waiting for valid finger...");
}
void readNumber(double num){
listenPlayer();
if(num < TempMin) excuteRead(CauThoai, 3); //003smailler20.mp3
else if (num > TempMax) excuteRead(CauThoai, 4); //004bigger90.mp3)
else {
excuteRead(CauThoai, 1); //nhiet do co the cua ban la.mp3
docSoHangChuc(num); //read number
if (num - (int)num > 0){
excuteRead(CauThoai, 5);
docSoHangChuc((num - (int)num)*100);
}
excuteRead(CauThoai, 2); //do C.mp3
}
}
void docSoHangChuc(int num){
if (num/10 > 0){
excuteRead(Chuc, num/10);
docSoDonVi(num%10);
} else
docSoDonVi(num);
}
void docSoDonVi(int num){
if (num > 0){
excuteRead(DonVi, num);
}
}
void excuteRead(int folder, int index){
myDFPlayer.playFolder(folder, index);
waitForPlayingFinish();
}
void waitForPlayingFinish(){
int detail = 0;
while (detail != DFPlayerPlayFinished){
if (myDFPlayer.available())
detail = getDetail(myDFPlayer.readType(), myDFPlayer.read());
pressBtnEvent();
}
}
int getDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
return TimeOut;
break;
case WrongStack:
// Serial.println(F("Stack Wrong!"));
return WrongStack;
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
return DFPlayerCardInserted;
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
return DFPlayerCardRemoved;
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
return DFPlayerCardOnline;
break;
case DFPlayerUSBInserted:
Serial.println("USB Inserted!");
return DFPlayerUSBInserted;
break;
case DFPlayerUSBRemoved:
Serial.println("USB Removed!");
return DFPlayerUSBRemoved;
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
return DFPlayerPlayFinished;
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
return DFPlayerError;
break;
default:
return -1;
break;
}
}
int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
void showDateTimeNow(){
// send what's going on to the serial monitor.
// Start with the year
Serial.print("Year: 2");
if (Century) { // Won't need this for 89 years.
Serial.print("1");
} else {
Serial.print("0");
}
Serial.print(Clock.getYear(), DEC);
// then the month
Serial.print(" Month:");
Serial.print(Clock.getMonth(Century), DEC);
// then the date
Serial.print(" Date:");
Serial.print(Clock.getDate(), DEC);
// and the day of the week
Serial.print(" Day of Week:");
Serial.print(Clock.getDoW(), DEC);
// Finally the hour, minute, and second
Serial.print(" ");
Serial.print(Clock.getHour(h12, PM), DEC);
Serial.print("h ");
Serial.print(Clock.getMinute(), DEC);
Serial.print("m ");
Serial.print(Clock.getSecond(), DEC);
Serial.print("s ");
// Add AM/PM indicator
if (h12) {
if (PM) {
Serial.print(" PM ");
} else {
Serial.print(" AM ");
}
} else {
Serial.print(" 24h ");
}
// Display the temperature
Serial.print("temp=");
Serial.print(Clock.getTemperature(), 2);
// Tell whether the time is (likely to be) valid
if (Clock.oscillatorCheck()) {
Serial.print(" O+");
} else {
Serial.print(" O-");
}
// Indicate whether an alarm went off
if (Clock.checkIfAlarm(1)) {
Serial.print(" A1!");
}
if (Clock.checkIfAlarm(2)) {
Serial.print(" A2!");
}
// New line on display
Serial.print('\n');
// Display Alarm 1 information
Serial.print("Alarm 1: ");
Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm);
Serial.print(ADay, DEC);
if (ADy) {
Serial.print(" DoW");
} else {
Serial.print(" Date");
}
Serial.print(' ');
Serial.print(AHour, DEC);
Serial.print(' ');
Serial.print(AMinute, DEC);
Serial.print(' ');
Serial.print(ASecond, DEC);
Serial.print(' ');
if (A12h) {
if (Apm) {
Serial.print('pm ');
} else {
Serial.print('am ');
}
}
if (Clock.checkAlarmEnabled(1)) {
Serial.print("enabled");
}
Serial.print('\n');
// Display Alarm 2 information
Serial.print("Alarm 2: ");
Clock.getA2Time(ADay, AHour, AMinute, ABits, ADy, A12h, Apm);
Serial.print(ADay, DEC);
if (ADy) {
Serial.print(" DoW");
} else {
Serial.print(" Date");
}
Serial.print(' ');
Serial.print(AHour, DEC);
Serial.print(' ');
Serial.print(AMinute, DEC);
Serial.print(' ');
if (A12h) {
if (Apm) {
Serial.print('pm');
} else {
Serial.print('am');
}
}
if (Clock.checkAlarmEnabled(2)) {
Serial.print("enabled");
}
// display alarm bits
Serial.print("\nAlarm bits: ");
Serial.print(ABits, BIN);
Serial.print('\n');
Serial.print('\n');
}
double findMod(double a, double b) {
// Handling negative values
if (a < 0)
a = -a;
if (b < 0)
b = -b;
// Finding mod by repeated subtraction
double mod = a;
while (mod >= b)
mod = mod - b;
return mod;
}