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