-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP_Hazel.ino
334 lines (291 loc) · 7.32 KB
/
ESP_Hazel.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
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Adafruit_Sensor.h>
#include <math.h>
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
#define BLYNK_PRINT Serial
#define VARID ""
#define RST_PIN 2
#define SS_PIN 15
const uint16_t kRecvPin = 0;
IRrecv irrecv(kRecvPin);
decode_results results;
int prev_ir_data;
MFRC522 mfrc522(SS_PIN, RST_PIN);
SoftwareSerial mini_Serial(5,4);
int getInt(String input);
char auth[] = "";
char ssid[] = "";
char password[] = "";
int status = WL_IDLE_STATUS;
char server[] = "api.openweathermap.org";
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 1000L;
boolean readingVal;
boolean getIsConnected = false;
String recv_data;
int val, temp;
float tempVal;
String writeName;
String writeAddress;
String writeEmail;
int writeAge;
String rcvbuf;
WiFiClient client;
void httpRequest();
void printWifiStatus();
String dump_byte_array(byte *buffer, byte bufferSize) {
String tmp_uid = "";
for (byte i = 0; i < bufferSize; i++) {
tmp_uid +=
String(buffer[i] < 0x10 ? " 0" : " ")+
String(buffer[i], HEX);
}
return tmp_uid;
}
String Card_UID = "";
int prev_rfid = -1;
BLYNK_WRITE(V0)
{
writeName = param.asStr();
Serial.print("Name: ");
Serial.println(writeName);
}
BLYNK_WRITE(V1)
{
writeAddress = param.asStr();
Serial.print("Address: ");
Serial.println(writeAddress);
}
BLYNK_WRITE(V2)
{
writeAge = param.asInt();
Serial.println(writeAge);
}
BLYNK_WRITE(V3)
{
writeEmail = param.asStr();
Serial.print("Emergency Email: ");
Serial.println(writeEmail);
}
int dailyScore(int weather_Score, int RFID_Score){
Serial.print("(Mood Score: " + String(RFID_Score) + ")*0.7 + (Weather Score: " + String(weather_Score) + ")*0.3 = ");
float DS = RFID_Score * 0.7 + weather_Score * 0.3;
Serial.println(DS);
int DS1 = round(DS);
return DS1;
}
int RFIDScore(){
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
delay(10);
String cardId = String(dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size));
Serial.print("Mood Score: ");
if(cardId.indexOf("22 8a 44 34") != -1){
Serial.println("Very Sad = 1");
return 1;
}
else if(cardId.indexOf("c6 fc 64 2b") != -1){
Serial.println("Sad = 2");
return 2;
}
else if(cardId.indexOf("c0 2f 32 32") != -1){
Serial.println("Normal = 3");
return 3;
}
else if(cardId.indexOf("24 27 7d 2b") != -1){
Serial.println("Happy = 4");
return 4;
}
else if(cardId.indexOf("a1 5c 20 1c") != -1){
Serial.println("Very Happy = 5");
return 5;
}
else{
Serial.print("ERR Code");
return -1;
}
}
else
return -1;
}
int getWeatherScore(String weather){
Serial.print("Today's Weather: ");
if(weather.indexOf("Clear")){
Serial.println("Clear = 5");
return 5;
}
else if(weather.indexOf("Clouds")){
return 4;
Serial.println("Clouds = 4");
}
else if(weather.indexOf("Drizzle")){
Serial.println("Drizzle = 3");
return 3;
}
else if(weather.indexOf("Rain")){
Serial.println("Rain = 2");
return 2;
}
else if(weather.indexOf("Thunderstorm")){
Serial.println("Thunderstorm = 1");
return 1;
}
else{
Serial.println("ERR Weather");
return -1;
}
}
int weatherScore(){
httpRequest();
unsigned long rev_time = millis();
while(millis() - rev_time < 5000){
if(client.available()) {
char c = client.read();
recv_data += c;
}
}
int tmp1 = recv_data.indexOf("main");
int tmp2 = recv_data.indexOf(",", tmp1+1);
String tmp_str_1 = recv_data.substring(tmp1+6, tmp2);
int weatherScore = getWeatherScore(tmp_str_1);
return weatherScore;
}
void httpRequest() {
client.stop();
if (client.connect(server, 80)) {
Serial.println("Connecting...");
client.print("GET /data/2.5/weather?q=06070,us&appid=");
client.print(VARID);
client.println(" HTTP/1.1");
client.println("Host: api.openweathermap.org");
client.println("Connection: close");
client.println();
lastConnectionTime = millis();
getIsConnected = true;
}
else {
Serial.println("Connection failed");
getIsConnected = false;
}
}
void printWifiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
int getInt(String input) {
char carray[20];
input.toCharArray(carray, sizeof(carray));
temp = atoi(carray);
return temp;
}
void serialParsing(){
if(mini_Serial.available()){
char data = mini_Serial.read();
if( data == '$'){
Serial.println("Notification");
char tmp_writeEmail[100];
writeEmail.toCharArray(tmp_writeEmail, writeEmail.length());
Blynk.email(tmp_writeEmail, "Alert: User not detected!", "Name: " + String(writeName) + " Age: " + String(writeAge) + " has not been detected for 72 hours! Location: "
+ String(writeAddress));
}
}
}
int IR_Recv(){
if (irrecv.decode(&results)) {
serialPrintUint64(results.value, HEX);
uint64_t ir_data = results.value;
irrecv.resume();
if( ir_data == 0xFF30CF){
return 5;
}
else if(ir_data == 0xFF18E7){
return 4;
}
else if(ir_data == 0xFF7A85){
return 3;
}
else if(ir_data == 0xFF10EF){
return 2;
}
else if(ir_data == 0xFF38C7){
return 1;
}
}
else{
return -1;
}
}
void setup() {
Serial.begin(9600);
Serial.println("START1");
mini_Serial.begin(9600);
irrecv.enableIRIn();
SPI.begin();
mfrc522.PCD_Init();
Serial.println("START2");
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Blynk.begin(auth, ssid, password);
Serial.println("START3");
}
void loop(){
Blynk.run();
BLYNK_WRITE(V0);
BLYNK_WRITE(V1);
BLYNK_WRITE(V2);
BLYNK_WRITE(V3);
serialParsing();
int tmp_rfid = RFIDScore();
if( tmp_rfid != prev_rfid && tmp_rfid != -1 ){
if(abs(prev_rfid - tmp_rfid) >= 4 && prev_rfid != -1){
Blynk.notify("Name: " + String(writeName) + " has been detected with potential mood swing!");
}
String send_data = "";
send_data += "@,";
send_data += dailyScore(weatherScore(), tmp_rfid);
send_data += ",#";
mini_Serial.println(send_data);
Serial.print("Send Daily Score data to Arduino : ");
Serial.println(send_data);
Serial.println();
prev_rfid = tmp_rfid;
}
int now_ir_data = IR_Recv();
if( prev_ir_data != now_ir_data && now_ir_data != -1 && now_ir_data < 6){
prev_ir_data = now_ir_data;
String send_data = "";
send_data += "$,";
send_data += now_ir_data;
send_data += ",#";
mini_Serial.println(send_data);
Serial.println(now_ir_data);
Serial.print("Send IR data to Arduino : ");
Serial.println(send_data);
Serial.println();
}
}