【问题标题】:Cant sent POST request to server with NodeMCU无法使用 NodeMCU 向服务器发送 POST 请求
【发布时间】:2017-11-13 10:09:55
【问题描述】:

我有一个用 Arduino 语言编程的 NodeMCU。我正在尝试将一些数据从我的传感器发送到我服务器上的 PHP 脚本,然后将数据写入 MySQL 服务器。当通过 URL 运行时,脚本运行良好,但是当我尝试从我的代码中发送值时,我得到了这个错误

200 插入传感器(温度、光线) VALUES ('', '')
SQLSTATE[HY000]:一般错误:1366 不正确的整数值:第 1 行的列 'temp' 的''

现在我知道这是因为没有数据发送到 PHP 脚本,并且空格不是整数。

这是 NodeMCU 上的代码

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>

void setup() {

  Serial.begin(115200);                                  //Serial connection
  WiFi.begin("ssid", "password");   //WiFi connection

  while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion

    delay(500);
    Serial.println("Waiting for connection");

  }

}

void loop() {

 if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status

   HTTPClient http;    //Declare object of class HTTPClient

   http.begin("http://server_public_ip_address/test3.php");      //Specify request destination
   http.addHeader("Content-Type", "text/plain");  //Specify content-type header

   int httpCode = http.POST("?temp=20&light=88");   //Send the request
   String payload = http.getString();                  //Get the response payload

   Serial.println(httpCode);   //Print HTTP return code
   Serial.println(payload);    //Print request response payload

   http.end();  //Close connection

 }else{

    Serial.println("Error in WiFi connection");   

 }

  delay(3000);  //Send a request every 30 seconds

}

我怀疑问题出在http.addHeader("Content-Type", "text/plain");int httpCode = http.POST("?temp=20&amp;light=88"); 行中。

另外,这只是一个测试程序。最终程序将在这一行 int httpCode = http.POST("?temp=20&amp;light=88"); 中使用变量而不是数字。那我该怎么写呢?

编辑:
也添加 PHP 脚本

<?php
$servername = "localhost";
$username = "root";
$password = "*************";
$dbname = "testbox1";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $temp = $_GET["temp"];
    $light = $_GET["light"];
    $sql = "INSERT INTO sensor (temp, light)
    VALUES ('$temp', '$light')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

【问题讨论】:

    标签: php mysql arduino arduino-esp8266


    【解决方案1】:

    你过去了

    http.POST("?temp=20&light=88")

    应该是这样的

    http.POST("temp=20&light=88")

    和标题

    http.addHeader("Content-Type", "text/plain")

    基本上是用来发送纯文本的,可以参考这里的content-type header的docs。因为,您正在发送表单数据,所以应该使用它

    http.addHeader("Content-Type", "application/x-www-form-urlencoded")

    以下代码应该可以解决您的问题。

    #include <ESP8266HTTPClient.h>
    #include <ESP8266WiFi.h>
    
    void setup() {
    
      Serial.begin(115200);                                  //Serial connection
      WiFi.begin("ssid", "password");   //WiFi connection
    
      while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion
    
        delay(500);
        Serial.println("Waiting for connection");
    
      }
    
    }
    
    void loop() {
    
     if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
    
       HTTPClient http;    //Declare object of class HTTPClient
    
       http.begin("http://server_public_ip_address/test3.php");      //Specify request destination
       http.addHeader("Content-Type", "application/x-www-form-urlencoded");  //Specify content-type header
    
       int httpCode = http.POST("temp=20&light=88");   //Send the request
       String payload = http.getString();                  //Get the response payload
    
       Serial.println(httpCode);   //Print HTTP return code
       Serial.println(payload);    //Print request response payload
    
       http.end();  //Close connection
    
     }else{
    
        Serial.println("Error in WiFi connection");   
    
     }
    
      delay(3000);  //Send a request every 30 seconds
    
    }
    

    将其用作您的 PHP 文件以查看发送的值:

    <?php 
        print_r($_POST);
    ?>
    

    问题在于您的 PHP 脚本。您必须使用 $_POST,而不是 $_GET。此外,您的 PDO 语法中有一个小错误。我试图修复它。检查这个:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "*************";
    $dbname = "testbox1";
    
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $temp = $_POST["temp"];
        $light = $_POST["light"];
        $sql = $conn->prepare("INSERT INTO sensor (temp, light) VALUES (?, ?)");
        $sql->bindParam(1, $temp);
        $sql->bindParam(2, $light);
        $sql->execute();
        echo "New record created successfully";
        }
    catch(PDOException $e)
        {
        echo $sql . "<br>" . $e->getMessage();
        }
    ?>
    

    【讨论】:

    • 该特定错误是数据库错误。根据您的问题,尝试在您的 php 脚本中输出 post 变量。如果您在上传 PHP 代码方面需要帮助。
    • 我的 PHP 脚本在从浏览器链接(即 server_ip/test3.php)使用时可以正常工作。
    • 请尝试使用我编辑代码时使用的脚本。您可以看到 post 方法将起作用。请在 http.begin(url) 方法中使用正确的 URL
    • 是的,值正在正确发送。那有什么问题呢?
    • 发生这种情况是因为我使用 GET 读取变量但发送 POST 代替吗?
    猜你喜欢
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 2012-12-12
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多