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

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?

https://en.cppreference.com/w/cpp/language/lambda

answers the request of a client(WebServer...)

Hello i need to read write a string to a sd card but everything i tried does not work...

What exactly have you tried? There are many examples that come with the SD library. Posting the code you tried, along with any errors would be helpful.

If you do post your code, please read the sticky post at the top of the forum to learn how to do it properly with code tags. It helps people help you.

Finally, there is a difference between a 'String' and a 'string' so which is it? Your title and text do not match.

shouldn't cross post

@bs_tuejj Topics merged

Why did you start a second one ?

i think it's ironic that someone would use a lamda function and not understand String vs c-string.

also ironic that the lamda function was duplicated ?? (why not inline)?

Stroustrup said developers shouldn't use features unnecessarily

Im new to the forum and i thougt the topic of this post wasnt on point of the problem any more...

Not intractable, see Taming Arduino Strings for the solutions.

The libraries being used here are already using Strings all over the place. There's no reason here to copy them to char arrays manually.

The use case here is an argument to a function that takes a function pointer. What would the syntax be for doing that "inline"?

Per above, I think either a lambda for a named function would be necessary.

Why pick a lambda? In the case I'll bet OP, like most newbies, simply copied what was in the examples.