From 04999e8c5558008d1fc2e5cb857626c82a077599 Mon Sep 17 00:00:00 2001 From: Francisco Solis <30329003+Im-Fran@users.noreply.github.com> Date: Tue, 30 Apr 2024 18:09:23 -0400 Subject: [PATCH] Create sketch_apr29a.ino --- sketch_apr29a.ino | 121 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 sketch_apr29a.ino diff --git a/sketch_apr29a.ino b/sketch_apr29a.ino new file mode 100644 index 0000000..f8ced7a --- /dev/null +++ b/sketch_apr29a.ino @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include + +#define SS_PIN 2 // SDA pin connected to D4 +#define RST_PIN 0 // RST pin connected to D3 + +MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance + +const char *ssid = ""; +const char *password = ""; + +void setup() { + Serial.begin(115200); + Serial.println(""); + Serial.print("Conectandose al Wi-Fi "); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.print("Conectado con la IP: "); + Serial.println(WiFi.localIP()); + SPI.begin(); // Init SPI bus + mfrc522.PCD_Init(); // Init MFRC522 + + Serial.println("RFID-RC522 inicializado."); +} + +void loop() { + // Look for new cards + if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { + Serial.println("NFC tag detectado!"); + + // Get NFC tag UID + String tagUID = getTagUID(); + + // Connect to the server and send the UID + if (tagUID != "") { + if (sendTagUID(tagUID)) { + Serial.println("Tag UID enviado correctamente."); + } else { + Serial.println("Error al enviar el Tag UID."); + } + } else { + Serial.println("Error al obtener el Tag UID."); + } + } + + mfrc522.PICC_HaltA(); // Stop reading + delay(1000); +} + +String getTagUID() { + String tagUID = ""; + for (byte i = 0; i < mfrc522.uid.size; i++) { + tagUID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""); + tagUID += String(mfrc522.uid.uidByte[i], HEX); + } + return tagUID; +} + +bool sendTagUID(String tagUID) { + std::unique_ptrclient(new BearSSL::WiFiClientSecure); + client->setInsecure(); + HTTPClient http; + String url = "https://api-lector-nfc.exdev.cl/" + tagUID; + Serial.print("Enviando solicitud a: "); + Serial.println(url); + + if (http.begin(*client, url)) { + int httpCode = http.GET(); + if (httpCode > 0) { + if (httpCode == HTTP_CODE_OK) { + DynamicJsonDocument doc(1024); + DeserializationError error = deserializeJson(doc, http.getString()); + if (!error) { + String status = doc["status"]; + if (status == "ok") { + String nombre = doc["nombre"]; + String puesto = doc["puesto"]; + Serial.println("Nombre: " + nombre); + Serial.print("Puesto: " + puesto); + Serial.println(); + return true; + } else { + Serial.println("Error desconocido en la respuesta JSON."); + } + } else { + Serial.println("Error al analizar el JSON."); + } + } else { + if(httpCode == 404) { + DynamicJsonDocument doc(1024); + DeserializationError error = deserializeJson(doc, http.getString()); + if(!error) { + String errorMessage = doc["error"]["mensaje"]; + Serial.println(errorMessage); + } else { + Serial.println("Error al analizar el JSON."); + } + } else { + Serial.print("Obtenido codigo de error: "); + Serial.println(httpCode); + } + } + } else { + Serial.println("Conexión fallida. Intenta más tarde."); + } + http.end(); + } else { + Serial.println("Failed to connect to server."); + } + return false; +}