无法使用ESP8266和AT命令发布到本地网络服务器

时间:2019-05-30 07:01:57

标签: php post raspberry-pi3 esp8266 at-command

我正在尝试将数据发送到在树莓派3上的Apache2上运行的本地Web服务器。IP地址为192.168.0.16。我将使用TIVA-C Launchpad使用ESP8266-01 wifi模块将POST请求发送到服务器,但是,在实现一切之前,我想尝试看看是否通过UART手动将AT命令输入到ESP8266-01 (使用FTDI和arduino串行监视器)可以工作。在命令末尾,ESP返回:

> 
busy s...

Recv 180 bytes

SEND OK
CLOSED

但是phpMyAdmin中的数据库永远不会更新。可能是怎么回事?

这是到目前为止我尝试过的AT命令序列,但无济于事。

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\r\n
Host: 192.168.0.16\r\n
Accept:*/*\r\nContent-Length: 40\r\n
Content-Type: application/x-www-form-urlencoded\r\n
\r\n
idSensor=16555&ch1=1&ch2=0&ch3=0&ch4=1

>
busy s...

Recv 162 bytes
SEND OK
CLOSED

这是我的网络服务器中的代码

<?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>

有什么方法可以调试或知道为什么我的POST请求没有在数据库中产生任何变化?谢谢

0 个答案:

没有答案