ESP32-WROOM-32 http message handle and memory issue

Hi all,

We are currently testing a library related to the IoT Platform with ESP32-WROOM-32 now.

_token is defined in .h file.
In one file HttpDownstream.cpp:

void HttpDownstreamClient::tokenCreate(char* URL, char* Subname, char* Suber, int expiretime) {

  Serial.print("The subscription is associated with the Subscripter: ");
  Serial.println(Suber);

  StaticJsonDocument<200> root;
 
  root["subscriber"] = Suber;
  root["subscription"] = Subname;
  root["expiresInMinutes"] = expiretime;
  String body2send = "";

  serializeJsonPretty(root, body2send);

  if (_networkClient->connect(URL, 443)) {
    Serial.println("Connected to the server to request token!");
    // Make a HTTP request:
    _networkClient->println("POST /notification2/token HTTP/1.1");
    _networkClient->print("Host: ");
    _networkClient->println(URL);
    _networkClient->print("Authorization: Basic ");
    _networkClient->println(_base64);
    _networkClient->println("Content-Type: application/json");
    _networkClient->print("Content-Length: ");
    _networkClient->println(body2send.length());
    _networkClient->println("Accept: application/json");
    _networkClient->println();
    _networkClient->println(body2send);

  }

//Start to handle incoming message sent from server
while (_token.length() == 0) {
    String msg = "";
    while (_networkClient->available()) {
      char c = _networkClient->read();
      msg += c;
    }
    Serial.println("debug!!!");
    Serial.println(msg);
    int start_init = msg.indexOf("\"token\"");
    int start = msg.indexOf(":",start_init);
    int until_n = msg.indexOf("\"", start + 3);
    Serial.println(until_n);
    if (start != -1 && until_n != -1 ) {
      _token = msg.substring(start + 2, until_n);
      Serial.print("Token is: ");
      Serial.println(_token);
     }
  }
 
}

In test.io we have:

void setup(){

//WiFi setting is not showed here

c8yclient.tokenCreate(host, SubscriptionName, SubscriberName, expiretime);

}

void loop()
{
  while (wifisecure.available()) {
    char c = wifisecure.read();
    Serial.print(c);
  }  
}

What I get is something like this:

21:31:57.993 -> The subscription is associated with the Subscripter: Test2Sub1
21:31:58.033 -> Connected to the server to request token!
21:31:58.073 -> debug!!!
21:31:58.073 -> ⸮
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!
21:31:58.073 -> 
21:31:58.073 -> -1
21:31:58.073 -> debug!!!

What I can make sure now is that the https request has been sent out. However, the incoming message from the server is not "well" processed. IMO there must be something wrong in memory since we have a "⸮" printed.

We tried this library with MKR wifi 1010, Arduino Uno rev 2 and nano 33 IoT. None of these boards has this issue. What has happened to ESP32? How can I fix this?

Many thanks in advance!

the code should only process "msg" if one has been received and after the char representig the end of a msg has been received

consider

    if (_! networkClient->available())
          return;
   char c = _networkClient->read();
   if (TERM_CHAR != c)
          return;

But the "while" condition part should stay, right?

What does "TERM_CHAR" mean and is it a predefined parameter in ESP32? Sorry for asking this silly question...

The "msg" that I expected is quite similar to what is listed as following:

"token": "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ0ZXN0U3Vic2NyaWJlciIsInRvcGljIjoibWFuYWdlbWVudC9yZWxub3RpZi90ZXN0U3Vic2NyaXB0aW9uIiwianRpIjoiZGRiNDRhNjQtMDQzMC00ODUxLWJmMGYtYjI4YWViN2JhNDYwIiwiaWF0IjoxNjMwNDA0NTk3LCJleHAiOjE2MzA0MDk5OTd9.Q56PSRp1HYWw2CjutzV1X9Qhtck0VLLy1GKVqN7kRqYoSpLuDJ0-6Kk_XvvT4NI-xRvu8ZwkWePK-hXLgZmpfs10n54rzLiF195dYnME3r2NuEopkCIIaiqKn6--gxxs0USKgRZ6rmeAKGbdc0bJVBQy_RURyrxPYSG2hYJKflwIKzVkyUsrTPbHRH5Sr_Ip35m7wb9YrYjLTWIYuYSwHqYnX3siBfjY6XSwrIgWJRT-YmYfRJ9LyaMyVdLAuwMQbz5fsytkrzIH7TEyWHK3B0nNIpNOawbjB9d1wFauGhtQ2GcDjiz3lXPLtSnt5pLBA_MzEQGSHXR6mz6ObaYpQQ"

The content for token is not the same.

in general i would say no.

it seems you send some message and expect a complete response to be immediately available.

but besides that issue, it seems to be a common mistake, that the processing of some message is performed regardless of whether there is any received data or if the data is complete.

it may make sense in this context to wait for a complete message. this means repeatedly checking if -- and i mean if() -- something is available and reading either a character or up to an expected last character and then only processing when the received message is complete, at which time that wait loop can be terminated

it's not obvious to me what the last character is. is it a NULL ('\0) or possible a linefeed ('\n'). i forget if any http message is always terminated by a linefeed

not sure if this is clear enough for you to understand

Thank for your answer! Now I understand the meaning and I will try to fix this as you have indicated.

I still think it is the problem of the memory. We tried on other MKR wifi 1010 and arduino uno rev2 with the same code and thez both work. It must have something to do with the ESP32 board.

yes, it's much faster and can complete an iteration of the while loop before the next character is available.

the code should be written to receive a complete complete message, not read bytes until another one is not available.

Serial.readBytesUntil() could be used if there is a termination character such as a NULL or linefeed ('\n')

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.