POST request fails to send sensor data to PHP script

Greetings,

I hope I am in the right forum with my question.

I have been trying to send temp and humidity data to a PHP script which, in turn, sends a text with this info. The text is always sent but it never contains the sensor data so either (1) the sensor data is not actually sent to the PHP script or (2) my PHP script is incorrect.

Here is what I receive on my cell phone:

cell-phone-results2

And here is the output from the serial monitor:

I have been able to send/receive singular sensor data (with another POST method) to the PHP script, but I would like to send/receive multiple data at the same time. I have struggled for several days with this and have failed.

Out of desperation I have tried both POST and GET methods and neither have worked. My understanding is that POST is to be used if sending data, not retrieving data.

Here is my sketch code:

    #include "config.h"
    #include <WiFi.h>
    #include "DHT.h"
    
    WiFiClient client;
    
    char servername[]="www.mywebsite.com"; 
    
    String result;
    float temperature;
    float humidity;
    
    #define DHTPIN 27
    #define DHTTYPE DHT22 
    DHT dht(DHTPIN, DHTTYPE);

    void setup() {
      dht.begin();
      delay(2000);
      Serial.begin(115200);
      Serial.println("Connecting");
      WiFi.begin(ssid, password);
      
      while (WiFi.status() != WL_CONNECTED) {
        delay(100);
      }
      Serial.println("Connected");
      delay(1000);
      
      readSensor();
      String temperatureString = String(temperature,1);
      String humidityString = String(humidity,1);  
      sendDataToServer(temperatureString,humidityString);
    }

    void loop() {    // do nothing here ...
    }

    void sendDataToServer(String temperature, String humidity)
    {  
      if (client.connect(servername, 80)) { 
        
        Serial.println("connected");

        //client.println("GET /includes/text_test.php?temperature="+temperature+"&humidity="+humidity+" HTTP/1.1");
        
        client.println("POST /includes/text_test.php?temperature="+temperature+"&humidity="+humidity+" HTTP/1.1");
        
        client.println("Host: www.mywebsite.com");
        client.println("Connection: close");  //close 1.1 persistent connection  
        client.println(); //end of get request
      } 
      else {
        Serial.println("connection failed"); //error message if no client connect
        Serial.println();
      }
     while(client.connected() && !client.available()) delay(1); //waits for data
      while (client.connected() || client.available()) { //connected or data available
        char c = client.read(); //gets byte from ethernet buffer
          result = result+c;
        }
      client.stop(); //stop client
      Serial.println(result);
    }

    void readSensor()
    {
      humidity = dht.readHumidity();
      temperature = dht.readTemperature(true);
      Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.print(" ºF\n");
      
      Serial.print("Humidity: ");
      Serial.print(humidity);
      Serial.print(" %\n");
    }

And here is my PHP script:

<?php

	//$temperature  =  $_GET["Temperature"];	// GET code ...
	//$humidity     =  $_GET["Humidity"];		// GET code ...

	$temperature  =  $_POST["Temperature"];     // POST method ...
	$humidity     =  $_POST["Humidity"];              // POST method ...

	//$text = "Temperature: {$temperature} C Humidity: {$humidity} %";	// centigrade code ...

	$text = "Temperature: .$temperature. F Humidity: .$humidit. %";	// get temp in fahrenheit ...

	//$admin_email = "myemail@something.com";	// email code ...

	$admin_email = "575XXXXXX@vtext.com";		// cell code 

	$email 	= "myemail@mac.com";
	$subject = "Temperature and Humidity Report";

	mail($admin_email,"$subject",$text,"From:" .$email);	//send email

?>

If anyone can point out my errors I would be most grateful.

Thank you in advance.

Don’t know if this was a copy/paste bit but this line is all messed up. start with the humidity variable name, and add in the concatenation now looking right, and I’d guess this is your culprit.

Thanks, Dave.

I will take a look at that and get back to you.

You cannot just change the request header type from GET to POST. The syntax for the GET request is correct, and it includes the values in the query string part of the request.

To send a POST request, you need to send the POST header, followed by the POST body. The following is from one of the many examples found on the web -

// send HTTP header
client.println("POST " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println(); // end HTTP header

// send HTTP body
client.println(queryString);

In this example, you can directly put your path name (/includes/text_test.php), host name (www.mywebsite.com), and the query string (?temperature=“+temperature+”&humidity="+humidity) into the correct locations in the string values.

The concatenation is ‘off’ in the php code, assuming that you are not putting those dots there as markers around the actual values. You can directly put php variables into an over all double-quoted string, without the using concatenation.

If I understood you correctly, this is my code and the results.

Here is the revised PHP script line:

$text = "Humidity: .$humidity. % Temperature: .$temperature. F ";

And this is what I received on my cell phone:

latest-text

The latest is the lower text. The variables and their sensor vars have been switched as I think you wanted me to correct. Still no sensor data.

Thanks.

No.

This.

$text = "Temperature: .$temperature. F Humidity: .$humidit. %"; // get temp in fahrenheit ...

Should look something like this (sorry if the syntax isn’t quite right, but it’s been a minute since I’ve done php)

$text = "Temperature: ".$temperature. "F Humidity:" .$humidity. "%"; // get temp in fahrenheit ...

or

$text = "Temperature: $temperature F Humidity: $humidity %"; // get temp in fahrenheit ...

Then you’ll need to make the changes @mabismad mentioned as well as the root problem is the values aren’t getting to your PHP script.

Thank you so much for your in-depth response to my query. My ignorance between POST and GET is obvious.

I will work on getting my code straightened out.

Thanks again for your help.

Thanks again for your help.

There seems to be many ways to achieve the same goal and I find that confusing. However, with your and @mabismad’s help this is all becoming a lot clearer to me.

I will report back later on.

Thanks again for your help.