Problem with the <MFRC522.h> library

Hello guys, I'm currently working on a project that consist of an Arduino MKR WiFi 1010 with a RFID Reader (RC522) that writes the UID of the RFID tags on an MQTT topic. My issue is that I can't find how to convert de UID (Byte) to String.

=> Goal : Converting Bytes to String and using the String in a Switch/Case operation where it sends different data to MQTT depending on the UID that got read.

=> Btw sorry for my bad english, trying to do my best to communicate my issue... :slight_smile:

=> Libraries used in my program :

=> My code :

//Include libraries
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include <SPI.h>
#include <MFRC522.h>

//Pin definition
#define RST_PIN 6
#define SS_PIN 11

//Reader instance
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;

//WiFi Config
const char ssid[] = "xxx";
const char pass[] = "xxx";

//MQTT Config
const char broker[] = "xxx";
int port = 1883;
const char user[] = "xxx";
const char password[] = "xxx";
const char topic[] = "xxx";

//Interval
const long interval = 1000;
unsigned long prevMillis = 0;

//Client config
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

void setup() {
  pinMode(2, OUTPUT);
  //Serial monitor start
  Serial.begin(115200);
  while (!Serial)
    ;

  //SPI Start
  SPI.begin();

  //WiFi connection
  Serial.print("Connecting to WiFi : ");
  Serial.print(ssid);
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    Serial.print(".");
    delay(5000);
  }
  Serial.println();
  Serial.println("WiFi Connected ");
  Serial.println();

  //Connecting to MQTT
  Serial.print("Connecting to MQTT ");
  Serial.println(broker);
  mqttClient.setUsernamePassword(user, password);
  if (!mqttClient.connect(broker, port)) {
    Serial.print("Couldn't connect to MQTT, error code = ");
    Serial.print(mqttClient.connectError());
    while (1)
      ;
  }
  Serial.println("Connected to MQTT succesfully!");
  Serial.println();

  //Reader INIT
  mfrc522.PCD_Init();
  mfrc522.PCD_DumpVersionToSerial();
}


void loop() {
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);

  Serial.print(F("UID du tag RFID : "));
  printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println();

  mfrc522.PICC_HaltA();

  mqttClient.poll();
}

void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
  if (buffer() == "E3 61 86 16") {
    digitalWrite(2, HIGH);
  }
}

Any help would be appreciated :slight_smile:

This will not happen, C++ allows only numeric switch case constructs.

As far I see, your buffer variable is a byte array. But you try to compare it with a string. This comparision will never match.

Byte array
buffer[] = {0xE3, 0x61, 0x86, 0x16}
and a c-string
"E3 61 86 16"
are two completely different pieces of data. For example, the length of the first is 4 bytes only, but the length of the second string is 12 bytes.

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