I have the nfc code, I put it in the json uid. I want to index so it doesn't have to be myArray[0] || myArray[1]. How do I set it up?
#include <SPI.h>
#include <MFRC522.h>
#include <Arduino_JSON.h>
#define RST_PIN D0
#define SS_PIN D8
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
const char input[] = "[\"7C 21 53 49\",\"83 95 E3 1A\",\"04 6A 5A 02 9C 53 80\"]";
const char* arraystring;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
// Prepare the key (used both as key A and as key B)
// using which is the default at chip delivery from the factory
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
// Check for compatibility
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K
&& piccType != MFRC522::PICC_TYPE_ISO_14443_4) {
Serial.println(F("No valid tag"));
return;
}
MFRC522::StatusCode status;
String content= "";
get_UID(content);
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
}
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
void get_UID(String content) {
JSONVar myArray = JSON.parse(input);
if (JSON.typeof(myArray) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
for (byte i = 0; i < mfrc522.uid.size; i++)
{
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
if (content.substring(1) == myArray[0] || myArray[1]) {
Serial.println("Tag true");
} else {
Serial.println("Tag false");
}
}
}
You can use a for-loop to iterate through your json array elements and compare.
Not familiar with json on Arduino but I think that this is a bug and should be
if (content.substring(1) == myArray[0] || content.substring(1) == myArray[1])
1 Like
How do we avoid having to type this just to find myArray[1], myArray[2], myArray[3]
As said, use a for-loop. JSON should have a way to determine how many elements are in myArray . It could look like below; you will have to figure out how to get the length (number of elements) of that myArray .
bool tagFound = true;
for(int cnt = 0; cnt < length_of_myArray; cnt++)
{
if (content.substring(1) != myArray[cnt])
{
tagFound = false;
break;
}
}
I have my doubts that it works because I doubt that myArray is an array of Strings. But as said, not familiar with JSON on Arduino.
blh64
July 24, 2023, 3:33pm
6
Why are you storing your UIDs in a JSON format? That seems like a very bad choice. If you are hard coding them into your sketch, just store them as a uint32_t array.
2 Likes
I tried using 2 cards and one of the cards couldn't be read properly
#include <SPI.h>
#include <MFRC522.h>
#include <Arduino_JSON.h>
#define RST_PIN D0
#define SS_PIN D8
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
const char input[] = "[\"7C 21 53 49\",\"83 95 E3 1A\",\"04 6A 5A 02 9C 53 80\"]";
bool tagFound = true;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
// Prepare the key (used both as key A and as key B)
// using which is the default at chip delivery from the factory
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
// Check for compatibility
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K
&& piccType != MFRC522::PICC_TYPE_ISO_14443_4) {
Serial.println(F("No valid tag"));
return;
}
MFRC522::StatusCode status;
String content= "";
get_UID(content);
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
}
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
void get_UID(String content) {
JSONVar myArray = JSON.parse(input);
if (JSON.typeof(myArray) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
for (byte i = 0; i < mfrc522.uid.size; i++)
{
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
for(int cnt = 0; cnt < myArray.length(); cnt++) {
if (content.substring(1) != myArray[cnt]) {
tagFound = false;
Serial.println("Tag false");
break;
}
Serial.println("Tag true");
break;
}
}
blh64
July 26, 2023, 12:02pm
8
again, why are you using JSON? Just use a String or a char array...
#include <SPI.h>
#include <MFRC522.h>
//#include <Arduino_JSON.h>
#define RST_PIN D0
#define SS_PIN D8
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
const char *input[] = { "7C215349", "8395E31A", "046A5A029C5380" };
const int numKeys = sizeof(input) / sizeof(input[0]);
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
// Prepare the key (used both as key A and as key B)
// using which is the default at chip delivery from the factory
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
// Check for compatibility
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K
&& piccType != MFRC522::PICC_TYPE_ISO_14443_4) {
Serial.println(F("No valid tag"));
return;
}
MFRC522::StatusCode status;
String content = "";
get_UID(content);
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
}
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
void get_UID(String content) {
for (byte i = 0; i < mfrc522.uid.size; i++)
{
if ( mfrc522.uid.uidByte[i] < 0x10 ) {
content.concat("0");
}
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
bool tagFound = false;
for (int cnt = 0; cnt < numKeys; cnt++) {
if ( strcmp(content.c_str(), input[cnt]) == 0) {
tagFound = true;
Serial.println("Tag true");
break;
}
}
if ( !tagFound ) {
Serial.println("Tag false");
}
}
system
Closed
January 22, 2024, 12:02pm
9
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.