Hi. I need help with my project. I'm having trouble to send IR sensor data from serial monitor to MySQL database.
Here is my arduino code:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
byte ip[] = { 192, 168, 0, 127 };
byte server[] = { 192, 168, 0, 121 };
//Initialize the Ethernet server library
EthernetClient client;
int LED = 13; // Use the onboard Uno LED
int isSensePin = 7; // This is our input pin
int isSensePin2 = 8;
int isSensePin3 = 9;
int sensor1 = HIGH; // HIGH MEANS NO OBSTACLE
int sensor2 = HIGH;
int sensor3 = HIGH;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(isSensePin, INPUT);
pinMode(isSensePin2, INPUT);
pinMode(isSensePin3, INPUT);
Serial.begin(9600);
while(!Serial)
{
;
}
if(Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
delay(1000);
Serial.println("connecting...");
if(client.connect(server, 80))
{
Serial.println("---connected---");
client.println("GET /arduino/getdata.php?");
client.println("Host: 192.168.0.121");
client.println("Connection: close");
client.println();
}
else
{
Serial.println("---connection failed---");
}
}
void loop()
{
sensor1 = digitalRead(isSensePin);
sensor2 = digitalRead(isSensePin2);
sensor3 = digitalRead(isSensePin3);
int rc = client.connect(server, 80);
if (rc)
{
Serial.println("---connection ok---");
client.print("GET /arduino/getdata.php?");
client.print("sensor1 = ");
client.print(sensor1);
client.print("&sensor2 = \n");
client.print(sensor2);
client.print("&sensor3 = \n");
client.print(sensor3);
client.println("HTTP/1.1");
client.println("Host: 192.168.0.121");
client.println("Connection: close");
client.println();
Serial.print("sensor1 = ");
Serial.println(sensor1);
Serial.print("sensor2 = ");
Serial.println(sensor2);
Serial.print("sensor3 = ");
Serial.println(sensor3);
client.stop();
}
else
{
Serial.println("---connection failed---\n");
Serial.println(rc);
client.stop();
}
delay (500);
sensor1 = digitalRead(isSensePin);
if (sensor1 == LOW)
{
Serial.println("Occupied1");
digitalWrite(LED, HIGH);
}
else
{
Serial.println("Available1");
digitalWrite(LED, LOW);
}
delay(1000);
sensor2 = digitalRead(isSensePin2);
if (sensor2 == LOW)
{
Serial.println("Occupied2");
digitalWrite(LED, HIGH);
}
else
{
Serial.println("Available2");
digitalWrite(LED, LOW);
}
delay(1000);
sensor3 = digitalRead(isSensePin3);
if (sensor3 == LOW)
{
Serial.println("Occupied3");
digitalWrite(LED, HIGH);
}
else
{
Serial.println("Available3");
digitalWrite(LED, LOW);
}
delay(1000);
}
The result that I get when I run the Arduino code:
connecting...
---connected---
---connection failed---
0
Available1
Available2
Available3
---connection ok---
sensor1 = 1
sensor2 = 1
sensor3 = 1
Available1
Available2
Available3
---connection ok---
sensor1 = 0
sensor2 = 1
sensor3 = 0
Occupied1
Available2
Available3
getdata.php:
<?php
$connect = mysqli_connect("localhost", "root", "", "arduino");
$sensor1 = $_GET['sensor1'];
$sensor2 = $_GET['sensor2'];
$sensor3 = $_GET['sensor3'];
$sql_insert = "insert into data (sensor1, sensor2, sensor3) values ('$sensor1', '$sensor2', '$sensor3')";
mysqli_query($connect, $sql_insert);
if($sql_insert)
{
echo "success";
}
else
{
echo "failed";
}
?>
When I test getdata.php, it works well to store the data to the database, and for status.php, it also works fine in retrieve the data.
status.php is where I show the data from MySQL database:
<?php
$url = $_SERVER['REQUEST_URI'];
header("Refresh: 5; URL = $url");
?>
<!DOCTYPE html>
<html>
<title> ARDUINO Parking Status </title>
<head>
<link rel = "stylesheet" type = "text/css" href = "testing.css" />
<meta charset="utf-8">
</head>
<body>
<div class="wrapper">
<header>
<img src="test 2.png" class="img2"/>
</header>
<nav>
<div id="rightcol">
<div class="imgh"><img src="parking.png" class="img4"/></div>
Welcome, Admin<!--?php echo $_SESSION['username']; ?-->.<p/>
<a href="logout.php" class="basicbtt">Logout</a>
<p/>
<hr>
<center><a href="#" class="sidebtt" style="padding: 6px 33px;">PARKING STATUS</a></center>
<p/>
Parking status:-
<p/>
Total:
<p/>
Available:
<p/>
Occupied:
<p/>
<center><a href="user.php" class="sidebtt">REGISTERED USER</a></center>
<center><a href="report.php" class="sidebtt" style="padding: 6px 60px;">FEEDBACK</a></center>
<p/><a href="testing.php">Main</a>
</div>
</nav>
<section>
[b]<div id="tablecol">
<table width="500" border="1" cellspacing="2" cellpadding="5">
<tr>
<th>NO.</th>
<th>TIMESTAMP</th>
<th>PARK 1</th>
<th>PARK 2</th>
<th>PARK 3</th>
</tr>
<?php
$connect = mysqli_connect("localhost", "root", "", "arduino");
$result = mysqli_query($connect, "select * from data");
while($line = mysqli_fetch_array($result))
{
echo'<tr>';
echo'<td>'.$line["id"].'</td>';
echo'<td>'.date('d.m.Y - H:i:s', strtotime($line["timestamp"])).'</td>';
echo'<td>'.$line["sensor1"].'</td>';
echo'<td>'.$line["sensor2"].'</td>';
echo'<td>'.$line["sensor3"].'</td>';
echo'</tr>';
}
?>[/b]
</table>
</div>
</section>
</div>
</body>
</html>
I think there's something wrong with my arduino code. But I don't know what it is. I've always checking the IP address for my PC and the Ethernet shield if there's any changes, and if so, I would change it in my code. But the problem is still the same.