Comments on: ESP32/ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Wed, 02 Jul 2025 10:49:54 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: Joselito https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/#comment-1060621 Tue, 24 Jun 2025 16:41:29 +0000 https://randomnerdtutorials.com/?p=85922#comment-1060621 Bom dia, falo do Brasil. alguém pode me ajudar, já tentei de tudo, estou usando esp8266, conecta com wifi, ver os dados do sensor, mas não inseri os dados no mysql
local da pasta no servidor
localhost/esp8266

root@Aspire-Axxxxxxxx3:/var/www/html/esp8266# ls -l
-rw-r–r– 1 Axxxxxxxx3 Axxxxxxxx3 2340 jun 23 21:54 esp-data.php
-rw-rw-r– 1 Axxxxxxxx3 Axxxxxxxx3 21 jun 23 15:30 info.php
-rw-r–r– 1 Axxxxxxxx3 Axxxxxxxx3 0 jun 23 11:42 leiame_txt
-rw-r–r– 1 Axxxxxxxx3 Axxxxxxxx3 2166 jun 23 22:23 post-esp-data.php

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

// Substitua pelas suas credenciais de rede
const char* ssid = “yyyyyyyyyyyyyyyyyyyyyyyyyyyy”;
const char* password = “xxxxxxxxxxxxxxxxxxxxxxxxxxxx”;

const char* serverName = “http://192.168.0.184/esp8266/post-esp-data.php”;

String apiKeyValue = “tPmAT5Ab3j7F9”;

String sensorName = “BMP280”;
String sensorLocation = “Office”;

#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(“Connecting to WiFi…”);
}
Serial.println(“”);
Serial.print(“Conectado à rede WiFi com endereço IP: “);
Serial.println(WiFi.localIP());

// (você também pode passar um objeto da biblioteca Wire como &Wire2)
bool status = bmp.begin(0x76);
if (!status) {
Serial.println(“Não foi possível encontrar um sensor BMP280 válido, verifique a fiação ou altere o endereço I2C!”);
while (1);
}
}

void loop() {
//Verifique o status da conexão WiFi
if(WiFi.status()== WL_CONNECTED){

std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setInsecure();
HTTPClient http;
// Seu nome de domínio com caminho de URL ou endereço IP com caminho
http.begin(*client, serverName);

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

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("Dados de solicitação http: ");
Serial.println(httpRequestData);

Serial.print("Conectado à rede WiFi com endereço IP: ");
Serial.println(WiFi.localIP());

int httpResponseCode = http.POST(httpRequestData);

if (httpResponseCode>0) {
//se não hover erro
Serial.print("Código de resposta HTTP: ");
Serial.println(httpResponseCode);
}
else {
// se houver erro imprime: codigo de erro: xxx
Serial.print("Código de erro: ");
Serial.println(httpResponseCode);
}
// Recursos gratuitos
http.end();

}
else {
Serial.println(“WiFi Disconnected”);
}
//Send an HTTP POST request every 30 seconds
delay(30000);
}
/////////////////////////////////////////////////////////////////////////////////////////

MONITOR SERIAL
Dados de solicitação http: api_key=tPmAT5Ab3j7F9&sensor=BMP280&location=Office&value1=29.88&value2=27.44&value3=1009.96
Conectado à rede WiFi com endereço IP: 192.168.0.23
Código de erro: -1

]]>
By: Warner Krelekamp https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/#comment-1017688 Tue, 18 Mar 2025 13:29:38 +0000 https://randomnerdtutorials.com/?p=85922#comment-1017688 In reply to Warner Krelekamp.

Found the culprit: You have to install Adafruit_Unified_Sensor library

]]>
By: Warner Krelekamp https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/#comment-1017687 Tue, 18 Mar 2025 13:21:46 +0000 https://randomnerdtutorials.com/?p=85922#comment-1017687 I get:
compilation terminated.
exit status 1
Adafruit_Sensor.h: No such file or directory
And going in the library manager I cannot find Adafruit_Sensor.

]]>
By: Chris https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/#comment-999922 Tue, 14 Jan 2025 21:58:40 +0000 https://randomnerdtutorials.com/?p=85922#comment-999922 Hey, I think for ESP32 a “client.stop();” need to be added.
Otherwise I ran into a “SSL – Memory allocation failed” if I try to connect to several servers.

// Free resources
client.stop();
https.end();

]]>
By: Claes Rolen https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/#comment-991079 Fri, 13 Dec 2024 07:50:19 +0000 https://randomnerdtutorials.com/?p=85922#comment-991079 In reply to Earl.

I also had a similar problem, adding delete solved it for me:

// Free resources
https.end();
delete client; // <----- clean up

//Claes

]]>
By: Earl https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/#comment-910311 Sat, 27 Apr 2024 22:00:05 +0000 https://randomnerdtutorials.com/?p=85922#comment-910311 In reply to Ric.

Hi Ric, I am experiencing the same issue.
On one instance the serial monitor provided a hint:
“E (1887635) esp-sha: Failed to allocate buf memory”
I added the snippet of code you provided that monitors Memory and Heap.
The Heap started at 277112 and gradually declines with each post.
When it gets to 48280 it then starts to generate a Error code: -1 and
E (522970) esp-sha: Failed to allocate buf memory

I’m not clear on what the fix was you referred to above.

]]>
By: Denis https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/#comment-910010 Fri, 26 Apr 2024 20:00:12 +0000 https://randomnerdtutorials.com/?p=85922#comment-910010 In reply to ElG.

Hello! I have the same problem. How did you solve it? Thank you.

]]>
By: MANOEL LUCAS MARTHOS https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/#comment-905121 Thu, 11 Apr 2024 19:38:18 +0000 https://randomnerdtutorials.com/?p=85922#comment-905121 In reply to manoel marthos.

Hi! Can I use WiFiClientSecure client instead of WiFiClientSecure *client = new WiFiClientSecure ? Could there be a problem?
for example:
// WiFiClientSecure *client = new WiFiClientSecure;
WiFiClientSecure client;
// *client->setInsecure(); //don’t use SSL certificate
client.setInsecure(); //don’t use SSL certificate
HTTPClient https;
Apparently it works fine.

]]>
By: manoel marthos https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/#comment-904603 Tue, 09 Apr 2024 03:08:04 +0000 https://randomnerdtutorials.com/?p=85922#comment-904603 After 63 accepted connections, 63 successful submissions, it declines all the others. Almost always 63 connections accepted and data sent and the next ones are always refused. Regardless of the time between connections.
I tested it on two servers. The second is a VPS. I hired a VPS (Virtual Private Server) server for a short period of time to do more tests with Esp32, being able to change some configuration values in msql.
Even modifying some parameters in the mysql of the VPS, maximum limits for example. The result was the same: after 63 posts error returns (-1), only by restart the post again, 63 succeeded. That is, he refused to connect after 63. Always between 63 most of the time and once in a while 62. So fixed values, not random. And it doesn’t matter how long it takes between posts/sends, 3 seconds, ten seconds, 30 seconds, or 60 seconds loop. And it doesn’t matter if the program I made or the demo program. The result is always: it accepts 63 connections, from there to the rejection and the looks like -1

]]>
By: Brandon https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/#comment-902558 Sun, 31 Mar 2024 17:22:57 +0000 https://randomnerdtutorials.com/?p=85922#comment-902558 In reply to Matthias.

Hello, Matthias.

Have you solved this problem? I have the same error and haven’t found the solution yet.

Thank you.

]]>