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);
}
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.