Data via Method GET to PHP

Hello guys,

I've searched the forum how to send data via arduino to a PHP page , but I ended up not find the error that prevents me from completing this task , looking at the code I'm using, someone can point to me
the problem in the code, because it does not work.

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x2A, 0x8D };
byte ip[] = { 192, 168, 0, 77 };
byte server[] = { 127, 0, 0, 1 };

EthernetClient client;

float var;

void setup() { 
	Serial.begin(115200);
  Ethernet.begin(mac, ip);
  
	var = 20;
}

void loop(){

  Serial.println(client.status());

	if(client.connect(server,80))
	{
		Serial.println(client.status());
		
		client.println("GET /arduino/index2.php HTTP/1.1"); 
		client.println("Host: 127.0.0.1"); 
		client.println("Content-Type: application/x-www-form-urlencoded"); 
		client.print(var); 
	}
  else
  {
    Serial.println("Connection unsuccesful");
  } 

	if (client.connected()) { 
		client.stop();	// DISCONNECT FROM THE SERVER
	}
}

Thanks in advance

A GET command contains name=value pairs to send the data.

GET /arduino/index2.php?name1=value1&name2=value2 HTTP/1.1

What name do you want the value associated with?

		client.println("Host: 127.0.0.1");

The server is NOT running on 127.0.0.1.

Sorry folks, my mistake, the server is in my local network LAN, I change the IP in the code to 192.168.0.2, and the variable is var, look the code:

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x2A, 0x8D };
byte ip[] = { 192, 168, 0, 77 };
byte server[] = { 192, 168, 0, 2 };

EthernetClient client;

float var;

void setup() { 
	Serial.begin(115200);
  Ethernet.begin(mac, ip);

  client.connect(server,80);
  
	var = 20;
}

void loop(){

  Serial.println(client.status());

	if(client.connect(server,80))
	{
		Serial.println(client.status());
		
		client.println("GET /arduino/index2.php?var= HTTP/1.1"); 
		client.println("Host: 192.168.0.2"); 
		client.println("Content-Type: application/x-www-form-urlencoded"); 
		client.print(var); 
	}
  else
  {
    Serial.println("Connection unsuccesful");
  } 

	if (client.connected()) { 
		client.stop();	// DISCONNECT FROM THE SERVER
	}
}
		client.println("GET /arduino/index2.php?var= HTTP/1.1");
		client.println("Host: 192.168.0.2");
		client.println("Content-Type: application/x-www-form-urlencoded");
		client.print(var);

Unless your machine is hosting multiple domains (highly unlikely), the Host statement is not needed. The Content-Type statement is not usually needed.

So, you should be making a GET request like so:

		client.print("GET /arduino/index2.php?var=");
		client.print(var); 
		client.println(" HTTP/1.1");

Thanks Paul for your time, but the code still not work, I've tried several things and I can not understand the problem!!!

If I put in my browser the URL: http://127.0.0.1/arduino/index2.php?var=0 the PHP code work fine, the only problem that I think it is in the code or in the arduino itself, what do you think?

Someone can test my code??

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x2A, 0x8D };
byte ip[] = { 192, 168, 0, 77 };
byte server[] = { 192, 168, 0, 2 };

EthernetClient client;

float var;

void setup() {
Serial.begin(115200);
Ethernet.begin(mac, ip);

client.connect(server,80);

var = 20;
}

void loop(){

Serial.println(client.status());

if(client.connect(server,80))
{
Serial.println(client.status());

client.print("GET /arduino/index2.php?var=");
client.print(var);
client.println(" HTTP/1.1");

}
else
{
Serial.println("Connection unsuccesful");
}

if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
}

client.connect(server,80);

That function returns a value. Why are you throwing it away?

Why are you making GET requests if you did not connect to the server?

What do the log files on the server tell you is happening? What do your anonymous print()s tell you is happening?

I'm not throwing anything away , I do not understand what you wanted to ask

I'm using GET because it's what I intend to do , but it is not currently doing anything I can take to simplify things , but it will do any good ?

In the Apache error he says that:

[Mon Jul 18 15:03:39.329915 2016] [:error] [pid 6096:tid 1380] [client 127.0.0.1:5143] PHP Notice: Undefined index: var in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\arduino\index2.php on line 7

But I think this error is normal since the variable var is empty...

In Apache Access file he say that:

127.0.0.1 - - [18/Jul/2016:15:03:39 -0300] "GET /arduino/index2.php HTTP/1.1" 200 228

[Mon Jul 18 15:03:39.329915 2016] [:error] [pid 6096:tid 1380] [client 127.0.0.1:5143] PHP Notice: Undefined index: var in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\arduino\index2.php on line 7

You should show us this php file.

But I think this error is normal since the variable var is empty...

It is not empty when you make the GET request.

<?php if (isset($_GET['var'])) { $data = $_GET['var']; echo $data; } else { echo "Data not received"; } ?>

Try adding

       session_start();
       printf("GET variables\n");
       print_r($_GET);
       session_destroy();

after the <?php line.

It would also be useful to add code to the Arduino to actually read the server response. Perhaps a clue-by-four will present itself.

After I add the code in PHP page the message is: GET variables Array ( ) Data not received

Is it necessary to use a float data type? Try this. It will read and display the server response. That might help debug your code.

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x2A, 0x8D };
byte ip[] = { 192, 168, 0, 77 };
byte server[] = { 192, 168, 0, 2 };

EthernetClient client;

int var = 20;

void setup() {
  Serial.begin(115200);
  Ethernet.begin(mac, ip);
}

void loop() {

  Serial.println(client.status());

  if(client.connect(server, 80) == 1)
  {
    client.print("GET /arduino/index2.php?var=");
    client.print(var);
    client.println(" HTTP/1.1");
    client.println("Host: 192.168.0.77");
    client.println("Connection: close\r\n"); // double CR/LF here

// read the server response
    while(client.connected())
    {
      while(client.available())
      {
        Serial.write(client.read());  
      }
    }

    Serial.println("\r\ndisconnected");
    client.stop();
  }
  else
  {
    Serial.println("Connection unsuccesful");
  }

  delay(5000);
}

Hi SurferTim,

I replace my code with yours and in the serial monitor from arduino the message is:

0
Connection unsuccesful

I mantain the PHP code in the server side:

<?php session_start(); printf("GET variables\n"); print_r($_GET); session_destroy(); if (isset($_GET['var'])) { $data = $_GET['var']; echo $data; } else { echo "Data not received"; } ?>

I mantain the PHP code in the server side

What you are doing on the server side does not matter when you can't connect to the server.

What OS are you running on the server? Is it's address really 192.168.0.2?

You are running Apache, right? Can you access the server from another PC in the same local network?

Can you ping the Arduino from the server?

What PaulS said. He is correct and asked the right questions.

edit: Only one more thing. The most common reason for a connection fail to a server (if the IP is correct) is the server's firewall. Insure you are allowing port 80 through the firewall.

PaulS:
Unless your machine is hosting multiple domains (highly unlikely), the Host statement is not needed.

The Host header is not optional in HTTP/1.1.

RFC 7230 HTTP/1.1 Message Syntax and Routing

5.4. Host
...
A client MUST send a Host header field in all HTTP/1.1 request messages.
...
A server MUST respond with a 400 (Bad Request) status code to any
HTTP/1.1 request message that lacks a Host header field and to any
request message that contains more than one Host header field or a
Host header field with an invalid field-value.