Failed to send data from Arduino to SQL

Hi! Can you please help me with my code? I attach a copy below.

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h" 
#define DHTPIN 7 
#define DHTTYPE DHT11  

DHT dht(DHTPIN, DHTTYPE);
int piezoPin = 6;
int ledPin = 13;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 254); // ip arduino 
char server[] = "192.168.1.180"; // ip ethernet  
EthernetClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); 
  
  while(!Serial){
    ;//wait for serial port to connect
    }
    
  Serial.println("DHT11 testing"); 
  dht.begin(); 
  pinMode (piezoPin, OUTPUT);
  pinMode(ledPin,OUTPUT);

  //start ethernet connection
  Ethernet.begin(mac, ip);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000); 
  
  float h = dht.readHumidity();
  //pembacaan dalam celcius
  float c = dht.readTemperature();

  
  if (isnan(h) || isnan(c)){
    Serial.println("Sensor reading failed");
    return;  
  }

  
  float htoc = dht.computeHeatIndex(c, h, false);

  if(htoc > 40 ){
    digitalWrite(ledPin, HIGH); //HIGH = ON
    delay(1000); 
    digitalWrite(ledPin, LOW); //LOW = OFF
    delay(1000);
    digitalWrite(piezoPin,HIGH);
    {
      tone(6,3047,400);
      delay(100);
      noTone(7);
      delay(100);
     }
     Serial.println("Warning! ");
     Serial.print("Temperature : ");
     Serial.print(htoc);
     Serial.print(" *C\t");
     Serial.print("Humidity : ");
     Serial.print(h);
     Serial.println(" %\t");
    }
  else{
    digitalWrite(piezoPin,LOW);
    digitalWrite(ledPin, LOW);
    
    
    Serial.print("Temperature : ");
    Serial.print(htoc);
    Serial.print(" *C\t");

    
    Serial.print("Humidity : ");
    Serial.print(h);
    Serial.println(" %");
  }

  // Connect to the server (your computer or web page)  
  if (client.connect(server, 80)) {
    Serial.println("--> connection ok\n");
    client.print("GET /write_data.php?"); // This
    client.print("Temperature: ");
    client.print(htoc);
    client.print("Humidity: ");
    client.print(h); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: 192.168.10.180"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
    client.stop();    // Closing connection to server

  }

  else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }
 
  // Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
  delay(10000);
    
}

I'm using Arduino Uno R3 as main controller, Wiznet Ethernet W5100, 5v piezo and DHT11 temperature and humidity sensor. When I opened my serial monitor it keeps giving me "connection failed" message but the sensor can read the temperature and humidity. I don't know what is wrong with my code or with my arduino , because it's run successfully in that reference project. Thanks

This appears wrong, although the problem you have reported already happened earlier:

    client.print("GET /write_data.php?"); // This
    client.print("Temperature: ");
    client.print(htoc);
    client.print("Humidity: ");
    client.print(h); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor

It should output something like this:

192.168.1.180/write_data.php?Temperature=25&Humidity=50

where Temperature and Humidity are thePHP variable names. So you have to adjust your code accordingly. You did test it in a browser ?

6v6gt:
It should output something like this:

192.168.1.180/write_data.php?Temperature=25&Humidity=50

where Temperature and Humidity are thePHP variable names. So you have to adjust your code accordingly. You did test it in a browser ?

I did it. If I tested it in the browser, it showed in the SQL. But, it can't write the data automatically to the SQL

writedata.JPG

writedata.JPG

Post your updated code.

PaulRB:

writedata.JPG

Post your updated code.

I'm Indonesian, so some comments I write in Indonesian.
This is the .ino code

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h" //library sensor yang telah diimportkan
#define DHTPIN 7 //pin apa yang digunakan
#define DHTTYPE DHT11 //tipe sensor DHT 

DHT dht(DHTPIN, DHTTYPE);
int piezoPin = 6;
int ledPin = 13;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 254); // ip arduino yang diambil secara acak
IPAddress server(192, 168, 1, 180); // ip ethernet dari ipconfig cmd 
EthernetClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //untuk komunikasi serial
  
  while(!Serial){
    ;//wait for serial port to connect
    }
    
  Serial.println("Pengujian DHT11"); //penulisan serial di monitor
  dht.begin(); //prosedur mulai pembacaan modul sensor
  pinMode (piezoPin, OUTPUT);
  pinMode(ledPin,OUTPUT);

  //start ethernet connection
  Ethernet.begin(mac, ip);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000); //menunggu beberapa detik untuk pembacaan
               //pembacaan sensor membutuhkan waktu 250ms
  //pembacaan data kelembaban
  float h = dht.readHumidity();
  //pembacaan dalam celcius
  float c = dht.readTemperature();

  //mengecek pembacaan apakah terjadi kegagalan atau tidak
  if (isnan(h) || isnan(c)){
    Serial.println("Pembacaan data dari modul sensor gagal");
    return;  
  }

  //prosedur pembacaan data index panas dalam bentuk celcius
  float htoc = dht.computeHeatIndex(c, h, false);

  if(htoc > 40 ){
    digitalWrite(ledPin, HIGH); //HIGH = ON
    delay(1000); //waktu tunggu saat nyala = 1 detik, dalam milidetik
    digitalWrite(ledPin, LOW); //LOW = OFF
    delay(1000);
    digitalWrite(piezoPin,HIGH);
    {
      tone(6,3047,400);
      delay(100);
      noTone(7);
      delay(100);
     }
     Serial.println("PERINGATAN! Kondisi saat ini : ");
     Serial.print("Suhu : ");
     Serial.print(htoc);
     Serial.print(" *C\t");
     Serial.print("Kelembaban : ");
     Serial.print(h);
     Serial.println(" %\t");
    }
  else{
    digitalWrite(piezoPin,LOW);
    digitalWrite(ledPin, LOW);
    
    //pembacaan nilai data suhu
    Serial.print("Suhu : ");
    Serial.print(htoc);
    Serial.print(" *C\t");

    //pembacaan nilai data kelembaban
    Serial.print("Kelembaban : ");
    Serial.print(h);
    Serial.println(" %");
  }

  // Connect to the server (your computer or web page)  
  if (client.connect(server, 80)) {
    Serial.println("--> connection ok\n");
    client.print("GET /write_data.php?"); // This
    client.print("Temperature: ");//This
    client.print(htoc);//This
    client.print("Humidity: ");//This
    client.print(h); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.print("Host: "); 
    client.println(server);// IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
    client.stop();    // Closing connection to server

  }

  else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }
 
  // Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
  delay(10000);
    
}

This is the "write_data.php" code :

<?php

    // Prepare variables for database connection
   
    $dbusername = "arduino";  // enter database username
    $dbpassword = "arduinotest";
    $server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"

    // Connect to your database

    $dbconnect = mysql_connect($server, $dbusername, $dbpassword);
    $dbselect = mysql_select_db("test",$dbconnect);

    // Prepare the SQL statement

    $sql = "INSERT INTO test.sensor (temperature, humidity) VALUES ('".$_GET["temperature"]."', '".$_GET["humidity"]."')";    

    // Execute SQL statement

    mysql_query($sql);

?>

This is the "get_data.php" code :

<?php
$url=$_SERVER['REQUEST_URI'];
header("Refresh: 5; URL=$url");  // Refresh the webpage every 5 seconds
?>
<html>
<head>
    <title>Temperature and Humidity Sensor</title>
</head>
    <body>
        <h1>Temperature and Humidity Sensor Readings</h1>
    <table border="0" cellspacing="0" cellpadding="4">
      <tr>
            <td>ID</td>
            <td>Timestamp</td>
            <td>Temperature</td>
 <td>Humidity</td>
      </tr>
      
<?php
    // Connect to database

   // IMPORTANT: If you are using XAMPP you will have to enter your computer IP address here, if you are using webpage enter webpage address (ie. "www.yourwebpage.com")
    $con=mysqli_connect("192.168.1.180","arduino","arduinotest","test");
 if (mysqli_connect_errno()){
 echo "Failed to connect to MySQL:".mysqli_connect_error();
 }
       
    // Retrieve all records and display them   
    $sql = "SELECT * FROM sensor ORDER BY id DESC";
 $result = mysqli_query($con, $sql);
      
    // Process every record
    
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {      
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['time'] . "</td>";
        echo "<td>" . $row['temperature'] . "</td>";
 echo "<td>" . $row['humidity'] . "</td>";
        echo "</tr>";
    }    
 mysqli_free_result($result); 
 
        
    // Close the connection   
    mysqli_close($con);
?>
    </table>
    </body>
</html>

You can start by making this change:

  if (client.connect(server, 80)) {
    Serial.println("--> connection ok\n");
    client.print("GET /write_data.php?"); // This
    client.print("Temperature: ");//This
    client.print(htoc);//This
    client.print("Humidity: ");//This
    client.print(h);

To:

  if (client.connect(server, 80)) {
    Serial.println("--> connection ok\n");
    client.print("GET /write_data.php?"); // This
    client.print("temperature=");// lowercase
    client.print(htoc);//This
    client.print("&humidity=");//lower case
    client.print(h);

Put this back as it was originally since you want server in a c_string format:

IPAddress server(192, 168, 1, 180); // ip ethernet dari ipconfig cmd

Can you see the access logs on the web server ? If so, include a sample of entries in this post.

ulfahputribisba:
I did it.

No, you did not do it.

Go back and read post #1 again.

I update my .ino to this code :

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h" //library sensor yang telah diimportkan
#define DHTPIN 7 //pin apa yang digunakan
#define DHTTYPE DHT11 //tipe sensor DHT 

DHT dht(DHTPIN, DHTTYPE);
int piezoPin = 6;
int ledPin = 13;

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 254); // ip arduino yang diambil secara acak
IPAddress server(192, 168, 1, 180); // ip ethernet dari ipconfig cmd 
EthernetClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //untuk komunikasi serial
  
  while(!Serial){
    ;//wait for serial port to connect
    }
    
  Serial.println("Pengujian DHT11"); //penulisan serial di monitor
  dht.begin(); //prosedur mulai pembacaan modul sensor
  pinMode (piezoPin, OUTPUT);
  pinMode(ledPin,OUTPUT);

  //start ethernet connection
  Ethernet.begin(mac, ip);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  currentMillis = millis();
  if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
    previousMillis = currentMillis;
    
  //pembacaan data kelembaban
  float h = dht.readHumidity();
  //pembacaan dalam celcius
  float c = dht.readTemperature();

  //mengecek pembacaan apakah terjadi kegagalan atau tidak
  if (isnan(h) || isnan(c)){
    Serial.println("Pembacaan data dari modul sensor gagal");
    return;  
  }

  //prosedur pembacaan data index panas dalam bentuk celcius
  float htoc = dht.computeHeatIndex(c, h, false);

  if(htoc > 40 ){
    digitalWrite(ledPin, HIGH); //HIGH = ON
    delay(1000); //waktu tunggu saat nyala = 1 detik, dalam milidetik
    digitalWrite(ledPin, LOW); //LOW = OFF
    delay(1000);
    digitalWrite(piezoPin,HIGH);
    {
      tone(6,3047,400);
      delay(100);
      noTone(7);
      delay(100);
     }
     Serial.println("PERINGATAN! Kondisi saat ini : ");
     Serial.print("Suhu : ");
     Serial.print(htoc);
     Serial.print(" *C\t");
     Serial.print("Kelembaban : ");
     Serial.print(h);
     Serial.println(" %\t");
    }
  else{
    digitalWrite(piezoPin,LOW);
    digitalWrite(ledPin, LOW);
    
    //pembacaan nilai data suhu
    Serial.print("Suhu : ");
    Serial.print(htoc);
    Serial.print(" *C\t");

    //pembacaan nilai data kelembaban
    Serial.print("Kelembaban : ");
    Serial.print(h);
    Serial.println(" %");
  }

  // Connect to the server (your computer or web page)  
  if (client.connect(server, 80)) {
    Serial.println("--> connection ok\n");
    client.print("GET /write_data.php?"); // This
    Serial.print("GET /write_data.php?");
    client.print("temperature: ");//This
    Serial.print("temperature: ");
    client.print(htoc);//This
    Serial.print(htoc);
    client.print("&humidity: ");//This
    Serial.print("&humidity: ");
    client.print(h); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    Serial.print(h);
    client.println(" HTTP/1.1"); // Part of the GET request
    Serial.print(" HTTP/1.1");
    client.print("Host: 192.168.1.180");// IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    Serial.print("Host: 192.168.1.180");
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    Serial.print("Connection: close");
    client.println(); // Empty line
    Serial.println();
    client.println(); // Empty line
    Serial.println();
    client.stop();    // Closing connection to server

  }
 }
  else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }
 
  // Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
  delay(10000);
    
}

And this is what happend :

failed.JPG

When you get time, look over the previous posts in the thread and check that you have followed the suggestions and answered the open questions.

failed.JPG

The above seems to show that the Arduino is unable to connect to the server at this line:

  if (client.connect(server, 80)) {

The sketch is not even getting to the lines where the incorrectly formated GET request is being sent.