diff --git a/the-cup.ino b/the-cup.ino index 4f1bbea..4b7cf62 100644 --- a/the-cup.ino +++ b/the-cup.ino @@ -34,6 +34,9 @@ MFRC522DriverSPI driver_1{ss_1_pin}; MFRC522 reader{driver_1}; // Create MFRC522 instance. +void read_card(char reader_uid[]); +void send_uid(char reader_uid[]); + void setup() { Serial.begin(115200); @@ -59,40 +62,49 @@ void setup() { } void loop() { - auto client = std::make_unique(); if (reader.PICC_IsNewCardPresent() && reader.PICC_ReadCardSerial()) { - reader.PICC_HaltA(); - reader.PCD_StopCrypto1(); char reader_uid[32] = ""; - byte_to_string(reader.uid.uidByte, 4, reader_uid); - Serial.println(reader_uid); + read_card(reader_uid); + send_uid(reader_uid); + } +} - client->setInsecure(); - HTTPClient https; - Serial.print("[HTTPS] begin...\n"); - // configure traged server and url - https.begin(*client, "https://mood.robiweb.net/rfid_tags"); // HTTP - https.addHeader("Content-Type", "application/json"); +void read_card(char reader_uid[]) { + reader.PICC_HaltA(); + reader.PCD_StopCrypto1(); + byte_to_string(reader.uid.uidByte, 4, reader_uid); + Serial.println(reader_uid); +} - // 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 { - displayError(); - } +void send_uid(char reader_uid[]) { + auto client = std::make_unique(); + + client->setInsecure(); + HTTPClient https; + Serial.print("[HTTPS] begin...\n"); + // configure traged server and url + https.begin(*client, "https://mood.robiweb.net/rfid_tags"); // HTTP + https.addHeader("Content-Type", "application/json"); + + // 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 { displayError(); } - - https.end(); - Serial.print("[HTTPS] ended...\n"); + } else { + displayError(); } + + https.end(); + Serial.print("[HTTPS] ended...\n"); + } void byte_to_string(byte array[], unsigned int len, char buffer[]) {