无法使用ESP8266和AT命令POST到本地Web服务器

I'm trying to send data to a local webserver running on Apache2 on a raspberry pi 3. The IP address is 192.168.0.16. I'm going to use a TIVA-C Launchpad to send POST requests to the server using a ESP8266-01 wifi module, however, before implementing everything I wanted to try and see if inputing the AT commands manually to the ESP8266-01 through UART (Using an FTDI and the arduino serial monitor) would work. At the end of the commands, the ESP returns:

> 
busy s...

Recv 180 bytes

SEND OK
CLOSED

But the database in phpMyAdmin never updates. What could be the matter?

This is the sequence of AT commands that I have tried so far but to no avail.

AT+CIPSTART="TCP","192.168.0.16",80
CONNECT

OK

AT+CIPSEND=162

OK

(I send the following POST request all in a single line)
POST /index.php HTTP/1.0

Host: 192.168.0.16

Accept:*/*
Content-Length: 40

Content-Type: application/x-www-form-urlencoded



idSensor=16555&ch1=1&ch2=0&ch3=0&ch4=1

>
busy s...

Recv 162 bytes
SEND OK
CLOSED

This is the code in my web server

<?php
header("Content-type: text/html; charset=\"utf-8\"");
$idSensor = 0;
$ch1 = 0;
$ch2 = 0;
$ch3 = 0;
$ch4 = 0;
//print_r($_POST);
if($_POST){
    $idSensor = $_POST['idSensor'];
    $ch1 = $_POST['ch1'];
    $ch2 = $_POST['ch2'];
    $ch3 = $_POST['ch3'];
    $ch4 = $_POST['ch4'];
    $conexion = mysqli_connect("localhost:3306","admin","admin4321", "Parqueo-matic");
    if(!$conexion){
        echo "Error: No se pudo conectar a MySQL.".PHP_EOL;
        echo "errno de depuración: ". mysqli_connect_errno().PHP_EOL;
        echo "error de depuración: ". mysqli_connect_error().PHP_EOL;

    }else{
        $query = "INSERT INTO `Datos` (`ID`, `Hora`, `ID_Sensor`, `P1`, `P2`, `P3`, `P4`) VALUES (NULL, CURRENT_TIMESTAMP, '$idSensor', '$ch1', '$ch2', '$ch3', '$ch4');";
        mysqli_query($conexion, $query);
        echo "Receiving data";

    }
    mysqli_close($conexion);
}

$conexion = mysqli_connect("localhost:3306","admin","admin4321", "Parqueo-matic");
if(!$conexion){
    echo "Error: No se pudo conectar a MySQL.".PHP_EOL;
    echo "errno de depuración: ". mysqli_connect_errno().PHP_EOL;
    echo "error de depuración: ". mysqli_connect_error().PHP_EOL;
}else{
    $query = "SELECT * FROM `Datos` ORDER BY ID DESC LIMIT 10";
    $result = mysqli_query($conexion,$query);
}
mysqli_close($conexion);

header("refresh: 1");

?>

<!DOCTYPE html>
<html lang='en'>
    <head>

        <!-- Required meta tags -->
        <meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
        <meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'>
        <title>PMatic</title>

        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

        <!-- jQuery first, then Tether, then Bootstrap JS. -->
        <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>


    </head>

    <body class="container">
        <br>
        <h1><strong>Parqueo-Matic</strong></h1>
        <br>
        <table class="table">
            <thead class="bg-primary text-white">
                <tr>
                    <th>ID Sensor</th>
                    <th>Parqueo 1</th>
                    <th>Parqueo 2</th>
                    <th>Parqueo 3</th>
                    <th>Parqueo 4</th>
                </tr>
            </thead>
            <tbody> 
                <?php
                    while($row = mysqli_fetch_array($result)){
                      echo "<tr>
                                <td>".$row['ID_Sensor']."</td>
                                <td>".$row['P1']."</td>
                                <td>".$row['P2']."</td>
                                <td>".$row['P3']."</td>
                                <td>".$row['P4']."</td>
                           </tr>";       

                    }
                ?>
            </tbody>
        </table>
        <br>

    </body>

</html>

Is there any way to debug or know why my POST request is not producing any change in the database? Thank you

ds355020
ds355020 谢谢!我真的很陌生。我怎样才能记录$_POST的内容?此外,我尝试重用连接,但一段时间后ESP似乎自动关闭连接
8 个月之前 回复
dourukeng5302
dourukeng5302 在进行INSERT时,使用mysqli_error()查看查询是否失败。如果是,则应记录错误消息。您还应该记录$_POST的内容以查看您获得的内容。您还可以使用Postman之类的东西来测试index.php文件。
8 个月之前 回复
dou12352
dou12352 每次进行新查询时,不要打开和关闭与数据库的新连接,只需打开一次,然后在请求中重复使用相同的连接。连接成本很高,因此如果您不需要,请避免额外增加。
8 个月之前 回复
duanliao6789
duanliao6789 警告:您对SQL注入非常开放,应该使用参数化的准备语句,而不是像这样手动构建查询。特别是因为你根本没有逃避用户输入!
8 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问