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

Hello

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.

//insert.php

<?php
$servername = '127.0.0.1'; //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);
mysqli_close($conn);
?>
#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 = "169.254.225.148"; //WiFi IP
 
const long interval = 5000;
unsigned long previousMillis = 0;
 
WiFiServer server(80);
WiFiClient client;
HTTPClient http;

void setup() 
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connecting to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); //169.254.225.148
  server.begin();
  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 ");
    Serial.println(phpHost);
    
    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();
        Serial.println(payload);
      }
    } 
    else {
      Serial.printf("GET failed, error: %sn", http.errorToString(httpCode).c_str());
    }
    http.end();
  }
  // Web server
  client = server.available();
  if(!client) return;
 
  Serial.println("New client");
  client.setTimeout(5000);
 
  String request = client.readStringUntil('r');
  Serial.println("request: ");
  Serial.println(request);
 
  while(client.available()) {
    client.read();
  }
 
  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("<html>");
  client.print("<head>"); 
  client.print("<meta charset="UTF-8" http-equiv="refresh" content="1">");
  client.print("<title>Senrsor test Webpage</title>");
  client.print("</head>");
  client.print("<body>");
  client.print("<h2>Senrsor test Webpage</h2>");
  client.print("
");
  client.print("Temperature : ");
  client.print(temp);
  client.print(" °C");
  client.print("
");
  client.print("Humidity : ");
  client.print(humidity);
  client.print(" %");
  client.print("</body>");
  client.print("</html>");
 
  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: 169.254.225.148

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: 169.254.225.148. 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.