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>