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:
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.