Aqib
Published

Advanced Garduino with Data Logging to Database

This not only will automatically water the plants but also will store this data in database.

AdvancedFull instructions provided12 hours2,016
Advanced Garduino with Data Logging to Database

Story

Read more

Code

Code snippet #1

Plain text
CREATE TABLE `garduino`.`data` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Event` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`Moisture` VARCHAR( 10 ) NOT NULL,
`Temperature` VARCHAR( 10 ) NOT NULL,
`Humidity` VARCHAR( 10 ) NOT NULL,
`Heat_index` VARCHAR( 10 ) NOT NULL,
`Pressure` VARCHAR( 10 ) NOT NULL,
`valve_status` CHAR( 10 ) NOT NULL
)

Code snippet #2

Plain text
<?php
$username = "root";
$pass = "";
$host = "localhost";
$db_name = "garduino";
$con = mysqli_connect ($host, $username, $pass);
$db = mysqli_select_db ( $con, $db_name );
?>

Code snippet #3

Plain text
<?php
include ('connection.php');
$sql_insert = "INSERT INTO data (moisture, temperature, humidity, heat_index, pressure, valve_status) VALUES ('".$_GET["moisture"]."', '".$_GET["temperature"]."', '".$_GET["humidity"]."', '".$_GET["heat_index"]."', '".$_GET["pressure"]."', '".$_GET["valve_status"]."')";
if(mysqli_query($con,$sql_insert))
{
echo "Done";
mysqli_close($con);
}
else
{
echo "error is ".mysqli_error($con );
}
?>

Code snippet #5

Plain text
#include <Adafruit_BMP085.h>
#include <Wire.h>
#include <PCD8544.h>
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"

#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
PCD8544 lcd;
Adafruit_BMP085 bmp;

char PRESSURESHOW[4];
char Str1[] = "ON";
char Str2[] = "OFF";
int valve_pin = 9;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {192, 168, 1, 18 }; //Read the code explanation
byte serv[] = {192, 168, 1, 5} ; //Read the code explanation
EthernetClient cliente;

void setup() {
Ethernet.begin(mac, ip);
lcd.begin(84, 48);
dht.begin();
pinMode(valve_pin, OUTPUT);
digitalWrite (valve_pin, HIGH);

lcd.setCursor(0, 0);
lcd.print(" WELCOME ");
lcd.setCursor(0, 1);
lcd.print(" To");
lcd.setCursor(0,2);
lcd.print("ElectronicsHobbyists.com");
delay(2000);
lcd.clear();

if (cliente.connect(serv, 8095)) {
lcd.setCursor(0, 0);
lcd.print("Connection Successful");
delay(1000);
lcd.setCursor(0, 2);
lcd.print("Sending Values");
delay(1000);
cliente.stop(); //Closing the connection
}
else{
lcd.setCursor(0, 0);
lcd.print("Connected Failed");
delay(10000);
}
}

void loop() {
lcd.clear();
int sensorValue = analogRead(A0);
int output = map(sensorValue, 0, 1023, 100, 0); //converting in percentage because the moisture is measured in percentage
float hum = dht.readHumidity(); //Reading the humidity and storing in hum
float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
float fah = dht.readTemperature(true); //reading the temperature in Fahrenheit
float heat_index = dht.computeHeatIndex(fah, hum); //Reading the heat index in Fahrenheit
float heat_indexC = dht.convertFtoC(heat_index); //Converting the heat index in Celsius

if (isnan(hum) || isnan(temp) || isnan(fah)) { //Checking if the arduino have recieved the values or not
lcd.print("Failed to read from DHT sensor!");
return;
}
if (!bmp.begin()){
lcd.print("Failed to read from BMP180!");//if there is an error in communication
while (1) {}
}

String PRESSUREVALUE = String(bmp.readPressure());
// convert the reading to a char array
PRESSUREVALUE.toCharArray(PRESSURESHOW, 4);

if (cliente.connect(serv, 8095)) { //Connecting at the IP address and port we saved before
cliente.print("GET /garduino/write_data.php?"); //Connecting and Sending values to database
cliente.print("moisture=");
cliente.print(output);
cliente.print("&temperature=");
cliente.print(temp);
cliente.print("&humidity=");
cliente.print(hum);
cliente.print("&heat_index=");
cliente.print(heat_indexC);
cliente.print("&pressure=");
cliente.print(PRESSURESHOW);
cliente.print("&valve_status=");

if(output <50)
{
cliente.println(Str1);
}
else
{
cliente.println(Str2);
}

//Printing the values on the LCD
lcd.setCursor(0,0);
lcd.print("Moist ");
lcd.print(output);
lcd.print("%");

lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print("*C ");

lcd.setCursor(0, 2);
lcd.print("Humi: ");
lcd.print(hum);
lcd.print("%\t");

lcd.setCursor(0,3);
lcd.print("Hi: ");
lcd.print(heat_indexC);
lcd.print("*C ");

lcd.setCursor(0,4);
lcd.print("Pre: ");
lcd.print(PRESSURESHOW);
lcd.print("hpa ");

lcd.setCursor(0,5);
lcd.print("Valve: ");

if(output <50)
{
digitalWrite (valve_pin, LOW);
lcd.print(Str1);
}
else
{
digitalWrite (valve_pin, HIGH);
lcd.print(Str2);
}
cliente.stop(); //Closing the connection
}
else {
// if you didn't get a connection to the server:
}
delay(10000);
}

Code snippet #7

Plain text
<?php
$url=$_SERVER['REQUEST_URI'];
header("Refresh: 5; URL=$url"); // Refresh the webpage every 5 seconds
?>
<html>
<head>
<title>Garduino</title>
<style type="text/css">
.table_titles {
padding-right: 40px;
padding-left: 40px;
color: #000;
}
.table_titles {
color: #FFF;
background-color: #0000FF;
}
table {
border: 2px solid #333;
}
body { font-family: "Trebuchet MS", Courier; }
</style>
</head>
<body>
<h1>Advanced Garduino</h1>
<table border="4" cellspacing="4" cellpadding="2">
<tr>
<td class="table_titles">ID</td>
<td class="table_titles">Date and Time</td>
<td class="table_titles">Moisture</td>
<td class="table_titles">Temperature</td>
<td class="table_titles">Humidity</td>
<td class="table_titles">Heat_index</td>
<td class="table_titles">Pressure</td>
<td class="table_titles">Valve_status</td>
</tr>
<?php
include('connection.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['Moisture'] . "</td>";
echo "<td '.$css_class.'>" . $row['Temperature'] . "</td>";
echo "<td '.$css_class.'>" . $row['Humidity'] . "</td>";
echo "<td '.$css_class.'>" . $row['Heat_index'] . "</td>";
echo "<td '.$css_class.'>" . $row['Pressure'] . "</td>";
echo "<td '.$css_class.'>" . $row['valve_status'] . "</td>";
echo "</tr>";
}
// Close the connection
mysqli_close($con);
?>
</table>
</body>
</html>

Github file

https://github.com/carlosefr/pcd8544/archive/master.zip

Github file

https://codeload.github.com/adafruit/Adafruit-BMP085-Library/zip/master

Github

https://github.com/adafruit/DHT-sensor-library.git

Credits

Aqib

Aqib

21 projects • 271 followers

Comments