ESP8266 Node MCU receiving commands in JSON

Asked

Viewed 48 times

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.

1 answer

2

Your main problem (no longer searched) is in the function str_to_char().

You are creating a local variable in that function with the name strAux. This variable ceases to exist once the function ends, but returns its address to the calling function. When the calling function tries to use this value it gives error because the object no longer exists.

You have several solutions (in order of preference)

  1. Creates the target object in the calling function and passes it as parameter

    char *str_to_char(char *dst, String S) {
      strcpy(dst, &S[0]);
      return dst;
    }
    //...
        char proto[1000]; // tamanho suficiente
        str_to_char(proto, str);
    //...
    
  2. Usa malloc() in function

    char *str_to_char(String S) {
      char *dst = malloc(strlen(S) + 1);
      if (!dst) exit(EXIT_FAILURE);
      strcpy(dst, S);
      return dst;
    }
    //...
        char *proto;;
        proto = str_to_char(str);
        // usar proto
        free(proto);
    //...
    
  3. Usa static so that the object is not destroyed when the function ends

    char *str_to_char(String S) {
      static char strAux[1000]; // static: não usar em threads ou paralelismo
      strcpy(strAux, &S[0]);
      return strAux;
    }
    // ...
        char *proto = str_to_char();
    // ...
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.