HI!
i am using soil moisture sensor and i want the value which are display on serial monitor must me store in database but i failed and i am new in ardiuno programming
I UESD:
- ARDIUNO UNO .
- ETHERENET SHEILD
- SOIL MOISTURE SENSOR
DATABASE
MYSQL (XAMPP) .
CAN U HELP ME
ARDIUNO CODE
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 6); //Enter the IP of ethernet shield
char server[] = "192, 168, 1,5" ; //Enter the IPv4 address
EthernetClient client;
void setup() {
Ethernet.begin(mac, ip);
Serial.begin(9600); //setting the baud rate at 9600
Serial.println(Ethernet.localIP());
}
void loop() {
int soil_moisture = analogRead(A0);
Serial.print(soil_moisture);
if (client.connect(server, 8080)) { //Connecting at the IP address and port we saved before
Serial.println("connected");
client.print("GET /soil_moisture/data1.php? "); //Connecting and Sending values to database
client.print("soil");
client.print(soil_moisture);
client.print("HTTP/1.1");
//client.print(" ");
client.stop(); //Closing the connection
//printing the value on the serial monitor
Serial.print("soil=");
Serial.println(soil_moisture);
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
delay(10000);
}
Connection1.php
<?php
$username = "soil_moisture";
$pass = "123";
$host = "localhost";
$db_name = "soil_moisture";
$con = mysqli_connect ($host, $username, $pass);
$db = mysqli_select_db ( $con, $db_name );
?>
data1.php
<?php
include ('connection1.php');
$sql_insert = "INSERT INTO soil_moisture.data (soil_moisture) VALUES ('".$_GET["soil_moisture"]."')";
if(mysqli_query($con,$sql_insert))
{
echo "Done";
mysqli_close($con);
}
else
{
echo "error is ".mysqli_error($con );
}
?>
display.php
<?php
$url=$_SERVER['REQUEST_URI'];
header("Refresh: 5; URL=$url"); // Refresh the webpage every 5 seconds
?>
<html>
<head>
<title>Arduino soil moisture Database</title>
<style type="text/css">
.table_titles {
padding-right: 20px;
padding-left: 20px;
color: #000;
}
.table_titles {
color: #FFF;
background-color: #0000FF;
}
table {
border: 2px solid #333;
}
body { font-family: "Trebuchet MS", Courier; }
</style>
</head>
<body>
<h1>Arduino Data Logging to Database</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">soil_moisture</td>
</tr>
<?php
include('connection1.php');
$result = mysqli_query($con,'SELECT * FROM data ORDER BY id DESC');
// Process every record
$oddrow = true;
while($row = mysqli_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['event'] . "</td>";
echo "<td '.$css_class.'>" . $row['soil_moisture'] . "</td>";
echo "</tr>";
}
// Close the connection
mysqli_close($con);
?>
</table>
</body>
</html>