ESP32 callback function with http response in setup() section

Hi all,

We are testing our library with http request on ESP32 WROOM 32. Here is onqe question that I can't figure out.

In the library, we'd like to process a http response sent from the server in the set up() section. The idea is like this.

setup(){
TokenCreate("hello.com", "hello.com",1440);
}

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);
  }

//Here is quite import for library and it doesn't work!!
   String msg = "";
    while (_networkClient->available()) {
      char c = _networkClient->read();
      msg += c;
    }
    Serial.println(msg);
    
}

The request has been successfully sent out. But the section "while" has been skipped somehow and it never get itself executed and we don't know why.

   String msg = "";
    while (_networkClient->available()) {
      char c = _networkClient->read();
      msg += c;
    }
    Serial.println(msg);
    

As you can see, we are waiting the msg sent back from server(it does send message!!) but it prints blanket as if it doesn't exist.

BTW: _networkClient is &Client

Help us please!

This is done a few microseconds after the request was sent…

You need to wait until something becomes available (or timeout) before trying to read the answer

A blocking test

    while (_networkClient->available() == 0)  yield(); // wait forever until something comes back
    String msg = "";
    while (_networkClient->available()) {
      char c = _networkClient->read();
      msg += c;
    }
    Serial.println(msg);

Hi,

It's working! Thank! Can I have more information of the function "yielding()?"

BR

The ESPs and other advanced boards (not the UNO) need to run multiple functions "In the background" like keeping WiFi connected, managing the TCP/IP stack etc.

the code architecture is such that when you call delay() or when the loop() loops, those tasks are given a bit of time to complete their work but if your code is blocking for too long — like an active wait on a button press or some network event or whatever — these functions will be prevented from running and the watchdog will kick-in believing that the arduino has crashed and this can cause your ESP to reset itself.

So one way to avoid such unwanted reboot is to not have blocking code but if you need to then calling yield() (or delay(0) will give a chance to those functions to run.

on small arduinos like a UNO, the function is defined as weak, meaning it can be overridden, and is an alias for a function doing nothing

so it does not hurt on a UNO to call yield() when you have blocking code and if you take this code to an ESP you'll be in a good place