0

I'm working on a project where I'm using an ESP8266 board to send sensor data to a REST API endpoint using HTTP POST requests. However, I'm encountering an error when trying to send the POST request, and I'm seeking assistance in resolving it.

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

Verified Wi-Fi credentials and network connection. Checked server availability and verified that it's running. Ensured that the ESP8266HTTPClient library is properly installed.

2
  • use the real IP address of your server and make sure the port is open on firewall of you computer Commented May 15, 2024 at 19:45
  • thank you, I found that my flask server was running locally Commented May 24, 2024 at 8:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.