Hi everyone,
I am trying to upload pH value from analog ph meter kit(ph sensor) to a sql database.
I am using analog ph meter kit, arduino uno, arduino ethernet shield and RJ 45 cable.
I am using Xampp(https://http://www.localhost:8080/phpmyadmin/) database running on my PC. I have created a table(phvalue) in this database(phvalue). I have saved php files in my computer at C:xampp/htdocs/fyp. I have hooked up Ethernet Shield to one of internet ports. After all this, i don't find table getting updated by pH values. Is there any mistake? Please guide me.
Here is the source code for Arduino:
/*
# This sample code is used to test the pH meter V1.0.
# Editor : YouYou
# Ver : 1.0
# Product: analog pH meter
# SKU : SEN0161
*/
#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00 //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40 //times of collection
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>
LiquidCrystal lcd(2,3,4,5,6,7);
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
int pHArrayIndex=0;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip (10,6,2,25);
EthernetClient client;
IPAddress server (12,34,56,78);
int interval = 5000;
void setup()
{
pinMode(LED,OUTPUT);
Serial.begin(9600);
while (!Serial) {; }
Ethernet.begin(mac, ip);
Serial.print("LAN IP : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());
Serial.println("pH meter experiment!"); //Test the serial monitor
lcd.begin(16, 2);
lcd.print("pH value: ");
}
void loop()
{
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float pHValue,voltage;
if(millis()-samplingTime > samplingInterval)
{
pHArray[pHArrayIndex++]=analogRead(SensorPin);
if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
pHValue = 3.5*voltage+Offset;
samplingTime=millis();
if(millis() - printTime > printInterval) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
{
Serial.print("Voltage:");
Serial.print(voltage,2);
Serial.print(" pH value: ");
Serial.println(pHValue,2);
digitalWrite(LED,digitalRead(LED)^1);
printTime=millis();
}
}
if (client.connect(server, 8080)) {
Serial.println("-> Connected");
client.print( "GET c:/xampp/htdocs/fyp/add_data.php?");
client.print("pHValue=");
client.print( "pHValue" );
client.println( "HTTP/1.1" );
client.print( "Host: " );
client.println( server );
client.println( "Connection: close" );
client.println();
client.println();
client.stop();
}
else {
// you didn't get a connection to the server:
Serial.println("--> connection failed");
}
delay(interval);
{
lcd.setCursor(7,1);
lcd.print(pHValue);
delay (100);
}
}
double avergearray(int* arr, int number){
int i;
int max,min;
double avg;
long amount=0;
if(number<=0){
Serial.println("Error number for the array to avraging!/n");
return 0;
}
if(number<5){ //less than 5, calculated directly statistics
for(i=0;i<number;i++){
amount+=arr[i];
}
avg = amount/number;
return avg;
}else{
if(arr[0]<arr[1]){
min = arr[0];max=arr[1];
}
else{
min=arr[1];max=arr[0];
}
for(i=2;i<number;i++){
if(arr[i]<min){
amount+=min; //arr<min
min=arr[i];
}else {
if(arr[i]>max){
amount+=max; //arr>max
max=arr[i];
}else{
amount+=arr[i]; //min<=arr<=max
}
}//if
}//for
avg = (double)amount/(number-2);
}//if
return avg;
}
Here is mysql_connect.php
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'phvalue');
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());
@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
?>
here is the pHvalue.php
<?php
// Start MySQL Connection
include('mysql_connect.php');
?>
<html>
<head>
<title>Arduino pH Value 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 pHvalue Log</h1>
<table border="0" cellspacing="0" cellpadding="4">
<tr>
<td class="table_titles">ID</td>
<td class="table_titles">pH Value</td>
<td class="table_titles">Date and Time</td>
</tr>
<?php
// Retrieve all records and display them
$query = "SELECT pHid,pHvalue,date FROM phvalue";
$result = @mysql_query ($query);
// 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["pHid"].'</td>';
echo ' <td'.$css_class.'>'.$row["pHvalue"].'</td>';
echo ' <td'.$css_class.'>'.$row["date"].'</td>';
echo '</tr>';
}
?>
</table>
</body>
</html>
and lastly add_data.php
<?php
// Connect to MySQL
include("mysql_connect.php");
// Prepare the SQL statement
$SQL = "INSERT INTO phvalue.phvalue (pHid, pHvalue, date) VALUES (pHid,pHvalue,NOW() )"; ;
// Execute SQL statement
mysql_query($SQL);
// Go to the review_data.php (optional)
header("pHvalue.php");
?>