RFID Stops detecting New Cards after HTTP POST, ESP32

I'm making an RFID music player. One of the player's functions is to turn the LED lights in my room a different color depending on what's being played. I use WLED API to send a POST request to the lights in my room. This works, and the lights change color. However, the RFID module stops detecting new cards after that. I have made sure that the program is still looping, I have no clue what's going on here. I have tried delays and no luck. Any help is greatly appreciated. Here is my code and PIN setup for both the RFID module and SD card module

  • RST/Reset RST 21
  • SPI SS SDA(SS) 5/15
  • SPI MOSI MOSI 23
  • SPI MISO MISO 19
  • SPI SCK SCK 18
#include <SPI.h>
#include <MFRC522.h>
#include <SD.h>
#include "AudioGeneratorWAV.h"
#include "AudioOutputSerialWAV.h"
#include "AudioFileSourceSD.h"
#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "MY SSID";
const char* password = "MY PASSWORD";

const char* serverName = "SERVER NAME";

AudioGeneratorWAV *mp3; //change to MP3 for actual
AudioFileSourceSD *file;
AudioOutputSerialWAV *out;//change to i2s

#define RFID_SS_PIN 5
#define RST_PIN 21

#define SD_SS_PIN 15

byte current_card[4];
byte current_song = 0;
byte playing = 0;

String ext = ".wav";

MFRC522 rfid(RFID_SS_PIN, RST_PIN); // Instance of the class
void setup() { 

  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");  // Change to an led or something 
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());

  SPI.begin();

 // pinMode(RFID_SS_PIN, OUTPUT);
  pinMode(SD_SS_PIN, OUTPUT);
  pinMode(4, OUTPUT);

  switch_to_rfid();
  rfid.PCD_Init(); // Init MFRC522

  out = new AudioOutputSerialWAV();
  mp3 = new AudioGeneratorWAV(); 

}
 
void loop() {
  switch_to_sd();
  if(! SD.begin(SD_SS_PIN)){
    Serial.println("SD ERROR");
    return;
  }

  switch_to_rfid();
  // Authenticate Block
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  byte block   = 1;
  MFRC522::StatusCode status;

  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent()){
    return;
  }

  digitalWrite(4, HIGH);
  delay(500);
  digitalWrite(4, LOW);

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial()){
    return;
  }

  Serial.print("Card detected...");

  // Is it Really a new cArd?
  byte identity[rfid.uid.size];
  for(byte i = 0; i < rfid.uid.size; i++){
    identity[i] = rfid.uid.uidByte[i];
  }

 /* if(! new_card(identity, current_card, rfid.uid.size)){
    Serial.println("Not Exactly a new Card");
    delay(50);
    return;
  }

  for(byte i = 0; i < rfid.uid.size; i++){
    current_card[i] = identity[i];
  }
  Serial.println("Actuaslluy a new Card");
*/
  // Authenticate Block
  status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(rfid.uid));
  if (status != MFRC522::STATUS_OK) {
   Serial.print("PCD_Authenticate() failed: ");
   Serial.println(rfid.GetStatusCodeName(status));
   return;
}

  //READ AND PRINT BLOCK 1
  byte buffer[18];
  byte size = sizeof(buffer);
  
  status = rfid.MIFARE_Read(block, buffer, &size);

  // Did Reading fail?
  if (status != MFRC522::STATUS_OK){
    Serial.print("Reading failed: ");
    Serial.print(rfid.GetStatusCodeName(status));
    return;
  }

  for(byte i = 0; i < 16; i++){
    Serial.print(buffer[i]);
  }
  Serial.println();
  
  delay(1000);
  rfid.PCD_StopCrypto1();
  rfid.PICC_HaltA();

  switch_to_sd();
  file = new AudioFileSourceSD();
  String albumname = "/" + String(buffer[0]) + String(buffer[1]) + String(buffer[2]) + String(buffer[3]);
 
  if(! SD.exists(albumname)){
    Serial.print("Album not found");
    Serial.println();
    return;
  }

  //Check WiFi connection status
  if(WiFi.status()== WL_CONNECTED){
    WiFiClient client;
    HTTPClient http;

    // Your Domain name with URL path or IP address with path
    http.begin(client, serverName);

    // Specify content-type header
    http.addHeader("Content-Type", "application/json");
    // Data to send with HTTP POST
    String ledData = "{\"seg\":[{\"col\":[[255,100,200]]}]}";           
    // Send HTTP POST request
    int httpResponseCode = http.POST(ledData);     
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode); 
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }

  delay(1000);

}


// Functions
void switch_to_rfid(){
  digitalWrite(RFID_SS_PIN, LOW);
  digitalWrite(SD_SS_PIN, HIGH);
}

void switch_to_sd(){
  digitalWrite(SD_SS_PIN, LOW);
  digitalWrite(RFID_SS_PIN, HIGH);
}

bool new_card(byte identity[], byte current[], byte size){
  for(byte i = 0; i < size; i++){
    if(identity[i] != current[i]) return 1;
  }
  return 0;
}

int numberofsongs(String directoryPath) {
  File directory = SD.open(directoryPath);

  if (!directory || !directory.isDirectory()) {
    // If the directory doesn't exist or is not a directory, return -1 as an error indicator.
    return -1;
  }

  int fileCount = 0;
  while (File file = directory.openNextFile()) {
    if (String(file.name()).endsWith(ext)) {
      fileCount++;
    }
    file.close();
  }

  directory.close();
  return fileCount - 1;
}

  delay(1000);
  rfid.PCD_StopCrypto1();
  rfid.PICC_HaltA();

Could you explain the rationale behind delaying the rfid.PICC_HaltA call so late in the process please?

Hi, This code started from me reading the UID of the RFID cards and printing them on Serial and so the code stopped @ the PICC_HALT. If I didn't add the delay, then when I moved the card away during the testing process, it would be "cut off" in the middle of the loop and I would have a timeout error. The delay helps with that bit Adding a little bit of time before the next loop starts, because you're probably not "fast enough" to take the card away as its being read Is it necessary now that I have the SD module, no I guess not. But that was kind of the reasoning , I hope that makes sense.

I see. Good luck with that approach.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.