httpRequestData: Error code -1

Meu código é esse e está dando resposta:
12:53:35.189 -> httpRequestData: api_key=tPmAT5Ab3j7F9&value1=25.64&value2=1015.60&value3=-19.55
12:53:35.189 -> Error code: -1

Como se o servidor nao estivesse aceitando.
Alguém pode me ajudar, quero inserir o mysql através POST, usando servidor localhost no linux e esp8266



//***********************************************************************
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>


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

const char* serverName = "http://localhost/esp/post-data.php";
 change the apiKeyValue value, the PHP file /post-data.php also needs to have the same key

String apiKeyValue = "xxx";


#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BMP280 bmp;  // I2C

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

  // (you can also pass in a Wire library object like &Wire2)
  bool status = bmp.begin(0x76);
  if (!status) {
    Serial.println("Could not find a valid BMP280 sensor, check wiring or change I2C address!");
    while (1);
  }
}

void loop() {
  
  //Check WiFi connection status
  if (WiFi.status() == WL_CONNECTED) {

    WiFiClient client;

    HTTPClient http;

    // Seu nome de domínio com caminho de URL ou endereço IP com caminho 
    http.begin(client, serverName);
    //http.begin(serverName);

    // Specify content-type header 
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    // Prepare your HTTP POST request data 
    String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(bmp.readTemperature()) + "&value2=" + String(bmp.readPressure()/100.0F) + "&value3=" + String(bmp.readAltitude()) + "";

    Serial.print("httpRequestData: ");
    Serial.println(httpRequestData);   
    
    // \/\/\/\/\/\/\/\/\/\/\/\/ Send HTTP POST request 
    int httpResponseCode = http.POST(httpRequestData);

    if (httpResponseCode > 0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
    }
     else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }

    // Free resources
    http.end();
    } 
    else {
      Serial.println("WiFi Disconnected");
    }

    //Send an HTTP POST request every 30 seconds 
    delay(30000); 
    }

o ESP8266 não sabe o que é o localhost já que isso apenas existe dentro do teu servidor linux.

Troca localhost pelo endereço IP do servidor a que te referes e já deve funcionar.

Bom dia, já fiz isso sim, de várias maneiras, o ESP e o servidor estão na mesma rede wifi. Seria o caso trocar de rede um deles de rede. vi isso na net:
A função retorna 1,-1,-2,-3 ou-4, onde 1 indica SUCESSO, -1 TIMED_OUT, -2 INVALID_SERVER, -3 TRUNCATED e -4 INVALID_RESPONSE.

-1 TIMED_OUT

Não é de várias maneiras… só há mesmo uma maneira que é colocares lá o endereço IP do teu servidor linux.

O estar na mesma rede WiFi é importante, e assumido, mas não vai resolver o facto do ESP8266 não saber o que é o localhost.

Fiz uma modificação e como você disse do ip mesmo, também alterei os arquivos de insert, fiz com PDO

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
  /var/www/html/E8266
  banco de dados: E8266

*/

#ifdef ESP32
  #include <WiFi.h>
  #include <HTTPClient.h>
#else
  #include <ESP8266WiFi.h>
  #include <ESP8266HTTPClient.h>
  #include <WiFiClient.h>
#endif

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>

// Replace with your network credentials
const char* ssid     = "xxxx";
const char* password = "xxxxxxxxx";

const char* serverName = "http://192.168.0.184/E8266/post-esp-data.php";

String apiKeyValue = "xxxxxxxxxxxxxxxx";

String sensorName = "BMP180";
String sensorLocation = "Office";

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BMP085 bmp;  // I2C
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());

  // (you can also pass in a Wire library object like &Wire2)
  bool status = bmp.begin(0x76);
  if (!status) {
    Serial.println("Could not find a valid BMP180 sensor, check wiring or change I2C address!");
    while (1);
  }
}

void loop() {
  //Check WiFi connection status
  if(WiFi.status()== WL_CONNECTED){
    WiFiClient client;
    HTTPClient http;
    
    // Your Domain name with URL path or IP address with path
    http.begin(client, serverName);
    
    // Specify content-type header
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    
    // Prepare your HTTP POST request data
    String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
                          + "&location=" + sensorLocation + "&value1=" + String(bmp.readTemperature())
                          + "&value2=" + String(bmp.readAltitude()) + "&value3=" + String(bmp.readPressure()/100.0F) + "";
    Serial.print("httpRequestData: ");
    Serial.println(httpRequestData);

    // Send HTTP POST request
    int httpResponseCode = http.POST(httpRequestData);
        
    if (httpResponseCode>0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
    }
    else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    // Free resources
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }
  //Send an HTTP POST request every 30 seconds
  delay(30000);  
}