-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAurdino Code.ino
256 lines (222 loc) · 6.69 KB
/
Aurdino Code.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
#include <SPI.h>
#include <Servo.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
Servo servo;
//#define BUZZER_pin D2
#define RST_PIN D0 // Configurable, see typical pin layout above
#define SS_PIN D8 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;
#define FIREBASE_HOST "esp8266final-default-rtdb.firebaseio.com" //Your Firebase Project URL goes here without "http:" , "\" and "/"
#define FIREBASE_AUTH "1Yk2lx09gDtQGpu2gBXqYmPY8SzNyluWYEKeoD2F" //Your Firebase Database Secret goes here
#define WIFI_SSID "V2037" //WiFi SSID to which you want NodeMCU to connect
#define WIFI_PASSWORD "ashishMrb"
const int irPin = D2;
const int moto = D1;
const long utcOffsetInSeconds = 19800;
//----------------------------------------
//----------------------------------------Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
FirebaseData firebaseData;
String value="";
String DBnm="Detected";
String HD="VehicleNo";
String TD="VehicleStatus";
String T="Time";
String R="Valid";
String F="Theft vehicle";
String loc="Chalukya Circle";
int buzzer = 2;
//int cnt=0;
//*****************************************************************************************//
void setup() {
pinMode(irPin, INPUT); // initialize digital pin 2 as an input
pinMode(moto, OUTPUT); //for LED as output
servo.attach(5); //D4
servo.write(90);
delay(2000);
Serial.begin(9600); // Initialize serial communications with the PC
pinMode(buzzer, OUTPUT);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Read personal data on a MIFARE PICC:")); //shows in serial that it is ready to read
Serial.println("Serial communication started\n\n");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //try to connect with wifi
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address is : ");
Serial.println(WiFi.localIP()); //print local IP address
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // connect to firebase
Firebase.reconnectWiFi(true);
delay(1000);
}
//*****************************************************************************************//
void loop() {
timeClient.update();
String hr, mn, sc;
if (timeClient.getHours() < 10) {
hr = "0" + String(timeClient.getHours());
}
else {
hr = String(timeClient.getHours());
}
if (timeClient.getMinutes() < 10) {
mn = "0" + String(timeClient.getMinutes());
}
else {
mn = String(timeClient.getMinutes());
}
if (timeClient.getSeconds() < 10) {
sc = "0" + String(timeClient.getSeconds());
}
else {
sc = String(timeClient.getSeconds());
}
String TimeNow = hr + ":" + mn + ":" + sc;
// Serial.print(TimeNow);
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
byte block;
byte len;
//-------------------------------------------
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println(F("**vehicle Detected:**"));
byte buffer1[18];
block = 4;
len = 18;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer1, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
for (uint8_t i = 0; i < 16; i++)
{
value += (char)buffer1[i];
}
value.trim();
//if(value!=""){
//// cnt++;
//}
//Serial.print(value);
//Serial.println(F("\n"));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
//if(value=="KA-02-E-6854")
//{
//Serial.println(F("THEFT VEHICLE DETECTED"));
//Serial.println(F("\n"));
//}
//else
//{
//Serial.println(F("VALID VEHICLE"));
//Serial.println(F("\n"));
//}
////Serial.println(F("\n"));
//delay(1000);
int valu = digitalRead(4);
// Serial.println(F(val));
if (valu == 0)
{
int valu = digitalRead(irPin);
//Serial.println(irValue);
if(value=="KA-02-E-6854")
{
servo.write(0);
delay(1000);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
//digitalWrite(buzzerPin, HIGH);
}
}
else
{
servo.write(90);
//digitalWrite(buzzerPin, LOW);
}
// delay(10);
// //delay(1000);
// }
//
// if (valu==1)
// {
// servo.write(90);
// // delay(1000);
// }
//}
// else
// {
// servo.write(90);
// }
if(value!=""){
String DBaddT = DBnm + "/" + T; //--> Creating a Database path
if(Firebase.setString(firebaseData,DBaddT,TimeNow)){
delay(1000);
}
else {
Serial.println(firebaseData.errorReason());
}
String DBaddH = DBnm + "/" + HD; //--> Creating a Database path
if(Firebase.setString(firebaseData,DBaddH,value)){
Serial.println("Value Uploaded Successfully");
delay(1000);
}
else {
Serial.println(firebaseData.errorReason());
}
if(value=="KA-02-E-6854")
{
String DBaddT = DBnm + "/" + TD; //--> Creating a Database path
if(Firebase.setString(firebaseData,DBaddT,F)){
}
else {
Serial.println(firebaseData.errorReason());
}
}
else
{
String DBaddT = DBnm + "/" + TD; //--> Creating a Database path
if(Firebase.setString(firebaseData,DBaddT,R)){
delay(1000);
}
else {
Serial.println(firebaseData.errorReason());
}
}
DBaddT = DBnm + "/" + "Location"; //--> Creating a Database path
if(Firebase.setString(firebaseData,DBaddT,loc)){
}
else {
Serial.println(firebaseData.errorReason());
}
value.clear();
}
}