-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrfid_with_esp8266.ino
246 lines (219 loc) · 6.63 KB
/
rfid_with_esp8266.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
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "HTTPSRedirect.h"
#include "Display.h"
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
//added
#include <LiquidCrystal_I2C.h>
PN532_I2C pn532_i2c(Wire);
// int ledpin1 = D5;
// int ledpin2 = D6;
NfcAdapter nfc = NfcAdapter(pn532_i2c);
// Primitive Values
String tagId1 = "69 C9 D2 35";
String tagId2= "39 0B B6 B0";
String tagId = "None";
byte nuidPICC[4];
bool isPend = false;
bool IN = false;
bool IN2 = false;
// Enter network credentials:
const char* ssid = "Lucerfan";
const char* password = "erfan_m1383";
// Enter Google Script Deployment ID:
const char *GScriptId = "AKfycby3o0voFb1YA4voIMcL9oC3iDWXYdOscl-w0u8fOQLO3EohFxk5YSSiIfqbWbfsBWeJ";
// Enter command (insert_row or append_row) and your Google Sheets sheet name (default is Sheet1):
String payload_base = "{\"command\": \"insert_row\", \"sheet_name\": \"Sheet1\", \"values\": ";
String payload = "";
// Google Sheets setup (do not edit)
const char* host = "script.google.com";
const int httpsPort = 443;
const char* fingerprint = "";
String url = String("/macros/s/") + GScriptId + "/exec";
HTTPSRedirect* client = nullptr;
// Declare variables that will be published to Google Sheets
String user = "";
String id = "";
String enter ="";
Display display(isPend);
void setup() {
Serial.begin(9600);
Serial.println('\n');
display.initialize();
// pinMode(ledpin1,OUTPUT);
// pinMode(ledpin2,OUTPUT);
Serial.println("System initialized");
nfc.begin();
// digitalWrite(ledpin1, LOW);
// digitalWrite(ledpin2, LOW);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
// Use HTTPSRedirect class to create a new TLS connection
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
Serial.print("Connecting to ");
Serial.println(host);
// Try to connect for a maximum of 5 times
bool flag = false;
for (int i=0; i<5; i++) {
int retval = client->connect(host, httpsPort);
if (retval == 1) {
flag = true;
Serial.println("Connected");
break;
}
else
Serial.println("Connection failed. Retrying...");
}
if (!flag){
Serial.print("Could not connect to server: ");
Serial.println(host);
return;
}
delete client; // delete HTTPSRedirect object
client = nullptr; // delete HTTPSRedirect object
}
void loop() {
if(readNFC()) {
if (tagId==tagId1) {
if (IN) {
// if( digitalRead(ledpin1) == 1) {
// digitalWrite(ledpin1, LOW);
enter="";
user="Erfan";
id="123";
enter="out";
IN = false;
updatesheet(user,id, enter);
display.successOUT(user, enter);
tagId = "";
delay(500);
// }
} else {
// if( digitalRead(ledpin1) == 0) {
// digitalWrite(ledpin1, HIGH);
user="Erfan";
id="123";
enter="in";
IN = true;
updatesheet(user,id, enter);
tagId = "";
display.successIN(user, enter);
delay(500);
// }
}
} else {
if (IN2) {
// if( digitalRead(ledpin2) == 1) {
// digitalWrite(ledpin2, LOW);
user="Hossein";
id="456";
enter="out";
IN2 = false;
updatesheet(user,id, enter);
display.successOUT(user, enter);
tagId = "";
delay(500);
// }
} else {
// if( digitalRead(ledpin2) == 0) {
// digitalWrite(ledpin2, HIGH);
user="";
id="";
enter="";
user="Hossein";
id="456";
enter="in";
IN2 = true;
updatesheet(user,id, enter);
display.successIN(user, enter);
tagId = "";
delay(500);
// }
}
}
}
else
display.pend();
}
void updatesheet(String user, String id, String enter) {
static bool flag = false;
if (!flag) {
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
flag = true;
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
}
if (client != nullptr) {
if (!client->connected()) {
client->connect(host, httpsPort);
}
}
else {
Serial.println("Error creating client object!");
}
// Create json object string to send to Google Sheets
payload = payload_base + "\"" + user + "," + id + "," + enter + "\"}";
// Publish data to Google Sheets
Serial.println("Publishing data...");
Serial.println(payload);
if(client->POST(url, host, payload)) {
// do stuff here if publish was successful
}
else {
// do stuff here if publish was not successful
Serial.println("Error while connecting");
}
// a delay of several seconds is required before publishing again
delay(1000);
}
bool readNFC() {
if (nfc.tagPresent()) {
NfcTag tag = nfc.read();
tag.print();
tagId = tag.getUidString();
Serial.println("tag details :");
String content= "";
byte myByte[] = {0x00, 0xFF};
// const unsigned char* myByte_ptr = myByte;
Serial.println();
tag.getUid(myByte, tag.getUidLength());
for (int i = 0 ; i < tag.getUidLength() ; i++) {
Serial.println(myByte[i]);
}
// tag.getUid(byte *uid, unsigned int uidLength)
// for (byte i = 0; i < tag.getUidLength(); i++)
// {
// Serial.print(tag.getUid(i, tag.getUidLength()) < 0x10 ? " 0" : " ");
// Serial.print(tag.getUid(i, tag.getUidLength()), HEX);
// content.concat(String(tag.getUid(i, tag.getUidLength()) < 0x10 ? " 0" : " "));
// content.concat(String(tag.getUid(i, tag.getUidLength()), HEX));
// }
content.toUpperCase();
tag.print();
// Serial.println("Tag UID : " + tag.print() + " ");
Serial.println("Tag id");
Serial.println(tagId);
delay(1000);
return true;
}
return false;
}