Arduino ethernet - send data to server with GET/POST php

Hey guys, I'm having a little bit of trouble here... Here's my situation:

I have a web server running on a Beaglebone Black, and I'm trying to send data from the arduino to its webpage so it can, eventually, be stored in a mySQL database.

I am able to connect to the server with my arduino (based off an example I took on the forum that worked for many), but it doesn't seem like the data I'm sending is going through...

Here's my code - arduino side:

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

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

byte server[] = { 192, 168, 2, 83  }; // Server IP

int data = 1022;

void setup()
{

Serial.begin(9600);               // Used for serial debugging

}
void loop()
{

Serial.println("Program running...");

 delay(5000);
 senddata();                                 // Data is sent every 5 seconds

}
void senddata()
{
Ethernet.begin(mac, ip, gw, subnet);
EthernetClient client;//(server, 80);
Serial.println();
Serial.println("ATE :)");
delay(1000);                                    //Keeps the connection from freezing

if (client.connect(server, 80)) {
Serial.println("Connected");
client.print("GET /insertdata.php?data=");
client.print(data);
client.println(" HTTP/1.1");
client.println("Host: 192.168.2.83");
client.println();
Serial.println();
                      }

else
{
Serial.println("Connection unsuccesful");
}
//}
 //stop client
 client.stop();
 while(client.status() != 0)
{
  delay(5);
}
}

And here's the code - server side - php

<!DOCTYPE html>
<html>

<?php
echo "Access OK";
echo "
"; //newline

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

//Connect to database

?> 

</html>

At first I tried simply echoing the $data, without the if condition, but I wasn't getting anything so I looked up online and found that this was a common thing to do. Now, still not getting the right thing, as it is just displaying the "Data not received", which means it didn't get anything from the $_GET statement...

Any help is extremely appreciated, hoping to get this fixed fairly soon :slight_smile:

First, this is not correct. you left out the dns server.

// change this
Ethernet.begin(mac, ip, gw, subnet);
// to this
Ethernet.begin(mac, ip, gw, gw, subnet);

Second, that call should be in setup, not in senddata.

Third, you are not reading the response and waiting for the server to close the connection.

client.println("Host: 192.168.2.83");

// add this
client.println("Connection: close");

client.println();
Serial.println();

// and add this
while(client.connected()) {
  while(client.available()) {
    Serial.write(client.read));
  }
}

else

First off, I'd like to thank you for your answer :slight_smile:
I totally forgot about the dns, as you pointed out... even though I had already previously checked for the arguments passed to the function, completely forgot to add it. As I said, I just took an example used frequently on the forum.

But now, I'm getting the following error regarding Serial.write:

Arduino: 1.6.1 (Windows 8.1), Board: "Arduino Uno"

data_send_webphp.ino: In function 'void senddata()': 
data_send_webphp.ino:51:29: error: no matching function for call to 'HardwareSerial::write(<unresolved overloaded function type>)'

It seems like it doesn't recognize the Serial.write, even though it's standard :confused:

Any idea as to why? or how it could be fixed?
Thanks again :smiley:

My bad. I forgot a parenthesis. This should compile and run ok.

    Serial.write(client.read());

Wow haha, even I should have spotted this one, thanks!
It's not compiling, and I uploaded it to my arduino, but I still don't see it on my server's webpage.

Here's what I'm getting from the serial monitor:

Program running...

ATE :)
Connected

HTTP/1.1 200 OK
X-Powered-By: PHP/5.4.36-0+deb7u3
Content-type: text/html
Connection: close
Transfer-Encoding: chunked
Date: Sun, 15 Mar 2015 22:03:44 GMT
Server: lighttpd/1.4.31

3c
<!DOCTYPE html>
<html>

Access OK
1022
 

</html>
0

Which means the data seems to be going through correctly? Problem might be on the server side now, my php code...
Just to be sure, what would my url be if I wanted to see the value I GET from data?

:slight_smile:

Try my web client code with your server. It is designed specifically to handle rude and malfunctioning servers.
http://playground.arduino.cc/Code/WebClient
The first is a GET method. That is the one you want.

In the loop function, you should change the page request to yours, like this:

//change this
    sprintf(pageAdd,"/",totalCount);
// to this
    sprintf(pageAdd,"/insertdata.php?data=%u",totalCount);

Let me know how it does.

edit: Yours is working. The 1022 is the data value you sent.

Access OK
1022
Pass 9
connecting...connected
HTTP/1.1 200 OK
X-Powered-By: PHP/5.4.36-0+deb7u3
Content-type: text/html
Connection: close
Transfer-Encoding: chunked
Date: Sun, 15 Mar 2015 22:35:59 GMT
Server: lighttpd/1.4.31

39
<!DOCTYPE html>
<html>

Access OK
9
 

</html>
0


disconnecting.

This is what I get on the serial monitor with your program.

And regarding my program, it does seem to be working, but I can't seem to retrieve the data with my php code, perhaps I'm not trying to access the right url? Or maybe there's something wrong with my php code?

What are you talking about? It IS working! You sent the parameter data=9, and here it is:

Access OK
9

I don't recall saying it doesn't work, both my program and yours work.

My problem seems to be GETTING what I sent on the php side :frowning:

Your php page is getting what you sent by your code and mine. You are not doing anything with it except displaying it on the page. What are you expecting it to do?

I know, it's only supposed to display it on the page, but it's not...
As I previously mentioned, it's currently displaying "Data not received", which means it wasn't able to GET anything from data :frowning:

I've tried accessing my data through both following urls:
http://192.168.2.83/insertdata.php
and also
http://192.168.2.83/insertdata.php?data=

But data isn't displayed in neither of them...
Thanks again for the help, and sorry if I'm confused and/or confusing you ^^

I think you are confused about what that page will do. Once it gets your data value and displays it on the page, it forgets all about it unless you save it in a database.

No, that is exactly what I want to do ^^ (or at least for now)

The only reason why I'm echoing the data is so I can see that the "transfer" was done, to see that I managed to send it and retrieve it. But as of now, it seems like I'm able to send it (arduino side), but not retrieve it (webpage - php side).

Eventually, when I'll be able to retrieve it and display it, I'll store the data in a database.

Your request sent data=1022 and here is is on the php side:
Access OK
1022

My request sent data=9 and here it is on the php side:
Access OK
9

Yes, I can see it on the monitor that it has worked, but my question is why can't I see it when I'm looking at my webpage on 192.168.2.83/insertdata.php ?

You certainly have a problem understanding this. You send a request, it displays the value and forgets it. You send another request, and it displays that value, then forgets it. Nothing is ever saved. When you send another request, it doesn't remember anything about previous sends. NOTHING IS SAVED.

You're right, I haven't worked with this before so I'm not quite sure how GET requests work...

If I understood correctly from what you said, it's not displayed on my page if I echo it, simply because the value isn't stored, so as soon as it's received it's forgotten? Therefore, in order for me to display it on my page, I'd have to save it?

I thought I could just echo the value every time it was sent&received for test purposes, and eventually save it to my database :S

Now you understand. As soon as it is received and displayed, it is forgotten. Nothing is saved unless you store it in a database. Then you can retrieve the values stored in the database.

Ohhh alright, sorry I didn't quite grasp this concept the first time ^^

So, in order for me to see if I can display the data on my webpage (by echoing its value), I'll have to store it first?

Is there any simpler/faster way to "store" it than through a mySQL database? I know it doesn't take too long to set up the connection and everything, but I don't feel like messing with it right now, I would just like to see if I can display the correct info :slight_smile:

Thanks a bunch for your help and explanation, really appreciated!
(and also fast response :wink: )

Here are a couple links to give you something to read.
http://php.net/manual/en/function.apc-store.php