我正在尝试向已设置的 MySQL 数据库发出 POST
请求。我试过各种教程都无济于事。我可以使用我创建的 PHP POST
页面通过浏览器输入数据,如下所示:
<?php
//post.php
include 'connect.php';
var_dump($_GET);
$id = $_GET['id'];
$value = $_GET['value'];
$id = mysql_real_escape_string($id);
$value = mysql_real_escape_string($value);
mysql_query(
"INSERT INTO iot (value, id) VALUES ('$value', '$id');"
)
?>
我首先尝试访问我自己的 PHP 站点并发送一个带有温度和露点的 POST
请求,如下所示:
//tempandhumidity2
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "SimpleDHT.h"
SimpleDHT11 dht11;
const char* ssid = "nameofssid";
const char* password = "password";
int ledPin = 2; // GPIO2
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");
}
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
HTTPClient http;
http.begin("http://192.168.43.162/iot/post.php");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST("id=12&value=35");
Serial.println(httpCode);
if(httpCode == HTTP_CODE_OK)
{
Serial.print("HTTP response code ");
Serial.println(httpCode);
String response = http.getString();
Serial.println(response);
}
else
{
Serial.println("Error in HTTP request");
}
http.end();
}
返回:
Waiting for connection
Waiting for connection
Waiting for connection
Waiting for connection
Waiting for connection
Connecting to nameofssid
Use this URL to connect: http://192.168.43.176/
200
HTTP response code 200
Connected successfullyarray(0) {
}
<br />
<b>Notice</b>: Undefined index: id in <b>C:\xampp\htdocs\iot\post.php</b> on line <b>4</b><br />
<br />
<b>Notice</b>: Undefined index: value in <b>C:\xampp\htdocs\iot\post.php</b> on line <b>5</b><br />
使用带有 http://10.0.0.90/iot/post.php?id=14&value=33
的网络浏览器返回:
Connected successfullyarray(2) { ["id"]=> string(2) "14" ["value"]=> string(2) "33" }
我已经尝试执行 GET
请求,只是为了看看我是否可以在此处访问外部站点:
//tempandhumidity4
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char *SERVER_WIFI_SSID = "nameofssid";
const char *SERVER_WIFI_PASS = "password";
void setup()
{
Serial.begin(115200);
Serial.println("");
Serial.println("Connecting to WiFi ");
WiFi.begin(SERVER_WIFI_SSID,SERVER_WIFI_PASS);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("Connected");
}
void loop(){
HTTPClient http;
http.begin("http://www.nfl.com");
int httpCode = http.GET();
Serial.println(httpCode);
if(httpCode == HTTP_CODE_OK)
{
Serial.print("HTTP response code ");
Serial.println(httpCode);
String response = http.getString();
Serial.println(response);
}
else
{
Serial.println("Error in HTTP request");
}
http.end();
}
这将返回以下内容:
Connecting to WiFi
.........Connected
400
Error in HTTP request
400
任何想法或建议将不胜感激?问题似乎是当我将我的 ESP8266 连接到我的手机然后尝试执行请求时我得到一个“-1”返回,这是连接被拒绝的。我已经尝试了几次,但无法弄清楚。可能是手机不允许我手动发送而不是使用浏览器吗?有什么想法吗?
最佳答案
有时发布您的问题可以帮助您解决问题。有两个问题:
- 我的路由器一直在我用作网络服务器的计算机上给我不同的 IP。可能在每次我重置它之后。
- 我在想什么!! post.php 正在使用 GET。这是新代码:
//post.php
include 'connect.php';
var_dump($_POST);
$id = $_POST['id'];
$value = $_POST['value'];
$id = mysql_real_escape_string($id);
$value = mysql_real_escape_string($value);
mysql_query(
"INSERT INTO iot (value, id) VALUES ('$value', '$id');"
)
我现在可以发帖了。我收到一个错误,我会在其他地方查找:
Warning: Unexpected character in input: '' (ASCII=18) state=0 in C:\xampp\htdocs\iot\post.php on line 5
关于php - ESP8266-01 向 MySQL 发送 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40566170/