I am trying to pass data from my IR sensor to my PHP page using the get statement, however, either my PHP is not receiving data or my arduino is not sending data because data is being inputted in my localhost every three seconds, but the data is always zero.
here's the arduino code
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte subnet[] = {255,255,255,0};
IPAddress server(192,168,1,5); // IP ni Server/PC
IPAddress ip(192,168,1,85); // IP ni Arduino
IPAddress gateway(192,168,1,1);
IPAddress dnsIP(192,168,1,1);
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, dnsIP, subnet);
delay(2000);
pinMode(21, INPUT);
}
void loop() {
int sensor = digitalRead(21);
char command = Serial.read();
if (client.connect(server, 8095)){
Serial.println("connected");
client.println("GET arduinophp/index.php?");
client.println("sensor=");
client.println(sensor);
client.println(" HTTP/1.1");
client.println("Host: 192.168.1.5");
client.println("Content-Type: application/x-www-form-urlencoded");
client.stop();
Serial.print("GET /arduinophp/index.php?");
Serial.print("sensor=");
Serial.print(sensor);
Serial.print('\n');
}
else
{
Serial.println("connection failed");
}
delay(10000);
}
and here's the php code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>ARDUINO PHP CONNECTION SIMULATOR</h1>
<p>Simulating PHP to Arduino Serial Connection and Arduino to PHP</p>
<?php
$user = "arduino";
$password = "arduino";
$host = "localhost";
$database = "arduino";
$con = mysqli_connect($host, $user, $password);
$sel = mysqli_select_db($con, $database);
if($con)
{
echo "Connection to database established!";
echo"
";
}
$sql_insert = "insert into databasetable (sensor) values ('".$_GET['sensor']."')";
mysqli_query($con, $sql_insert);
if ($sql_insert)
{
echo "Connection to databse table successful!";
echo"
";
}
if (isset($_GET['sensor'])){
$data = $_GET['sensor'];
echo $data;
}
else{
echo "Data not received";
}
?>
<table width="500" border="1" cellspacing="2" cellpadding="5">
<tr>
<td>DATE</td>
<td>SENSOR</td>
</tr>
<?php
$result = mysqli_query($con, "select * from databasetable");
while($line= mysqli_fetch_array($result))
{
echo'<tr>';
echo '<td>'.date('d/m/Y - H:i:s', strtotime($line["date"])).'</td>';
echo '<td>'.$line["sensor"].'</td>';
//echo '<td>'.$line["servo"].'</td>';
//echo '<td>'.$line["rfid"].'</td>';
echo'</tr>';
}
?>
</table>
</body>
</html>