extract read_card and send_uid functions
This commit is contained in:
parent
8300244767
commit
6719cabaa7
1 changed files with 37 additions and 25 deletions
62
the-cup.ino
62
the-cup.ino
|
|
@ -34,6 +34,9 @@ MFRC522DriverSPI driver_1{ss_1_pin};
|
||||||
|
|
||||||
MFRC522 reader{driver_1}; // Create MFRC522 instance.
|
MFRC522 reader{driver_1}; // Create MFRC522 instance.
|
||||||
|
|
||||||
|
void read_card(char reader_uid[]);
|
||||||
|
void send_uid(char reader_uid[]);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
|
@ -59,40 +62,49 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
auto client = std::make_unique<BearSSL::WiFiClientSecure>();
|
|
||||||
|
|
||||||
if (reader.PICC_IsNewCardPresent() && reader.PICC_ReadCardSerial()) {
|
if (reader.PICC_IsNewCardPresent() && reader.PICC_ReadCardSerial()) {
|
||||||
reader.PICC_HaltA();
|
|
||||||
reader.PCD_StopCrypto1();
|
|
||||||
char reader_uid[32] = "";
|
char reader_uid[32] = "";
|
||||||
byte_to_string(reader.uid.uidByte, 4, reader_uid);
|
read_card(reader_uid);
|
||||||
Serial.println(reader_uid);
|
send_uid(reader_uid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
client->setInsecure();
|
void read_card(char reader_uid[]) {
|
||||||
HTTPClient https;
|
reader.PICC_HaltA();
|
||||||
Serial.print("[HTTPS] begin...\n");
|
reader.PCD_StopCrypto1();
|
||||||
// configure traged server and url
|
byte_to_string(reader.uid.uidByte, 4, reader_uid);
|
||||||
https.begin(*client, "https://mood.robiweb.net/rfid_tags"); // HTTP
|
Serial.println(reader_uid);
|
||||||
https.addHeader("Content-Type", "application/json");
|
}
|
||||||
|
|
||||||
// start connection and send HTTP header and body
|
void send_uid(char reader_uid[]) {
|
||||||
int httpCode = https.POST("{\"identifier\":\"" + String(reader_uid) + "\"}");
|
auto client = std::make_unique<BearSSL::WiFiClientSecure>();
|
||||||
Serial.print("httpCode: ");
|
|
||||||
Serial.println(httpCode);
|
client->setInsecure();
|
||||||
|
HTTPClient https;
|
||||||
if (httpCode > 0) {
|
Serial.print("[HTTPS] begin...\n");
|
||||||
if (httpCode == HTTP_CODE_CREATED) {
|
// configure traged server and url
|
||||||
displaySuccess();
|
https.begin(*client, "https://mood.robiweb.net/rfid_tags"); // HTTP
|
||||||
} else {
|
https.addHeader("Content-Type", "application/json");
|
||||||
displayError();
|
|
||||||
}
|
// start connection and send HTTP header and body
|
||||||
|
int httpCode = https.POST("{\"identifier\":\"" + String(reader_uid) + "\"}");
|
||||||
|
Serial.print("httpCode: ");
|
||||||
|
Serial.println(httpCode);
|
||||||
|
|
||||||
|
if (httpCode > 0) {
|
||||||
|
if (httpCode == HTTP_CODE_CREATED) {
|
||||||
|
displaySuccess();
|
||||||
} else {
|
} else {
|
||||||
displayError();
|
displayError();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
https.end();
|
displayError();
|
||||||
Serial.print("[HTTPS] ended...\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
https.end();
|
||||||
|
Serial.print("[HTTPS] ended...\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void byte_to_string(byte array[], unsigned int len, char buffer[]) {
|
void byte_to_string(byte array[], unsigned int len, char buffer[]) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue