HTTP response code with -1

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

const char* ssid = "Try";
const char* password = "k@li12345678";

void setup() {
  pinMode(D8, OUTPUT);
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
    Serial.println(WiFi.status());
  }

  Serial.println("Connected to WiFi");
}

void loop() {
  // Make HTTP GET request to server
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client; // Create a WiFiClient object
    HTTPClient http;
    http.begin(client, "http://10.10.193.226:5000/led"); // Replace with your server IP address and port
    int httpCode = http.GET();

    StaticJsonDocument<200> doc; // Define the JSON document
    
    if (httpCode > 0) {
      String payload = http.getString();
      DeserializationError error = deserializeJson(doc, payload);
      if (error) {
        Serial.print("deserializeJson() failed: ");
        Serial.println(error.c_str());
        return;
      }

      signed char sensor = doc["led_state"];

     if(sensor == 1){
      digitalWrite(D8, HIGH);
     }
     if(sensor == 0){
      digitalWrite(D8, LOW);
     }

      Serial.println(sensor); // Print server response
    } else {
      Serial.println("Error on HTTP request");
      Serial.println(httpCode);
    }

    http.end();
  }

  delay(1000); // Wait 5 seconds before making the next request
}

My server is running fine. It is reachable from my Web Browser and Mobile browser.

Wifi also connected but still it responding with -1

I moved your topic to an appropriate forum category @bobby0bash.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

For that -1 returned value the library you are using says:

/// HTTP client errors
#define HTTPC_ERROR_CONNECTION_FAILED   (-1)

So it looks like your ESP (which model exactly?) can't somehow connect to that address.

Enable debug print to serial to be able to see why the http.begin() failed:
image

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.