convert string to char

Hi all!

i want to convert string to char.

my string (Json) is a payload from a post request.

i do that to sent this Json data from NodeMcu to Mega.

code:

#include <Arduino.h>
#include <ArduinoJson.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClientSecureBearSSL.h>
// Fingerprint for demo URL, expires on June 2, 2019, needs to be updated well before this date
// const uint8_t fingerprint[20] = {0x5A, 0xCF, 0xFE, 0xF0, 0xF1, 0xA6, 0xF4, 0x5F, 0xD2, 0x11, 0x11, 0xC6, 0x1D, 0x2F, 0x0E, 0xBC, 0x39, 0x8D, 0x50, 0xE0};

ESP8266WiFiMulti WiFiMulti;

char post_ping() {

  Serial.begin(115200);
  // Serial.setDebugOutput(true);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("xxxxxx", "xxxxx");

  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);

    //client->setFingerprint(fingerprint);
    client->setInsecure();

    HTTPClient https;

    Serial.print("[HTTPS] begin...\n");
    if (https.begin(*client, "https://xxxxxxxxxxxxx")) {  // HTTPS

      Serial.print("[HTTPS] POST...\n");
      // start connection and send HTTP header
      int httpCode = https.POST("borago"); 

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTPS] POST... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
       char copy[16];
      payload.toCharArray(copy, 16);

      s.begin(115200);
      Serial.print("================envoie payload à mega============");
Serial.print("/n");
Serial.print(copy);


  //s.write(copy);


        }
      } else {
        Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    } else {
      Serial.printf("[HTTPS] Unable to connect\n");
    }

      

  }

  Serial.println("Wait 10s before next round...");

}

and i got this :

[SETUP] WAIT 4…
[SETUP] WAIT 3…
[SETUP] WAIT 2…
[SETUP] WAIT 1…
[HTTPS] begin…
[HTTPS] POST…
[HTTPS] POST… code: 200
{⸮j

Thank you all !

The first thing you need to do is get your terminology right. The data type of your payload variable is String. A null terminated array of type char is called a string. Despite the similarities between the two names, String and string are two completely different things. Please be very careful to use the correct terms to avoid confusion.

So what you really are trying to say is: "i want to convert String to string."

You seem to be taking the right approach to this already. The trick with strings is that you need to allocate enough space in the array. Always remember to leave space for the null terminator at the end of the string. I don't know where you came up with the number 16, or whether that provides space for the null terminator. My recommendation would be to use the length() function of the String class to determine the length of payload:

byte payloadLength=payload.length();
char copy[payloadLength + 1];  // size the array to hold the payload and the null terminator
payload.toCharArray(copy, payloadLength + 1);

Of course the best thing is to just avoid using String altogether. It looks like the way to do this would be using https.writeToStream(). I don't have experience with the ESP8266HTTPClient library, but from looking at the ReuseConnection example that comes with it, it appears you could just do this:

https.writeToStream(&Serial);