I'd like to know how ESP8266 works with MySQL.


I’m not used to using ESP8266 yet.
I tried to print the value of the sensor to the web server using this. Now I’m going to try to save the sensor’s value in the database. I tried this by looking at a number of examples, but the connection kept failing.
Below are my php code and arduino code.


$servername = ''; //localIP
$username = 'root'; 
$password = 'PASSWORD'; 
$dbName = 'sensordata';  //Database name
$conn = mysqli_connect($servername, $username, $password, $dbname);
$temp = $_GET['temp'];
$humidity = $_GET['humidity'];
$sql = "INSERT INTO sensor2(temperature, humidity) VALUES($temp, $humidity)"; //Insert data to table
mysqli_query($conn, $sql);
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include "Adafruit_SHTC3.h"
#include "Adafruit_SI1145.h"

const char* ssid = "V Pro";
const char* password =  "PASSWORD";
String host = ""; //WiFi IP
const long interval = 5000;
unsigned long previousMillis = 0;
WiFiServer server(80);
WiFiClient client;
HTTPClient http;

void setup() 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  Serial.print("Connecting to ");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); //
  Serial.println("Server started");

void loop() {
  unsigned long currentMillis = millis(); 
  //send data 5sec
  if(currentMillis - previousMillis >= interval) { 
    previousMillis = currentMillis;
    float humidity = 1.1; //send data test
    float temp = 2.2;

    //data send to DB thouth php
    String phpHost = host+"/insert.php?temp="+String(temp)+"&humidity="+String(humidity);
    Serial.print("Connect to ");
    http.begin(client, phpHost);
    http.setTimeout(1000); //update for 1sec
    int httpCode = http.GET();
    if(httpCode > 0) {
      Serial.printf("GET code : %dnn", httpCode);
      if(httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
    else {
      Serial.printf("GET failed, error: %sn", http.errorToString(httpCode).c_str());
  // Web server
  client = server.available();
  if(!client) return;
  Serial.println("New client");
  String request = client.readStringUntil('r');
  Serial.println("request: ");
  while(client.available()) {
  float humidity = 1.1; //send data test
  float temp = 2.2;
  client.print("HTTP/1.1 200 OK");
  client.print("Content-Type: text/htmlrnrn");
  client.print("<!DOCTYPE HTML>");
  client.print("<meta charset="UTF-8" http-equiv="refresh" content="1">");
  client.print("<title>Senrsor test Webpage</title>");
  client.print("<h2>Senrsor test Webpage</h2>");
  client.print("Temperature : ");
  client.print(" °C");
  client.print("Humidity : ");
  client.print(" %");
  Serial.println("Disconnected client");

This is the result of running this.

GET failedm error: connection failed

This is the IP address of the Wi-Fi I connected to.

Connecting to V Pro
IP address:

I don’t know much about the database and the server yet. The IP address is suspected as the cause of this problem. It’s because I’ve followed many examples, but they’ve never been connected properly.

Can I get some help with this problem?

Thank you in advance.

I see only one I address here: This is the local address assigned to the esp by your wifi router.

Your code seems to be sending http requests to that same address - itself - not some other server where the php code is hosted. I guess that wasn't your intention? Where is the IP address of the php server?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.