Hello,
I'm working on a project to connect an node mcu esp8266 to wifi and then execute a post request of a website hosted on localhost. I managed to connect the esp8266 to wifi but the post request always end up failing.
this is the sketch I'm using :
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
WiFiClient Wificlient;
void setup() {
Serial.begin(9600); //Serial connection
WiFi.begin("AndroidAP1234", "00224455"); //WiFi connection
while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
delay(500);
Serial.println("Waiting for connection");
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http; //Declare object of class HTTPClient
String url = "http://127.0.0.1/Nodemcu_db_record_view/test1.php";
http.begin(Wificlient,url); //Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCode = http.POST("valeur=0"); //Send the request
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
} else {
Serial.println("Error in WiFi connection");
}
delay(30000); //Send a request every 30 seconds
}
and this is the log I'm getting :
[HTTP-Client][begin] url: http://127.0.0.1/Nodemcu_db_record_view/test1.php
[HTTP-Client][begin] host: 127.0.0.1 port: 80 url: /Nodemcu_db_record_view/test1.php
[HTTP-Client][sendRequest] type: 'POST' redirCount: 0
[HTTP-Client] failed connect to 127.0.0.1:80
[HTTP-Client][returnError] error(-1): connection failed
[HTTP-Client][end] tcp is closed
-1
[HTTP-Client][end] tcp is closed
Can you please help me?
Thanks