无法使用NodeMCU向服务器发送POST请求

时间:2017-11-13 10:09:56

标签: php mysql arduino arduino-esp8266

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

  

200   INSERT INTO传感器(温度,光照)       VALUES('','')
SQLSTATE [HY000]:常规错误:1366错误的整数值:''对于专栏' temp'在第1行

现在我明白这种情况正在发生,因为没有数据发送到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&light=88");行的某处。

此外,这只是一个测试程序。最终的程序将采用此行int httpCode = http.POST("?temp=20&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;
?>

1 个答案:

答案 0 :(得分:1)

你传递了

  

http.POST( “温度= 20安培;光= 88”)

哪个应该是

  

http.POST( “温度= 20安培;光= 88”)

和标题

  

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

基本上用于发送纯文本,您可以在此处参考内容类型标题的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();
    }
?>