HTTP Request ESP32 to local IP

I got this HTTP Request Post from ESP to API point some local IP

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

// Replace with your network credentials
const char* ssid = "Wifi";
const char* password = "Password";

String sensorName = "Idk";

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  //Check WiFi connection status
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client;
    HTTPClient http;

    StaticJsonDocument<200> doc;
    String url, nodemcuData;

    //equate with your computer's IP address and your directory application
    url = "<myIP>";

    doc["sensor"] = sensorName;
    doc["value"] = String(0.01 + random(0, 100));

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

    serializeJson(doc, nodemcuData);
    Serial.print("POST data >> ");
    Serial.println(nodemcuData);

    int httpCode = http.POST(nodemcuData);  //Send the request
    Serial.println(httpCode);
    String payload;
    if (httpCode > 0) {            //Check the returning code
      payload = http.getString();  //Get the request response payload
      payload.trim();
      if (payload.length() > 0) {
        Serial.println(payload + "\n");
      }
    }

    http.end();  //Close connection
  } else {
    Serial.println("WiFi Disconnected");
  }
  //Send an HTTP POST request every 1 seconds
  delay(10000);
}

The problem was when the URL is domain like www.domain.com/ESP/API.php the response is 200 (working), but when using local IP from XAMPP like 192.168.1.x/ESP/API.php it doesnt work with response code -1.

API is working with postman but in ESP doesnt work

url should be a full url including http://

already done and same problem

Usually with application like XAMPP the web server is setted to accept connection only from localhost.

No, i already try from different computer (same wifi) using postman and its work.

but from esp is error code -1, the problem is from ESP i think because they cant address IP but with domain its work

did you ever find a solution? I am having the same exact issue with my Nano Exp32. I can access the website successfully from both my dev station and my cell phone on the same wifi. I change the IP to http://www.google.com and I get a response back. Also note that I copied the code from an example. Changed the SSID/PW and IP address and port to what mine is but I still get -1 from the nano.

I little forgot about it, but if im not wrong http request is always request to port 80 so if you try to change the port maybe it doesn't work.

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