Hello,
My following code ( on a ESP32-Oled-Lora module) never comes back from the call :
String response = http.getString();
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "******************";
const char* password = "**************";
void setup() {
Serial.begin(115200);
delay(2000); //Delay needed before calling the WiFi.begin
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { //Check for the connection
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
}
void loop() {
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
HTTPClient http;
http.begin("http://mainrouter:85/wf.php");
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpResponseCode = http.POST("data=abc&targetfile=ds.txt"); //Send the actual POST request
if(httpResponseCode>0){
Serial.print("httpResponseCode>0 : ");
Serial.println(httpResponseCode); //Print return code
String response = http.getString(); // <- NEVER COMES BACK !!!!!!!!!!!!!
Serial.println(httpResponseCode); //Print return code
Serial.println(response); //Print request answer
}else{
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); //Free resources
}else{
Serial.println("Error in WiFi connection");
}
delay(5000); //Send a request every 5 seconds
}
It`s an error, very similar to the one, discussed here :ESP32 get returned json from HTTPs Get - Programming Questions - Arduino Forum
By debugging it down, I found, that WiFiClient.getString() calls the end() function, which calls the flush()-function.
In the flush() function is a while-loop, which never ends ; in my case, a=2 and res=0, so 2-0 will stay forever 2. see code here
// Though flushing means to send all pending data,
// seems that in Arduino it also means to clear RX
void WiFiClient::flush() {
int res;
size_t a = available(), toRead = 0;
if(!a){
return;//nothing to flush
}
uint8_t * buf = (uint8_t *)malloc(WIFI_CLIENT_FLUSH_BUFFER_SIZE);
if(!buf){
return;//memory error
}
while(a){ // WHILE NEVER BREAKS a=2
Serial.print("Start : WiFiClient::flush() while(a) a : ");
Serial.println(a);
toRead = (a>WIFI_CLIENT_FLUSH_BUFFER_SIZE)?WIFI_CLIENT_FLUSH_BUFFER_SIZE:a;
res = recv(fd(), buf, toRead, MSG_DONTWAIT); // AND res = 0
Serial.print("res = recv(fd(), buf, toRead, MSG_DONTWAIT) res : ");
Serial.println(res);
if(res < 0) {
log_e("%d", errno);
stop();
break;
}
a -= res; // SO IT CALCULATES 2-0 !!!!!!!
}
free(buf);
}
If I change the line
if(res < 0) {
to
if(res <= 0) {
it seems to work ; but I don’t feel well, because I don’t know, what other sideffects follow….
By the way : When working, I get the message back :
Warning: file_put_contents(../Ws/Wd/): failed to open stream: Is a directory in /tmp/mnt/USBTomato/Ws/wf.php on line 10
FAIL. Could not connect to file.
So , if someone knows, why my php-script receives an empty stsing, and not as foreseen the String in http.POST("data=abc&targetfile=ds.txt"), I would be happy to solve this also
Happy Christmas
Daniro