-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodemcuRFID.ino
131 lines (99 loc) · 3.08 KB
/
NodemcuRFID.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
/*
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# NodeMCU ESP8266/ESP12E RFID MFRC522 / RC522 #
# D2 <----------> SDA/SS #
# D5 <----------> SCK #
# D7 <----------> MOSI #
# D6 <----------> MISO #
# GND <----------> GND #
# D1 <----------> RST #
# 3V/3V3 <----------> 3.3V #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <SPI.h>
#include <MFRC522.h>
#include <ArduinoJson.h>
#define SS_PIN D2
#define RST_PIN D1
MFRC522 mfrc522(SS_PIN, RST_PIN);
#define ON_Board_LED 2
int readsuccess;
byte readcard[4];
char str[32] = "";
String StrUID;
const char* ssid = "YOUR_NETWORK_SSSID";
const char* password = "YOUR_NETWORK_PASSWORD";
int getid() {
if(!mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if(!mfrc522.PICC_ReadCardSerial()) {
return 0;
}
for(int i=0;i<4;i++){
readcard[i]=mfrc522.uid.uidByte[i];
array_to_string(readcard, 4, str);
StrUID = str;
}
mfrc522.PICC_HaltA();
return 1;
}
void array_to_string(byte array[], unsigned int len, char buffer[]) {
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
}
void handleReadCard() {
readsuccess = getid();
if(readsuccess) {
digitalWrite(ON_Board_LED, LOW);
handleAttendIn();
digitalWrite(ON_Board_LED, HIGH);
}
}
void handleAttendIn(){
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
WiFiClient wifiClient;
http.begin(wifiClient,"http://3.144.31.214/api/iot/attend/"+StrUID);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "key=CONTACT_ME_TO_GIVE_YOU_THE_KEY";
int httpResponseCode = http.PUT(httpRequestData);
if(httpResponseCode>0){
String response = http.getString();
Serial.write(1);
Serial.println(response);
}else{
Serial.print("Error on sending PUT Request: ");
Serial.println(httpResponseCode);
}
http.end();
}else{
Serial.println("Error in WiFi connection");
}
delay(1500);
}
void setup() {
Serial.begin(9600); // KEEP FOR SERIAL COMMUNICATION
SPI.begin();
mfrc522.PCD_Init();
pinMode(ON_Board_LED,OUTPUT);
digitalWrite(ON_Board_LED, HIGH); // Turn off Led On Board
ESP.eraseConfig();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}
void loop() {
handleReadCard();
}