I want to send some date via post request from an ESP8266 to server running php. Purpose is to capture data via ESP8266 & DHT22 and store it on mariaDB via php. I know that PDO might be more secure, but I want to try it this way first. Problem is that the data seems to get send but it is not written to the db.
- Connection to Wifi (OK)
- connect to remote server (seems OK)
- PHP file on server side (seems OK) - data entry via link does work
Sketch running on ESP8266 :
if (client.connect(server, 80)) {
Serial.println("connected!!");
WiFi.printDiag(Serial);
String data = "temp="
+ (String) sensorTemp
+ "&humi="
+(String) sensorHum;
client.println("POST /connect2.php HTTP/1.1");
client.print("Host: xxx.xxx.xx.xxxx
");
client.println("User-Agent: ESP8266/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.print("
");
client.println (data);
client.stop();
Serial.println("
");
Serial.println("My data string im POSTing looks like this: ");
Serial.println(data);
Serial.println("And it is this many bytes: ");
Serial.println(data.length());
delay(2000);
client.stop();
}
Server side php :
<?php
$servername = "xxx.xxx.xx.xxx";
$username = "username";
$password = "password";
$dbname = "dbname";
$temp = $_REQUEST['temp'];
$humi = $_REQUEST['humi'];
//date_default_timezone_set('America/Toronto');
//$date=date('m-d-Y H:i:s') ;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO dbname (temp, humi)
VALUES ('$humi', '$temp')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>