-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32.ino
133 lines (117 loc) · 3.37 KB
/
esp32.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
#include <SPI.h>
#include <MFRC522.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#define SS_PIN 21
#define RST_PIN 22
MFRC522 mfrc522(SS_PIN, RST_PIN);
#define WIFI_NETWORK "Skyworth"
#define WIFI_PASSWORD "628717806"
#define WIFI_TIMEOUT_MS 20000
#define serverName "http://{YOUR_WIFI_IPV4}:8000/rfid_val/" //check seetings.py of RFID_Blood_Bank for more information
// int sensorPin = A0;
// int sensorValue = 0;
// String lastBlock="";
void sendData(String rfid_id) {
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/json");
String jsonData = "{\"RFID\": \"" + rfid_id + "\"}";
int httpCode = http.POST(jsonData);
Serial.println(httpCode);
http.end();
}
void connectToWiFi() {
Serial.print("connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);
unsigned long startAttemptTime = millis();
while(WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < WIFI_TIMEOUT_MS) {
Serial.print(".");
delay(100);
}
if(WiFi.status() != WL_CONNECTED) {
Serial.print(" Failed!");
}
else {
Serial.print("Connected!");
Serial.println(WiFi.localIP());
}
}
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
connectToWiFi();
//mfrc522.setPassiveActivationRetries(0x9A);
}
boolean flag=false;
String rfid;
// int counter=0;
void loop() {
if ( !mfrc522.PICC_IsNewCardPresent()) {
if(mfrc522.PICC_IsNewCardPresent()){
if(!flag){
// byte sector = 1;
// byte blockAddr = 4;
// byte dataBlock[] = {
// 0x01, 0x02, 0x03, 0x04, // 1, 2, 3, 4,
// 0x05, 0x06, 0x07, 0x08, // 5, 6, 7, 8,
// 0x08, 0x09, 0xff, 0x0b, // 9, 10, 255, 12,
// 0x0c, 0x0d, 0x0e, 0x0f // 13, 14, 15, 16
// };
// byte trailerBlock = 7;
// byte status;
// byte buffer[18];
// byte size = sizeof(buffer);
// status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
// dump_byte_array(buffer, 16);
// dump_byte_array();
Serial.println("Card detected");
mfrc522.PICC_ReadCardSerial();
rfid = array_to_string(mfrc522.uid.uidByte, 4);
String rfid_id = "1" + rfid;
sendData(rfid_id);
flag=true;
}
}
else{
if(flag){
Serial.println("Card removed");
String rfid_id = "0" + rfid;
sendData(rfid_id);
flag=false;
}
}
return;
}
}
String array_to_string(byte array[], unsigned int len)
{
char buffer[32] = "";
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';
String rfid = buffer;
return rfid;
}
// byte *buffer, byte bufferSize
// void dump_byte_array() {
// String a=String((char)buffer[9]);
// String b=String((char)buffer[10]);
// String c=String((char)buffer[11]);
// String d=String((char)buffer[12]);
// String str="";
// str+=a;
// str+=b;
// str+=c;
// str+=d;
// lastBlock=str;
// Serial.println("Card detected");
// }