Connect NodeMCU and send DHT Sensor data to MYSQL by PHP (Xampp)

Hi ,

i tried to connect the nodemcu to xampp database but i cannot connect to the database my arduino out put giving me some kind of SSL errors and i dont know how to fix it.

This is my insert php code.

<?php

    $temp     =(float) $_GET['temp'];
    $humidity = (float)$_GET['humidity'];
   //var_dump($humidity);die;

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "temperature_humidity";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO temps (temp, humidity)
    VALUES ('".$temp."', '".$humidity."')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "
" . $e->getMessage();
    }

$conn = null;

?>

And this is my Arduino Code

#include <ESP8266WiFi.h>

const char* ssid     = "AdiWifi";
const char* password = "adithya123";

const char* host = "192.168.43.220";

float temp = 23;
float humidity = 34;  
void setup() {

 Serial.begin(9600);
 delay(10);

 Serial.println();
 Serial.println();
 Serial.print("Connecting...");
 Serial.println(ssid);

 WiFi.begin(ssid, password);

 while(WiFi.status() !=WL_CONNECTED)
 {
    delay(500);
    Serial.print(".");
     
 }

 Serial.println("");
 Serial.println("WiFi Connected");
 Serial.println("IP Address :");
 Serial.println(WiFi.localIP());


}
 
void loop() {
  
 Serial.print("Connecting to");
 Serial.println(host);

 WiFiClient client;

 const int httpPort = 8080;
 if(!client.connect(host, httpPort)){
     Serial.println("connection Failed");
     return;  
  }
   //Creating URI
  String url = "http://localhost/Nodemcu/save.php?";
         url += "temp=";
         url += temp;
         url += "&humidity";
         url += humidity;

  Serial.print("Requesting URL :");
  Serial.println(url);

 //This will send the request to the server and print them
  client.print(String("GET") + url + "HTTP/1.1\r\n" + 
               "HOST :" + host + "\r\n" + 
               "Connection: close \r\n\r\n");

  unsigned long timeout =millis();             
  while(client.available() == 0){
     if(millis() - timeout > 5000){
      Serial.println(">>>Client Timeout");
      client.stop();
      return;
      }
  }

  while(client.available()){
     String line = client.readStringUntil('\r');
     Serial.print(line);
  }
  Serial.println();
  Serial.println("Closing Connection");

 delay(10000);
}

And these are the errors im getting

Connecting to192.168.43.220
Requesting URL :http://localhost/Nodemcu/save.php?temp=27.00&humidity2.00
HTTP/1.1 400 Bad Request
Date: Thu, 04 Apr 2019 08:29:28 GMT
Server: Apache/2.4.38 (Win64) OpenSSL/1.1.1b PHP/7.3.3
Vary: accept-language,accept-charset
Accept-Ranges: bytes
Connection: close
Content-Type: text/html; charset=utf-8
Content-Language: en
Expires: Thu, 04 Apr 2019 08:29:28 GMT

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Bad request!</title>
<link rev="made" href="mailto:postmaster@localhost" />
<style type="text/css"><!--/*--><![CDATA[/*><!--*/ 
    body { color: #000000; background-color: #FFFFFF; }
    a:link { color: #0000CC; }
    p, address {margin-left: 3em;}
    span {font-size: smaller;}
/*]]>*/--></style>
</head>

<body>
<h1>Bad request!</h1>
<p>


    Your browser (or proxy) sent a request that
    this server could not understand.

</p>
<p>
If you think this is a server error, please contact
the <a href="mailto:postmaster@localhost">webmaster</a>.

</p>

<h2>Error 400</h2>
<address>
  <a href="/">localhost</a>

  <span>Apache/2.4.38 (Win64) OpenSSL/1.1.1b PHP/7.3.3</span>
</address>
</body>
</html>


Closing Connection

please help me with this…

  String url = "http://localhost/Nodemcu/save.php?";
         url += "temp=";
         url += temp;
         url += "&humidity";
         url += humidity;

Another clueless person. You clearly have no idea what localhost means. Look it up.

Unless you have managed to port PHP and MySQL to the Arduino, the URL does NOT involve localhost.

A GET request does NOT contain a protocol OR a host name, either.

 String url = "/Nodemcu/save.php?";
         url += "temp=";
         url += temp;
         url += "&humidity";
         url += humidity;

sorry sir i even changed it like this but still getting error nor connecting

Adithyawije:

 String url = "/Nodemcu/save.php?";

url += "temp=";
        url += temp;
        url += "&humidity";
        url += humidity;




sorry sir i even changed it like this but still getting error nor connecting

If you can't connect, you can't make a GET request, so the correctness/boneheadedness of your URL is meaningless. It would have been nice of you had said what your problem was up front.

Can you connect to 192.168.43.220 on port 8080 using another PC in the network?

The messages that you showed in your initial post indicate that you CAN connect.

Show your complete current code AND your current output.

PaulS:
If you can’t connect, you can’t make a GET request, so the correctness/boneheadedness of your URL is meaningless. It would have been nice of you had said what your problem was up front.

Can you connect to 192.168.43.220 on port 8080 using another PC in the network?

The messages that you showed in your initial post indicate that you CAN connect.

Show your complete current code AND your current output.

yes i can connect to 192.168.43.220 on port 8080…

#include <ESP8266WiFi.h>

const char* ssid     = "AdiWifi";
const char* password = "adithya123";

const char* host = "192.168.43.219";

float temp = 23;
float humidity = 34;  
void setup() {

 Serial.begin(9600);
 delay(10);

 Serial.println();
 Serial.println();
 Serial.print("Connecting...");
 Serial.println(ssid);

 WiFi.begin(ssid, password);

 while(WiFi.status() !=WL_CONNECTED)
 {
    delay(500);
    Serial.print(".");
     
 }

 Serial.println("");
 Serial.println("WiFi Connected");
 Serial.println("IP Address :");
 Serial.println(WiFi.localIP());


}
 
void loop() {

  //=================================
     temp = temp + 2;
     humidity +=  2; 
  //=================================
 Serial.print("Connecting to");
 Serial.println(host);

 WiFiClient client;

 const int httpPort = 8080;
 if(!client.connect(host, httpPort)){
     Serial.println("connection Failed");
     return;  
  }
   //Creating URI
  String url = "/Nodemcu/save.php?";
         url += "temp=";
         url += temp;
         url += "&humidity=";
         url += humidity;

  Serial.print("Requesting URL :");
  Serial.println(url);

 //This will send the request to the server and print them
  client.print(String("GET") + url + "HTTP/1.0 \r\n" + 
                "HOST :" + host + "\r\n" +
              // "Accept: *" + "/" + "*\r\n" +
              // "Content-Length: " + temp.length() + humidity.length() +"\r\n" +
               //"Content-Type: aplication/x-www-form-urlencoded\r\n" + 
               "Connection: close \r\n\r\n");

  unsigned long timeout =millis();             
  while(client.available() == 0){
     if(millis() - timeout > 5000){
      Serial.println(">>>Client Timeout");
      client.stop();
      return;
      }
  }

  while(client.available()){
     String line = client.readStringUntil('\r');
     Serial.print(line);
  }
  Serial.println();
  Serial.println("Closing Connection");

 delay(10000);
}

This is my current code

Connecting to192.168.43.219
Requesting URL :/Nodemcu/save.php?temp=43.00&humidity=54.00
HTTP/1.1 400 Bad Request
Date: Mon, 08 Apr 2019 10:22:51 GMT
Server: Apache/2.4.38 (Win64) OpenSSL/1.1.1b PHP/7.3.3
Vary: accept-language,accept-charset
Accept-Ranges: bytes
Connection: close
Content-Type: text/html; charset=utf-8
Content-Language: en
Expires: Mon, 08 Apr 2019 10:22:51 GMT

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Bad request!</title>
<link rev="made" href="mailto:postmaster@localhost" />
<style type="text/css"><!--/*--><![CDATA[/*><!--*/ 
    body { color: #000000; background-color: #FFFFFF; }
    a:link { color: #0000CC; }
    p, address {margin-left: 3em;}
    span {font-size: smaller;}
/*]]>*/--></style>
</head>

<body>
<h1>Bad request!</h1>
<p>


    Your browser (or proxy) sent a request that
    this server could not understand.

</p>
<p>
If you think this is a server error, please contact
the <a href="mailto:postmaster@localhost">webmaster</a>.

</p>

<h2>Error 400</h2>
<address>
  <a href="/">localhost</a>

  <span>Apache/2.4.38 (Win64) OpenSSL/1.1.1b PHP/7.3.3</span>
</address>
</body>
</html>


Closing Connection

This is the current output error popping.

HTTP/1.1 400 Bad Request i thinks this is the main error i dont know how to fix this sir!!

PaulS:
If you can't connect, you can't make a GET request, so the correctness/boneheadedness of your URL is meaningless. It would have been nice of you had said what your problem was up front.

Can you connect to 192.168.43.220 on port 8080 using another PC in the network?

The messages that you showed in your initial post indicate that you CAN connect.

Show your complete current code AND your current output.

Hi PaulS,

Thanks for replying and giving solutions

problem is in this line

 client.print(String("GET") + url + "HTTP/1.0 \r\n" +

and i corrected it like this

 client.print(String("GET /") + url + "HTTP/1.0 \r\n" +

had to put " / " after GET.

Now my program works.

Thanks PaulS