0

I need help to record databаse from dht11 sensor to mysql server. this is my code:

#include <DHT.h>
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // RESERVED MAC ADDRESS
EthernetClient client;

IPAddress server(192,168,0,102); // IP Adres (or name) of server to dump data to

#define DHTPIN 2 // SENSOR PIN
#define DHTTYPE DHT11 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);

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

float temperature = 0;  // TEMPERATURE VAR
float humidity = 0; // HUMIDITY VAR
String data;

void setup() { 
    Serial.begin(115200);

    if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP"); 
    }

    dht.begin(); 
    delay(10000); // GIVE THE SENSOR SOME TIME TO START

       temperature = (float) dht.readTemperature();
       humidity = (float) dht.readHumidity();

}

void loop(){

    currentMillis = millis();
    if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
        previousMillis = currentMillis;
        temperature = (float) dht.readTemperature();
                humidity = (float) dht.readHumidity();
    }


    if (client.connect(server, 3306)) {
          Serial.print("temperature=" );
          Serial.println( temperature );
          Serial.print("humidity=" );
          Serial.println( humidity );
          Serial.println("-> Connected");
            if(client.connected()){
            // Make a HTTP request:
            client.print( "GET /add_data.php?");
            client.print("temperature=");
            client.print( temperature);
            client.print("humidity=");
            client.print( humidity);
            client.println( " HTTP/1.1");
            client.println( "Host: 192.168.0.102" );
            //client.println(server);
            client.println( "Connection: close" );
            client.println();
            client.println();
            client.stop();
          }
        }
    if (client.connected()) { 
        client.stop();  // DISCONNECT FROM THE SERVER
    }

    delay(30000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}

I connected to database but can't make records. this is output on serial monitor: temperature=21.00 humidity=73.00 -> Connected

this is my php:add_data.php

<?php
    // Connect to MySQL
    include("dbconnect.php");

    // Prepare the SQL statement
    $SQL = "INSERT INTO test.test.temperature (temperature ,humidity) VALUES ('".$_GET["temperature"]."', '".$_GET["humidity="]."')";

    // Execute SQL statement
    mysql_query($SQL);

    // Go to the review_data.php (optional)
    header("Location: review_data.php");
?>

dbconnect.php=>

<?php
$MyUsername = "root";  // enter your username for mysql
$MyPassword = "fifa2005";  // enter your password for mysql
$MyHostname = "localhost";      // this is usually "localhost" unless your database resides on a different server

$dbh = mysql_pconnect($MyHostname , $MyUsername, $MyPassword);
$selected = mysql_select_db("test",$dbh);
?>

review_data.php=>

<html>
<head>
    <title>Arduino Temperature Log</title>
    <style type="text/css">
        .table_titles, .table_cells_odd, .table_cells_even {
            padding-right: 20px;
            padding-left: 20px;
            color: #000;
        }
        .table_titles {
            color: #FFF;
            background-color: #666;
        }
        .table_cells_odd {
            background-color: #CCC;
        }
        .table_cells_even {
            background-color: #FAFAFA;
        }
        table {
            border: 2px solid #333;
        }
        body { font-family: "Trebuchet MS", Arial; }
    </style>
</head>

<body>
<h1>Arduino Temperature Log</h1>
<table border="0" cellspacing="0" cellpadding="4">
    <tr>
        <td class="table_titles">ID</td>
        <td class="table_titles">Date and Time</td>
        <td class="table_titles">Temperature</td>
        <td class="table_titles">Humidity</td>
    </tr>
    <?php
    // Retrieve all records and display them
    $result = mysql_query("SELECT * FROM temperature ORDER BY id ASC");

    // Used for row color toggle
    $oddrow = true;

    // process every record
    while( $row = mysql_fetch_array($result) )
    {
        if ($oddrow)
        {
            $css_class=' class="table_cells_odd"';
        }
        else
        {
            $css_class=' class="table_cells_even"';
        }

        $oddrow = !$oddrow;

        echo '<tr>';
        echo '   <td'.$css_class.'>'.$row["id"].'</td>';
        echo '   <td'.$css_class.'>'.$row["timestamp"].'</td>';
        echo '   <td'.$css_class.'>'.$row["temperature"].'</td>';
        echo '   <td'.$css_class.'>'.$row["humidity"].'</td>';
        echo '</tr>';
    }
    ?>
</table>
</body>
</html>
  • You should compare the result client.connect against 1 to make sure it's really connected. (The result might be negative, indicating an error.) Do you see the connection in the server log? If so, it's not really an Arduino problem. – fuenfundachtzig May 11 '15 at 18:51
  • I compare the result of client.connect and everything is ok. :) Maybe i should try in stackoverflow for php :) – Supreme May 11 '15 at 19:07
  • I meant if (client.connect(server, 3306) == 1). And yes, if that is the part where you have problems with, then do so. – fuenfundachtzig May 11 '15 at 20:28
  • Hey! Let's all inject some SQL into that incredibly insecure query!!! :) – Majenko May 25 '15 at 22:24
1

You can break down testing in steps.

Call your PHP page from the browser with some dummy parameters and see if it saves them in the DB.

If that part works use a network sniffer, like WireShark to sniff the TCP connection and make sure the correct data is sent to the PHP server from the Arduino.

Depending on which part is not working you can ask the question in the appropriate stack exchange.

| improve this answer | |
1

You have one problem in the construction of your HTTP GET request on Arduino side:

        // Make a HTTP request:
        client.print( "GET /add_data.php?");
        client.print("temperature=");
        client.print( temperature);
        client.print("humidity=");
        client.print( humidity);

You forgot to separate both request arguments with an ampersand. It should be:

        // Make a HTTP request:
        client.print( "GET /add_data.php?");
        client.print("temperature=");
        client.print( temperature);
        client.print("&humidity=");
        client.print( humidity);

Note the "&humidity" instead of just "humidity".

| improve this answer | |
  • I fixed my mistake and made several checks, but still there is something wrong.I'm not sure that add_data.php receive data or my mysql query is wrong. – Supreme May 12 '15 at 6:12
1

i'm not sure why my old code doesn't work, but this is my new one and it is working fine. Maybe it will help someone.

#include <dht.h>
#include <SPI.h>
#include <Ethernet.h>
EthernetClient client;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "192.168.0.101";
dht DHT;
int interval = 5000; // Wait between dumps
#define DHT11_PIN 2
void setup()
{
  Ethernet.begin(mac);
  Serial.begin(115200);

  //Serial.println("Humidity (%),\tTemperature (C)");
}
void loop()
{
  Serial.print("Temperature2 = ");
    Serial.print(DHT.temperature);
    Serial.println(" *C");

    Serial.print("Humidity = ");
    Serial.print(DHT.humidity);
    Serial.println(" ");

  // READ DATA
  //Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);

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

    client.print( "GET /diplomna/Arduino/add_data.php?");
    client.print("temperature=");;
    client.print( DHT.temperature );
    client.print("&&");
    client.print("humidity=");
    client.println( DHT.humidity );
    client.println( "HTTP/1.1");
    client.println( "Connection: close" );
    client.stop();
    Serial.println("CONNECTED");
  }
  else {
    // you didn’t get a connection to the server:
    Serial.println("–> connection failed/n");
  }
  delay(interval);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.