我有一个用 Arduino 语言编程的 NodeMCU。我试图将一些数据从我的传感器发送到我服务器上的 PHP 脚本,然后将数据写入 MySQL 服务器。当通过 URL 运行时,脚本运行正常,但是当我尝试从我的代码发送值时,出现此错误
200 INSERT INTO sensor (temp, light) VALUES ('', '')
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'temp' at row 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;
?>
最佳答案
你路过
http.POST("?temp=20&light=88")
应该是
http.POST("temp=20&light=88")
和标题
http.addHeader("Content-Type", "text/plain")
基本上是用来发送明文的,可以引用docs此处的内容类型 header 。因为,您正在发送表单数据,所以应该使用它
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 - 无法使用 NodeMCU 向服务器发送 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47261778/