Invalid conversion from 'int' to 'const char*' while reading SD card

Hi! I need to read a txt file from a sd card but I am always getting an error...(I don´t now why the read() funktion si giving me an int value...)
I saw a few topict about this problem but nothing worked for me..
Full error message:

SD_SERVER\SD_SERVER.ino: In function 'void readSSID()':
SD_SERVER:189:21: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
     ssid = file.read();

invalid conversion from 'int' to 'const char*' [-fpermissive]


My Full Code:

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <Adafruit_NeoPixel.h>
#include <ESPmDNS.h>

Adafruit_NeoPixel strip(12, 25, NEO_GRB + NEO_KHZ800);

const char* ssid;
const char* pas;
// Create AsyncWebServer object on port 806
AsyncWebServer server(80);

void initSDCard() {
  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();

  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }

  Serial.print("SD Card Type: ");
  if (cardType == CARD_MMC) {
    Serial.println("MMC");
  } else if (cardType == CARD_SD) {
    Serial.println("SDSC");
  } else if (cardType == CARD_SDHC) {
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }
  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);
}
//readFile(SD, "/config/AP_SSID.txt"), readFile(SD, "/config/AP_Pas.txt")
void initWiFi() {
  WiFi.softAP(ssid,pas);
  Serial.println(WiFi.softAPIP());
}

void setup() {
  Serial.begin(115200);
  strip.begin();
  strip.show();
  initWiFi();

  if(!MDNS.begin("starhopper")) {
     Serial.println("Error starting mDNS");
     return;
}
  initSDCard();

  server.on("/ap", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(SD, "/index.html", "text/html");

    int paramsNr = request->params();
    Serial.println(paramsNr);

    for (int i = 0; i < paramsNr; i++) {

      AsyncWebParameter* p = request->getParam(i);
      Serial.print("Param name: ");
      Serial.println(p->name());
      Serial.print("Param value: ");
      Serial.println(p->value());
      Serial.println("------");

      if(p->name()=="ap_ssid"){
      writeFile(SD, "/config/AP_SSID.txt", p->value().c_str());
      }
      else{
        writeFile(SD, "/config/AP_Pas.txt", p->value().c_str());
      }
     

    
}});

  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {

    request->send(SD, "/index.html", "text/html");

    int paramsNr = request->params();
    Serial.println(paramsNr);

    for (int i = 0; i < paramsNr; i++) {

      AsyncWebParameter* p = request->getParam(i);
      Serial.print("Param name: ");
      Serial.println(p->name());
      Serial.print("Param value: ");
      Serial.println(p->value());
      Serial.println("------");




      int number = (int)strtol(&(p->value().c_str())[1], NULL, 16);
      Serial.println(number);
      int r = number >> 16;
      int g = number >> 8 & 0xFF;
      int b = number & 0xFF;

      // DEBUG
      Serial.print("RGB: ");
      Serial.print(r, DEC);
      Serial.print(" ");
      Serial.print(g, DEC);
      Serial.print(" ");
      Serial.print(b, DEC);
      Serial.println(" ");


      for (int i = 0; i < 12; i++) {
        strip.setPixelColor(i, strip.Color( r, g, b ) );
        delay(50);
        strip.show();

      }

    }
  });



  server.serveStatic("/", SD, "/");

  server.begin();
}

void loop() {

}

























void writeFile(fs::FS &fs, const char * path, const char * message){
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if(!file){
    Serial.println("Failed to open file for writing");
    return;
  }
  if(file.print(message)){
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}



void readSSID(){
  File file = SD.open("/config/AP_SSID.txt");

  while(file.available()){
    ssid = file.read();
  }
  file.close();
}

Thanks for the help in advance :smiley:

Why do you think it should give you a 'const char *' value? That's the data type of the variable you're trying to assign the return value from read() to.

See Arduino Software Solutions for various sketche, with their pros and cons, to read from Serial, i.e. a Stream input which your file object is.
The Stream read() method returns an int not a char or char* See the sketches in the link.

ok but is there a way to read a const char* value from the sd card

the code above appears to repeatedly set "ssid" to a value read from EEPROM. it suggests the code is attempting to read a c-string from EEPROM.

but ssid is simply a pointer to c-string, the address of a char array in memory. there doesn't appear to be a a char array allocated to store a c-string.

the following may be what is attempted

#define SSID_LEN    20
char ssid [SSID_LEN];

    char *p = & ssid [0];
    int   n = 0;

    while (file.available()) {
        *p++ = file.read();
        if (SSID_LEN <= n)
            break;
    }

so this gives me a char value back. How can i convert it to a const char?

why?
aren't you reading a string of chars?

I think you forgot to increment n (and you might want to leave some space for the null terminator).

IIRC, the maximum WiFi SSID length is 32 characters, so you might want to use that instead of 20.

right
but still not sure this is what is desired?

ok i know that im probably a bit lost... I need to get the ssid from the webserver and then when the esp restarts i need to load it into the WiFi.softAP() funktion. I thougnt about saving the ssid(and later also the pasword) on the eeprom but i found no way to do it(do you now a not to complicated way? )... so i am trying to safe it on the sd card... The WiFi.softAP() funktion needs constant chars so i somehow need to get the value frome the sd card and feed it into the funktion.
Thanks for the help!!!

there's a difference between wrong and uncomplicated

what about

#define SSID_LEN  20
char ssid [SSID_LEN];

void writeSSID () {
    File file = SD.open("/config/AP_SSID.txt");
    file.write (ssid, SSID_LEN);
    file.close();
}

void readSSID(){
    File file = SD.open("/config/AP_SSID.txt");
    file.read (ssid, SSID_LEN);
    file.close();
}

Now i get the "cannot convert 'const String' to 'char' in assignment error" again

grafik
:cry:

ssid [SSID_LEN] = p->value;

not sure you understand that there is a difference between a "String" and a char array (i.e. c-string);

p->value is a "String", ssid is an array of chars. the above assigns a value to the SSID_LEN element of the array, which is actually one more than the length of the array (if SSID_LEN were 1, it would assign it to the 2nd element of a 1 byte array).

this may also explain your misunderstanding about using a const char * ptr.

let us know if this makes sense. others could probably guide you better with how copy the String to a char array

OK Why is this so hard... Is there maybe a way to directly read and write a string to the sd card?

see FS.h

looks like there's a readString() but not a writeString() (not sure).

see c_str()

C has char arrays. C++ introduced String. but the arduino seems to use a Java style of String and because String dynamically de/allocates memory (to make is easier to use), i'm told may lead to intractable problems

BTW you can't read into a constant anything, its constant.

It's not hard, per se. But you have to play by the language's rules. Try this inside the if() statement:

  strcpy(ssid, p->value().c_str());

that makes sense

I updated my code to:

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <Adafruit_NeoPixel.h>
#include <ESPmDNS.h>

Adafruit_NeoPixel strip(12, 25, NEO_GRB + NEO_KHZ800);
#define SSID_LEN  32
char ssid [SSID_LEN];

const char* pas = "12345678";
// Create AsyncWebServer object on port 806
AsyncWebServer server(80);

void initSDCard() {
  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();

  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }

  Serial.print("SD Card Type: ");
  if (cardType == CARD_MMC) {
    Serial.println("MMC");
  } else if (cardType == CARD_SD) {
    Serial.println("SDSC");
  } else if (cardType == CARD_SDHC) {
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }
  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);
}
//readFile(SD, "/config/AP_SSID.txt"), readFile(SD, "/config/AP_Pas.txt")
void initWiFi() {
  WiFi.softAP(ssid,pas);
  Serial.println(WiFi.softAPIP());
}

void setup() {
  Serial.begin(115200);
  strip.begin();
  strip.show();
          File file = SD.open("/config/AP_SSID.txt");
          itoa( file.read (), ssid, 10 );
          file.close();
          Serial.println(ssid);
  initWiFi();

  if(!MDNS.begin("starhopper")) {
     Serial.println("Error starting mDNS");
     return;
}
  initSDCard();

  server.on("/ap", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(SD, "/index.html", "text/html");

    int paramsNr = request->params();
    Serial.println(paramsNr);

    for (int i = 0; i < paramsNr; i++) {

      AsyncWebParameter* p = request->getParam(i);
      Serial.print("Param name: ");
      Serial.println(p->name());
      Serial.print("Param value: ");
      Serial.println(p->value());
      Serial.println("------");

      if(p->name()=="ap_ssid"){
        
        //strcpy(ssid, p->value().c_str());
        Serial.println("ssid recieved trying to write");
    if(!(File file = SD.open("/config/AP_SSID.txt"))){
      Serial.println("1");
    }
    if(!(file.write ((uint8_t)atoi((p->value().c_str()))))){
      Serial.println("2");
    }
    file.close();

      }
      else{
        
      }
     

    
}});

  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {

    request->send(SD, "/index.html", "text/html");

    int paramsNr = request->params();
    Serial.println(paramsNr);

    for (int i = 0; i < paramsNr; i++) {

      AsyncWebParameter* p = request->getParam(i);
      Serial.print("Param name: ");
      Serial.println(p->name());
      Serial.print("Param value: ");
      Serial.println(p->value());
      Serial.println("------");




      int number = (int)strtol(&(p->value().c_str())[1], NULL, 16);
      Serial.println(number);
      int r = number >> 16;
      int g = number >> 8 & 0xFF;
      int b = number & 0xFF;

      // DEBUG
      Serial.print("RGB: ");
      Serial.print(r, DEC);
      Serial.print(" ");
      Serial.print(g, DEC);
      Serial.print(" ");
      Serial.print(b, DEC);
      Serial.println(" ");


      for (int i = 0; i < 12; i++) {
        strip.setPixelColor(i, strip.Color( r, g, b ) );
        delay(50);
        strip.show();

      }

    }
  });



  server.serveStatic("/", SD, "/");

  server.begin();
}

void loop() {

}
``
I am getting no errors anymore but the reading Writing still does not work

what is this?