//This code allows a app to send a URI to the ESP32 via a POST request. //The ESP32 then writes the URI to an NFC card using the MFRC522 library. #include #include #include #include const char* ssid = "SSID_NAME"; // Replace with your network SSID const char* password = "SSID_PASSWORD"; // Replace with your network password #define RST_PIN D6 #define SS_PIN D7 MFRC522 mfrc522(SS_PIN, RST_PIN); WebServer server(80); String receivedUri = ""; // <- store latest URI bool writePending = false; // <- flag to indicate pending write void handleUriPost() { if (server.hasArg("uri")) { receivedUri = server.arg("uri"); writePending = true; // mark that we need to write Serial.println("Received URI: " + receivedUri); server.send(200, "text/plain", "URI received: " + receivedUri); } else { server.send(400, "text/plain", "Missing 'uri' parameter"); } } void setup() { Serial.begin(115200); SPI.begin(); mfrc522.PCD_Init(); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi..."); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected! IP address: "); Serial.println(WiFi.localIP()); server.on("/uri", HTTP_POST, handleUriPost); server.begin(); Serial.println("HTTP server started"); } void loop() { server.handleClient(); if (writePending) { Serial.println("Waiting for NFC card to write..."); MFRC522::MIFARE_Key key; for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; // Default key if (!mfrc522.PICC_IsNewCardPresent()) return; if (!mfrc522.PICC_ReadCardSerial()) return; Serial.print(F("Card UID: ")); for (byte i = 0; i < mfrc522.uid.size; i++) { Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i], HEX); } Serial.println(); // Prepare data byte buffer1[16]; byte buffer2[16]; const char* playlistID = receivedUri.c_str(); size_t len = strlen(playlistID); // Fill buffer1 with first 16 bytes for (byte i = 0; i < 16; i++) { buffer1[i] = (i < len) ? playlistID[i] : ' '; } // Fill buffer2 with next 16 bytes (if any) for (byte i = 0; i < 16; i++) { byte index = i + 16; buffer2[i] = (index < len) ? playlistID[index] : ' '; } // Write to block 1 byte block = 1; MFRC522::StatusCode status; status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); if (status != MFRC522::STATUS_OK) { Serial.print(F("Auth failed for block 1: ")); Serial.println(mfrc522.GetStatusCodeName(status)); return; } status = mfrc522.MIFARE_Write(block, buffer1, 16); if (status != MFRC522::STATUS_OK) { Serial.print(F("Write failed for block 1: ")); Serial.println(mfrc522.GetStatusCodeName(status)); return; } Serial.println(F("Block 1 written successfully.")); // Write to block 2 block = 2; status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); if (status != MFRC522::STATUS_OK) { Serial.print(F("Auth failed for block 2: ")); Serial.println(mfrc522.GetStatusCodeName(status)); return; } status = mfrc522.MIFARE_Write(block, buffer2, 16); if (status != MFRC522::STATUS_OK) { Serial.print(F("Write failed for block 2: ")); Serial.println(mfrc522.GetStatusCodeName(status)); return; } Serial.println(F("Block 2 written successfully.")); mfrc522.PICC_HaltA(); mfrc522.PCD_StopCrypto1(); Serial.println(F("Done writing playlist ID!")); writePending = false; // clear the flag after writing delay(3000); } }