Arduino NodeMCU POST无法正常工作

时间:2017-05-01 01:05:54

标签: php post arduino esp8266 nodemcu

我有一个带有NodeMCU 1.0的{​​{1}}并使用Arduino IDE在其上编写代码。

我想将ESP-12E消息发送到我的webserer上的远程POST页面,其中包含来自我的模块的值,但首先我需要发送一些内容......

我尝试了来自不同来源和代码片段的许多不同示例,但似乎都没有效果。它被发送的方法 它工作的php方法,我可以从php页面检索数据,但我无法发送给他们。

我的代码:

GET

我的php:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char* ssid     = "SomeWireless";
const char* password = "12345";

String server = "www.example.net"; // www.example.com

void setup() {
 Serial.begin(115200);
  delay(10);
 // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

}

void loop() {
  jsonPOST();
  myPOST();
  myGET();
}


void myPOST() {
  HTTPClient http;

  http.begin("http://example.net/asd/recv.php");
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  http.POST("title=foo");
  http.writeToStream(&Serial);
  http.end();

  delay(1000);
}

void jsonPOST() {
  WiFiClient client;

  if (client.connect("example.net", 80)) {
      Serial.println("Connected to server");
  // Make the HTTP request
      int value = 2.5;  // an arbitrary value for testing
      String content = "{\"JSON_key\": " + String(value) + "}";
      client.println("POST /asd/recv.php HTTP/1.1");
      client.println("Host: example.net");
      client.println("Accept: */*");
      client.println("Content-Length: " + String(content.length()));
      client.println("Content-Type: application/json");
      client.println();
      client.println(content);
  }
  delay(1000);
}

void myGET() {
  if (WiFi.status() == WL_CONNECTED) { //Check     WiFi connection status
    HTTPClient http;  //Declare an object of class HTTPClient
    http.begin("http://www.exmaple.net/asd/btnCheck.php");  //Specify request destination
    int httpCode = http.GET();  //Send the request

    if (httpCode > 0) { //Check the returning code
      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload
    }
    http.end();   //Close connection
  }
  delay(1000); 
}

编辑:我想提一下,网络服务器可以接受POST消息,而且我有一个经销商类型的帐户,所以我没有完全控制服务器。

对此事有任何建议,值得。我已经用它打了2天 谢谢!

1 个答案:

答案 0 :(得分:0)

答案:

我设法正确地完成了请求。问题来自我的帖子内容 使用 public function get_book($id) { $this->load->database(); $this->db->select('book_id, book_title, book_publisher, book_summary'); $this->db->from('books'); $this->db->where('auther_id',$id); $data=$this->db->get(); return $data->result(); } 库的代码是:

ESP8266HTTPClient.h

对于php页面:

void jPOST() {
  HTTPClient http;

  http.begin("http://example.net/asd/urlen.php");
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  http.POST("{\"y\":6,\"x\":11}");
  http.writeToStream(&Serial);
  http.end();

  delay(2000);
}