How display POST request from ESP32 on webserver

francinnov
Posts: 6
Joined: Tue Nov 06, 2018 9:07 am

How display POST request from ESP32 on webserver

Postby francinnov » Tue Nov 06, 2018 9:41 am

Hi,

I have a NGINX server on a raspberry pi. I want to send a POST request from my ESP32 board to the NGINX web-server.

I use this code I found in a tutorial:

Code: Select all

#include <WiFi.h>
#include <HTTPClient.h>
 
const char* ssid = "freebox_FSVWVO";
const char* password = "legendaireinvincible1234";
 
void setup() {
 
  Serial.begin(115200);
  delay(4000);   //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://192.168.20.55:150/postfile/receive.php");  //Specify destination for HTTP request
   //http.begin("http://jsonplaceholder.typicode.com/posts");  //Specify destination for HTTP request
   http.addHeader("Content-Type", "text/plain");             //Specify content-type header
 
   int httpResponseCode = http.POST("test=POSTING from ESP32");   //Send the actual POST request
 
   if(httpResponseCode>0){
 
    String response = http.getString();                       //Get the response to the request

    Serial.println("Return code");
    Serial.println(httpResponseCode);   //Print return code
    Serial.println("Request answer");
    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(10000);  //Send a request every 10 seconds
 
}
On my webserver, I have created a php file (receive.php) to diplay the POST request thrown from my ESP32 :

Code: Select all

<html>
<body>

Text received :  <?php echo $_POST['test']; ?><br>

</body>
</html>
What I get on my serial monitor is :

Code: Select all

Return code
200
Request answer
<html>
<body>

Text received :  <br>

</body>
</html>
I don't know how to display the POST request from ESP32 on my webserver. Could you help me ?

Thank you,


francinnov
Posts: 6
Joined: Tue Nov 06, 2018 9:07 am

Re: How display POST request from ESP32 on webserver

Postby francinnov » Tue Nov 06, 2018 4:56 pm

Thanks,

francinnov
Posts: 6
Joined: Tue Nov 06, 2018 9:07 am

Re: How display POST request from ESP32 on webserver

Postby francinnov » Tue Nov 06, 2018 5:24 pm

Eventually, I have checked my access logs and they seem OK, this is the access.log content :
192.168.0.254 - - [06/Nov/2018:17:09:54 +0000] "POST /postfile/receive.php HTTP/1.1" 200 336 "-" "ESP32HTTPClient"
We can see the value 336 which corresponds to the number of bytes received by the server (if I understand properly).

If I do a test with a simple HTML form and I save the POST value inside of a file I can get the content of the POST request.
This is content of the PHP file on my server :

Code: Select all

<html>
<body>
<?php
if(isset($_POST["name"]))
{
        echo "Le nom est {$_POST['name']}.";
}

?>
<?php

  $fh = fopen('/var/www/html/postfile/log', 'w');

  fwrite($fh, print_r($_REQUEST, true));

  fclose($fh);

?>

<form action="receive.php" method="post">
  First name:<br>
  <input type="text" name="name" value="Mickey"><br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse"><br><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>
And this is the content of the log file when I submit the form on my browser :

Code: Select all

Array
(
    [name] => Mickey
    [lastname] => Mouse
)
But, when I power again my ESP32, that is the content of the log file I get :

Code: Select all

Array
(
)
The server seems to receive the POST request from the ESP32 but what it receives is empty...
I have tried the solution offered here but it doesn't work.

I cannot understand the issue. Would you have an idea ?

Who is online

Users browsing this forum: No registered users and 70 guests