Sorry, I'm kind of new to this Arduino and writing programs; I'm having a problem with programming
My arduino is suppose to get input from sensor
then once the sensor detects; the LED goes HIGH
Once it goes high, it suppose to send HTTP GET Request using function.
and i have PHP file which will insert a record into the database within my wampserver
As a result, it did send the HTTP GET Request but it only send once (the first time).
Once the LED goes LOW it would not send the HTTP GET Request.
I have tried and looked over google so that it could send every time the function is being called
Here is my code:
#include <Ethernet.h>
#include <SPI.h>
#define trigPin1 13
#define echoPin1 12
#define led1 10
#define led2 11
#define led3 9
byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
EthernetClient client;
IPAddress ip(192,168,0,196);
volatile int state1 = LOW;
int count1 = 0;
void setup() {
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(led1, OUTPUT);
EthernetSetup();
}
void loop() {
sensor1();
delay(500);
}
void sensor1() {
long duration1, distance1;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
if (distance1 < 13)
{
count1++;
if(count1>5 && state1==0)
{
state1 = 1;
count1 = 0;
SEND_TO_PHP(1);
Serial.println(state1);
}
}
else
{
if(state1==1)
{
state1 = 0;
count1 = 0;
SEND_TO_PHP(0);
Serial.println(state1);
}
}
digitalWrite(led1,state1);
}
void EthernetSetup()
{
if (Ethernet.begin(mac) == 0)
Serial.println("Failed to configure Ethernet using DHCP");
else Serial.println("Ethernet Begin (MAC) passed");
Serial.print("Current IP : ");
Serial.println(Ethernet.localIP());
delay(1000);
Serial.println("connecting...");
if (client.connect(ip,80))
Serial.println("connected");
}
void SEND_TO_PHP(int data){//HTTP request
// SEND MAC ADDRESS AND IP to WEB SERVER
Serial.println(data);
if(data==1)
client.println("GET /sensor/sensor.php?data=1");
else if(data==0)
client.println("GET /sensor/sensor.php?data=0");
client.println(" HTTP/1.1");
client.println("Host: localhost");
client.println("Accept: text/html");
client.println("Connection: close");
client.println();
Serial.println();
Serial.println("data sent");
readc();
data = 0;
client.flush();// flush ethernet before stop
client.stop();
}
void readc()
{
int a = 0;
while (client.connected())
{ Serial.println("con");
if (client.available())
{ Serial.println("ava");
char c = client.read();
Serial.print(c);
}
}
}
And here is my PHP file:
<?php
require_once('config.inc.php');
require_once('function.inc.php');
session_start();
$link = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
mysql_select_db(DB_DATABASE,$link) or die("Could not connect database");
// Check connection
if (mysqli_connect_errno()) {
printf("Unable to connect to database: %s", mysqli_connect_error());
exit();}
$sql= "INSERT INTO spotreal(data) VALUES('$_GET[data]')";
mysql_query($sql,$link);
echo "1 record added";
?>
Thank you in advance.