cannot convert 'String' to 'const char*

hi im kinda new to arduino but i was task todo this project where i have to interface arduino una + sd card +rfid + rtc . now before doing big code im taking it step by step coding each module individually but coming across interfacing 2 module im a bit having a problem with my code on the rfid and sd card . i give myself challenges weekly but im stuck with this almost 3 weeks no. my challenge was just simply putting 1 rfid id in a text file where when i tap my rfid it would just print access granted or denied. here is my code. to be honest im starting on doing random things just to make it work now . can you guys help me . i need to store my rfid scan digits to a variable then try looking for that var in my given text file.

sdsdsdcardfinalTRY.ino (2.06 KB)

What problem you would like help with?

If you are receiving the error "cannot convert 'String' to 'const char*" then you are trying to pass a String data type into a function that is expecting a character array. You can get two birds with one stone here though. Don't use String, anywhere anyway. When you change from String to character array (char[]) the error should go away (plus other problems you have yet to run into will never arise).

1 Like

OPs code.

#include <SD.h>
#include <MFRC522.h>
File myFile;
char buf[10];
#define SS_PIN 3 //rfid pin
#define RST_PIN 2 //rfid pin
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup()
{
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    
    while (!Serial) 
    {
        ; // wait for serial port to connect. Needed for Leonardo only
    }

    Serial.print("Initializing SD card...");
    pinMode(4, OUTPUT);//sd pin
    
    if (!SD.begin(10)) 
    {
        Serial.println("initialization failed!");
        return;
    }

    Serial.println("initialization done.");  
    // re-open the file for reading:
    SPI.begin();      // Initiate  SPI bus
    mfrc522.PCD_Init();   // Initiate MFRC522
    Serial.println("Approximate your card to the reader...");
    Serial.println();
    myFile = SD.open("test.txt");
    
    if (myFile) 
    {
        Serial.println("test.txt:");
        // read from the file until there's nothing else in it:
        while (myFile.available()) 
        {
            // Look for new cards
            if ( ! mfrc522.PICC_IsNewCardPresent()) 
            {
                return;
            }
            // Select one of the cards
            if ( ! mfrc522.PICC_ReadCardSerial()) 
            {
                return;
            }
            Serial.print("UID tag :");
            String content= "";
            char store= stringTochar(content);
            for (byte i = 0; i < mfrc522.uid.size; i++) 
            {
                Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
                Serial.print(mfrc522.uid.uidByte[i], HEX);
                content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
                content.concat(String(mfrc522.uid.uidByte[i], HEX));
            }
            myFile.read(buf,10);       
            if(strncmp(buf,content, 8) == 0)
            {
                Serial.println("Match!");
                break;     
            }
            else if (strncmp(buf,content, 8) == 1)
            {
                Serial.println("No Match!");
                break;     
            };
        }
        // close the file:
        myFile.close();
    } 
    else 
    {
        // if the file didn't open, print an error:
        Serial.println("error opening test.txt");
    }
}

void loop()
{
    // nothing happens after setup
}
//
char stringTochar(String s)
{
    char arr[12];
    s.toCharArray(arr, sizeof(arr));
    return atol(arr);
}

Personally, I'd like to know what you 'think' you're doing here?

char stringTochar(String s)
{
char arr[12];
s.toCharArray(arr, sizeof(arr));
return atol(arr);
}