You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ESP8266HTTPClient库发送HTTP POST请求报错求助

ESP8266通过HTTP POST发送DHT11数据到REST API时请求失败求助

我正在进行一个项目,使用ESP8266开发板通过HTTP POST请求将DHT11传感器数据发送至REST API端点,但发送请求时遇到错误,现寻求解决帮助。

#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include "DHT.h"

// SSID and Password
const char *ssid = "LTE4G-B310-AC2B1";
const char *password = "Nour2003h@ne";

// Base REST API endpoint - Change the IP Address
const char *base_rest_url = "http://127.0.0.1:5000";

WiFiClient client;
HTTPClient http;

// Read interval
unsigned long previousMillis = 0;
const long readInterval = 5000;

#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT11 sensor type

DHT dht(DHTPIN, DHTTYPE);

struct DHT11Readings
{
  float temperature;
  float humidity;
};

const int JSON_DOC_SIZE = 384;

void setup()
{
  Serial.begin(9600);
  for (uint8_t t = 2; t > 0; t--)
  {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  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());

  dht.begin();
}

void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= readInterval)
  {
    previousMillis = currentMillis;

    Serial.println("---------------");
    Serial.println("Reading DHT11 sensor");

    DHT11Readings readings = readDHT11();
    if (isnan(readings.humidity) || isnan(readings.temperature))
    {
      Serial.println(F("Failed to read from DHT sensor!"));
      return;
    }

    Serial.print("Temperature :: ");
    Serial.println(readings.temperature);
    Serial.print("Humidity :: ");
    Serial.println(readings.humidity);

    sendDHT11Readings(readings);
  }
}

DHT11Readings readDHT11()
{
  float humidity = dht.readHumidity();
  float temperatureInC = dht.readTemperature();

  return {temperatureInC, humidity};
}

void sendDHT11Readings(DHT11Readings readings)
{
  char rest_api_url[200];
  sprintf(rest_api_url, "%sapi/sensors/dht11_1", base_rest_url);
  Serial.println(rest_api_url);

  String jsondata = "";
  StaticJsonDocument<JSON_DOC_SIZE> doc;
  JsonObject readingsObj = doc.createNestedObject("readings");
  readingsObj["temperature"] = readings.temperature;
  readingsObj["humidity"] = readings.humidity;

  serializeJson(doc, jsondata);
  Serial.println("JSON Data...");
  Serial.println(jsondata);

  http.begin(client, rest_api_url);
  http.addHeader("Content-Type", "application/json");

  int httpResponseCode = http.PUT(jsondata);
  if (httpResponseCode > 0)
  {
    String response = http.getString();
    Serial.println(httpResponseCode);
    Serial.println(response);
  }
  else
  {
    Serial.print("Error on sending POST: ");
    Serial.println(httpResponseCode);
    http.end();
  }
}

已排查内容:

  • 验证Wi-Fi凭据及网络连接正常
  • 确认服务器可用且正在运行
  • 确保ESP8266HTTPClient库已正确安装

内容的提问来源于stack exchange,提问作者Hiba DJERADI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 00:05:55