-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendData.ino
183 lines (160 loc) · 5.09 KB
/
sendData.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
#include <SPI.h>
#include <MFRC522.h> //13.56 MHz
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <WiFiClientSecureBearSSL.h>
const uint8_t fingerprint[20] = {0x14, 0xE1, 0x36, 0x8E, 0xEA, 0xF3, 0x30, 0x6C, 0x1E, 0xDB, 0x9E, 0x43, 0x1E, 0xEF, 0xAA, 0x94, 0x1C, 0x9E, 0xA7, 0x74};
// 14 E1 36 8E EA F3 30 6C 1E DB 9E 43 1E EF AA 94 1C 9E A7 74
#define RST_PIN D3 // Configurable, see typical pin layout above
#define SS_PIN D4 // Configurable, see typical pin layout above
#define BUZZER D2 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
ESP8266WiFiMulti WiFiMulti;
MFRC522::StatusCode status;
/* Be aware of Sector Trailer Blocks */
int blockNum = 2;
/* Create another array to read data from Block */
/* Legthn of buffer should be 2 Bytes more than the size of Block (16 Bytes) */
byte bufferLen = 18;
byte readBlockData[18];
String data2;
/* Google sheet url */
const String data1 = "https://script.google.com/macros/s/AKfycbzvyV3YUf0d3EEcVUlKUNNgUcfrQLD6LazFsRBxZpENXZUOFylDHK5QVATXSJzAcD2Z/exec?name=";
void setup()
{
/* Initialize serial communications with the PC */
Serial.begin(9600);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--)
{
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
/* Put your WIFI Name and Password here */
Serial.printf("ready to connet\n");
/* wifi creds */
WiFiMulti.addAP("TEST", "test123456");
Serial.printf("Conneceted to TEST");
/* Set BUZZER as OUTPUT */
pinMode(BUZZER, OUTPUT);
/* Initialize SPI bus */
SPI.begin();
}
void loop()
{
/* Initialize MFRC522 Module */
mfrc522.PCD_Init();
/* Look for new cards */
/* Reset the loop if no new card is present on RC522 Reader */
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
/* Select one of the cards */
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
/* Read data from the same block */
Serial.println();
Serial.println(F("Reading last data from RFID..."));
ReadDataFromBlock(blockNum, readBlockData);
/* If you want to print the full memory dump, uncomment the next line */
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
/* Print the data read from block */
Serial.println();
Serial.print(F("Last data in RFID:"));
Serial.print(blockNum);
Serial.print(F(" --> "));
for (int j=0 ; j<16 ; j++)
{
Serial.write(readBlockData[j]);
}
Serial.println();
digitalWrite(BUZZER, HIGH);
delay(200);
digitalWrite(BUZZER, LOW);
delay(200);
digitalWrite(BUZZER, HIGH);
delay(200);
digitalWrite(BUZZER, LOW);
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED))
{
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setFingerprint(fingerprint);
// Or, if you happy to ignore the SSL certificate, then use the following line instead:
// client->setInsecure();
data2 = data1 + String((char*)readBlockData);
data2.trim();
Serial.println(data2);
HTTPClient https;
Serial.print(F("[HTTPS] begin...\n"));
if (https.begin(*client, (String)data2))
{
// HTTP
Serial.print(F("[HTTPS] GET...\n"));
// start connection and send HTTP header
int httpCode = https.GET();
// httpCode will be negative on error
if (httpCode > 0)
{
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
}
else
{
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
delay(1000);
}
else
{
Serial.printf("[HTTPS} Unable to connect\n");
}
}
}
void ReadDataFromBlock(int blockNum, byte readBlockData[])
{
/* Prepare the ksy for authentication */
/* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF;
}
/* Authenticating the desired data block for Read access using Key A */
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("Authentication failed for Read: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Authentication success");
}
/* Reading data from the Block */
status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
if (status != MFRC522::STATUS_OK)
{
Serial.print("Reading failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println("Block was read successfully");
}
}