1
Well I am coding an ESP8266 in C in which ESP receives a command through JSON: I have to take the data from this JSON (a String) and convert them to char* then be able to send this data to perform an http or https connection. Here’s the code:
char* str_to_char(String S) {
char strAux[S.length() + 1];
strcpy(strAux, &S[0]);
return strAux;
}
void getBufferData(DynamicJsonDocument dataBuf) {
String str;
if (dataBuf.containsKey("address")) {
if (dataBuf["address"].containsKey("proto")) {
str = dataBuf["address"]["proto"].as<String>();
proto = str_to_char(str);
Serial.print("proto: "); Serial.println(proto);
}
if (dataBuf["address"].containsKey("host"))
host = str_to_char(dataBuf["address"]["host"].as<String>());
if (dataBuf["address"].containsKey("path"))
path = str_to_char(dataBuf["address"]["path"].as<String>());
if (dataBuf["address"].containsKey("file"))
file = str_to_char(dataBuf["address"]["file"].as<String>());
}
if (dataBuf.containsKey("coordinates")) {
if (dataBuf["coordinates"].containsKey("X"))
crdX = dataBuf["coordinates"]["X"].as<int16_t>();
if (dataBuf["coordinates"].containsKey("Y"))
crdY = dataBuf["coordinates"]["Y"].as<int16_t>();
}
Serial.print("host: "); Serial.println(host);
Serial.print("path: "); Serial.println(path);
Serial.print("file: "); Serial.println(file);
delay(10000);
}
Only in the print of the end of the function it brings a lot of nonsense, and nothing like a string, seems more binary.
Note that in the first instruction I try to bring everything to variables but it did not work.