# Need multiple "HTTP Get Request"

**URL:** <https://forum.arduino.cc/t/need-multiple-http-get-request/188787>\
**Category:** Programming\
**Created:** [October 16, 2013, 12:28pm UTC](https://forum.arduino.cc/t/need-multiple-http-get-request/188787 "2013-10-16T12:28:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 16, 2013, 12:28pm UTC](https://forum.arduino.cc/t/need-multiple-http-get-request/188787/1 "2013-10-16T12:28:32Z")

</div>

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:

```cpp
#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:

```cpp
<?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.

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [October 16, 2013, 1:01pm UTC](https://forum.arduino.cc/t/need-multiple-http-get-request/188787/2 "2013-10-16T13:01:27Z")

</div>

Here is an example sketch that sends a request for a page every 30 seconds. Use the GET method.  
[http://playground.arduino.cc/Code/WebClient](http://playground.arduino.cc/Code/WebClient)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 16, 2013, 1:10pm UTC](https://forum.arduino.cc/t/need-multiple-http-get-request/188787/3 "2013-10-16T13:10:10Z")

</div>

```cpp
  sensor1();

```

The name says NOTHING about what the function does.

```cpp
  client.println("GET /sensor/sensor.php?data=0");
  
  client.println(" HTTP/1.1");

```

The GET should be sent using client.print(). The HTTP part should be sent BEFORE the cr/lf sent by the println() method.

The Host record is not needed, unless you are hosting multiple domains - unlikely, since localhost means the serving running on this computer (the Arduino). And, that is not the machine you are actually sending the request to.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 16, 2013, 3:59pm UTC](https://forum.arduino.cc/t/need-multiple-http-get-request/188787/4 "2013-10-16T15:59:20Z")

</div>

Thank you to all the reply; I actually found out the answer by myself.

The answer to this issue is that  
every time I'm going to call an HTTP GET Request, i suppose to connect to the server every time;  
in which i had forgotten.

---

<div class="post-metadata">

**Author:** ![Bergerie](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@Bergerie](https://forum.arduino.cc/u/Bergerie)\
**Post date:** [August 9, 2016, 6:29am UTC](https://forum.arduino.cc/t/need-multiple-http-get-request/188787/5 "2016-08-09T06:29:15Z")

</div>

I'm trying to carry out two successive HTTP GET using the following code.  
if (client.connect(server, 3480)) {  
client.print("GET /data\_request?id=variableset&DeviceNum=125&serviceId=urn:upnp-org:serviceId:TemperatureSensor1&&Variable=CurrentTemperature");  
client.print("&Value=");  
client.print(temperature,1);  
client.println(" HTTP/1.0");  
client.print("&&");

client.print("GET /data\_request?id=variableset&DeviceNum=127&serviceId=urn:micasaverde-com:serviceId:HumiditySensor1&Variable=CurrentLevel");  
client.print("&Value=");  
client.print(humidity,0);  
client.println(" HTTP/1.0");  
client.println();

}  
else {  
//if there is no connection  
Serial.println("Connection Error");  
}  
//stop connection with VERA  
client.stop();  
}

Unfortunately the second GET command does not get across. Have I made a syntax error or shall this always be done through an individual connection each time. I had thought that using client.print("&&"); I could have successive GET commands.  
Any advice would be appreciated.

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [August 9, 2016, 12:18pm UTC](https://forum.arduino.cc/t/need-multiple-http-get-request/188787/6 "2016-08-09T12:18:09Z")

</div>

You must use a "Connection: Keep-Alive" or close the first connection and establish another. The Keep-Alive is more difficult to maintain. The new connection is slower.

---

<div class="post-metadata">

**Author:** ![Bergerie](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@Bergerie](https://forum.arduino.cc/u/Bergerie)\
**Post date:** [August 10, 2016, 6:34am UTC](https://forum.arduino.cc/t/need-multiple-http-get-request/188787/7 "2016-08-10T06:34:07Z")

</div>

@SurferTim, I take it this is what you meant? However, still only one (first GET) data is coming through. Where did I make a mistake?

if (client.connect(server, 3480))  
{

client.print("GET /data\_request?id=variableset&DeviceNum=125&serviceId=urn:upnp-org:serviceId:TemperatureSensor1&&Variable=CurrentTemperature");  
client.print("&Value=");  
client.print(temperature,1);  
client.println(" HTTP/1.0");  
client.println("Connection: Keep-Alive");

client.print("GET /data\_request?id=variableset&DeviceNum=127&serviceId=urn:micasaverde-com:serviceId:HumiditySensor1&Variable=CurrentLevel");  
client.print("&Value=");  
client.print(humidity,0);  
client.println(" HTTP/1.0");  
client.println();  
client.stop();  
}  
else  
{  
//if there is no connection  
Serial.println("Connection Error");  
client.stop();

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:22pm UTC](https://forum.arduino.cc/t/need-multiple-http-get-request/188787/8 "2021-05-05T20:22:52Z")

</div>

Closed on May 5, 2021, 8:22pm UTC


