ESP8266-01 POST请求MySQL

时间:2016-11-12 18:22:26

标签: php mysql esp8266 arduino-esp8266

我正在尝试向我已设置的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连接到我的手机然后尝试执行请求时,我得到了一个&#34; -1&#34;返回哪个连接被拒绝了。我已经尝试了几次并且无法解决这个问题。可能是手机不允许我手动发送而不是使用浏览器吗?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

有时发布您的问题有助于您解决问题。有两个问题:

  1. 我的路由器一直在我用作网络服务器的计算机上给我一个不同的IP。每次我重置之后都是Probablly。
  2. 我在想什么!! post.php正在使用GET。这是新代码:
  3. <a href="#contact">Click here to jump to Some Heading</a>
    <div id="space" class="space"></div>
    <div id="contact" class="contact"></div>
    <a href="#space">Click here to jump to Some Heading</a>
    

    我现在可以发帖了。我收到一个错误,我将寻找其他地方:

      

    警告:输入中出现意外字符:&#39;&#39; (ASCII = 18)状态= 0 C:\ xampp \ htdocs \ iot \ post.php 5

相关问题