使用ESP8266发送HTTP发布请求

时间:2019-09-23 02:55:54

标签: c++ http arduino esp8266

我正在尝试使用esp8266发出POST请求。我已经建立了一个Webhook,并尝试使用esp8266调用它。我尝试了各种方法,但似乎不起作用。

我首先尝试使用ESP8266HTTPClient库,但它不起作用。 然后,我尝试通过“ client.println”直接发出发布请求,但它也没有起作用。当我发现无法将http请求发送到我设置的Webhook时,我开始将其发送到我通过express制作的本地网站,这样可以更轻松地对其进行测试。当发送简单的curl命令时,我得到正确的响应,但是当我尝试通过esp8266发送http调用时,我做不到。

这是我尝试使用ESP8266HTTPClient库:

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

const int sensorPin = 4;

WiFiServer server(80);

const  String altLink = "http://localhost:3000/message";

const String ssid = "shefer 2.4GHz";
const String password = "1234554321";

void setup() {
  Serial.begin(9600);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid,password);

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

  Serial.println();
  Serial.print("Connected, IP Address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop() {
  HTTPClient http;
  http.begin(altLink);
  http.addHeader("Content-Type", "text/plain");
  int sensorValue = int(analogRead(sensorPin)/128);
  if(sensorValue >= 0 && sensorValue <=3){
    Serial.println("Light is on, sending request");
    int httpCode = http.POST("Hello from esp8266");
    //int httpCode = http.POST("");
    Serial.println("HTTP Code: " + httpCode);
    if (httpCode != 200){
      Serial.println("Couldn't send the request, got code: " + httpCode);
    } else {
      Serial.println("Request was sent successfully");
    }
    http.end();
    delay(5000);
  }
 }

这是我尝试通过'client.println'直接发出发帖请求

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

WiFiServer server(80);

const String ssid = "shefer 2.4GHz";
const String password = "1234554321";

void setup() {
  Serial.begin(9600);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid,password);

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

  Serial.println();
  Serial.print("Connected, IP Address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop() {
  WiFiClient client;
  String data;
  Serial.print("Requesting POST: ");
   // Send request to the server:
   client.println("POST /message HTTP/1.1");
   client.println("Host: localhost:3000");
   client.println("Accept: */*");
   client.println("Content-Type: text/plain");
   client.print("Content-Length: ");
   client.println(data.length());
   client.println();
   client.print(data);
   delay(50000);
 }

我希望esp8266发送POST请求,但不会发送。我尝试了很多事情,我非常绝望。所有帮助将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:0)

在任何一种情况下都无法正常工作,因为您使用localhost作为目的地。

localhost的意思是“自己”-指正在发送请求的计算机。 IP地址127.0.0.1也是如此。在这种情况下,它们将引用您正在运行代码的ESP8266-而不是您尝试与之通信的Web服务器。

您需要使用要将请求发送到的计算机的实际名称或IP地址,而不是localhost

答案 1 :(得分:0)

在打印数据后添加终止println()

void loop() {
   WiFiClient client;
   String data;
   Serial.print("Requesting POST: ");
   // Send request to the server:
   client.println("POST /message HTTP/1.1");
   client.println("Host: localhost:3000");
   client.println("Accept: */*");
   client.println("Content-Type: text/plain");
   client.print("Content-Length: ");
   client.println(data.length());
   client.println();
   client.print(data);
   client.println();   //HTTP request termination empty line
   delay(50000);
}

答案 2 :(得分:0)

您应该将 localhost 替换为您 PC 网络上的 IPv4 地址。例如我的是 192.168.1.4

 "http://192.168.1.4:3000/cameraTest"