我正在尝试向POST
我设置的 MySQL 数据库发出请求。我尝试了各种教程都无济于事。我可以使用我创建的 PHPPOST
页面通过这样的浏览器输入数据:
<?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”返回,即连接被拒绝。我已经尝试了几次,但无法弄清楚。难道是手机不允许我手动发送而不是使用浏览器?有任何想法吗?