to my PC, I am using a ESP8266 Wireless unit with AT commands. I installed xampp and
have set up a database, originally there was 8 variables but because I could not get
it working, have just set up a Test with just 3 variables, ID + Col_1 and Col_2.
I can connect OK to the server and get it to download html pages. The problem is I
cannot get the GET or POST to work, I thought with just AT Commands it would be quite
simple to do it, following a HTTP POST example. I get a 200 OK and it does access the
PHP file and Database but the data is empty. The data is reaching the server but not
being sent to the php file, but I cannot find out why. If I remove "isset" it comes up
with undefined variables. I have tried different arg separators.
Commands being sent below:
- Code: Select all
HSerOut ["AT+CIPSTART=0,"TCP","192.168.1.103",80"\r\n"]
HSerOut ["AT+CIPSEND=0,54\r\n"]
HSerOut ["POST /insert.php HTTP/1.1\r\n"]
HSerOut ["Host: 192.168.1.103\r\n"]
HSerOut ["\r\n"]
HSerOut ["\r\n"]
Note: Value1 = 54 Value2 = 61
HSerOut ["Content-Length: 19\r\n"]
HSerOut ["\r\n"]
HSerOut ["\r\n"]
HSerOut ["Col_1=",Dec Value1,"&Col_2=",Dec Value2,"\r\n"]
HSerOut ["\r\n"]
HSerOut ["\r\n"]
Reply recived:-
AT+CIPSTART=0,"TCP","192.168.1.103",80
0,CONNECT
OK
Content-Length: 19
Col_1=54&Col_2=61
Recv 54 bytes
SEND OK
+IPD,0,225:HTTP/1.1 200 OK
Date: Wed, 25 May 2022 14:28:45 GMT
Server: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/8.1.5
X-Powered-By: PHP/8.1.5
Content-Length: 28
Content-Type: text/html; charset=UTF-8
record created successfully
0,CLOSED
PHP File "insert.php":-
<?php
$servername = "192.168.1.103:3306";
$username = "root";
$password = "xxxxxxxxxxxx";
$dbname = "test_esp";
//var_dump($_GLOBALS);
//var_dump($_POST);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/* * Get data out of $_POST array
* and do a query to insert it into test_scores table *
*/
$Col_1 = isset($_POST['Col_1']);
$Col_2 = isset($_POST['Col_2']);
$sql = "INSERT INTO esp_table (Col_1, Col_2)
VALUES ('" . $Col_1 . "', '" . $Col_2 . "')";
if ($conn->query($sql) === TRUE) {
echo " record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
$conn->close();
}
The ESP8266 Docs state to include before data "Content-Length: 19\r\n"
but when I put it in I got from Access Log:-
192.168.1.121 - - [24/May/2022:15:02:15 +0100] "POST /insert.php HTTP/1.1" 200 41 "-" "ESP8266"
192.168.1.121 - - [24/May/2022:15:02:15 +0100] "Content-Length: 19" 400 329 "-" "-"
Then commenting it out I got:-
192.168.1.121 - - [24/May/2022:15:04:54 +0100] "POST /insert.php HTTP/1.1" 200 41 "-" "ESP8266"
192.168.1.121 - - [24/May/2022:15:04:54 +0100] "Col_1=54&Col_2=61" 400 329 "-" "-"
This line seems to prove data is being received by server. "Col_1=54&Col_2=61"
But then putting a second carriage return/line feed after each I now just get 200 OK as above.
On the reply from server the Time is 1 hour earlier, it is correct in the Access Log.
I do not know whether the format I am sending is wrong, I have tried to monitor what Xampp is actually
receiving but cannot find a way to do it.
Any ideas or help/suggestions Thanks.