extract read_card and send_uid functions

This commit is contained in:
Christophe Robillard 2025-11-08 11:10:37 +01:00
parent 8300244767
commit 6719cabaa7

View file

@ -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);
if (httpCode > 0) { client->setInsecure();
if (httpCode == HTTP_CODE_CREATED) { HTTPClient https;
displaySuccess(); Serial.print("[HTTPS] begin...\n");
} else { // configure traged server and url
displayError(); 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 { } 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[]) {