Pages: [1]   Go Down
Author Topic: Arduino GET data  (Read 991 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have an example web client that can connect to a predefined website .. but how do I configure this to input variables?

ie here :

Code:
   // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET /Admin/Update.php?page=update&key=17&name=Test&temp=15&light=99 HTTP/1.1");
    client.println("Host: www.test.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }

instead of using predefined values for temp and light ... I want to use variables temp and light .. but if I just type the name there then it sends just the name and not the variable value.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
instead of using predefined values for temp and light ... I want to use variables temp and light .. but if I just type the name there then it sends just the name and not the variable value.
See those double quotes on the ends? They mean send everything from here to here AS IS. Since that's not what you want, you need to re-enroll in CS100, and learn about sprintf().
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 148
Posts: 6105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use sprintf. Yours should look something like this:
Code:
char outBuffer[100];
int myKey = 17;
char myName = "Test";
int myTemp = 15;
int myLight = 99;

sprintf(outBuffer,"GET /Admin/Update.php?page=update&key=%u&name=%s&temp=%u&light=%u HTTP/1.1",myKey,myName,myTemp,myLight);
client.println(outBuffer);
Insure outBuffer is large enough to hold all that.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19040
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    client.print ("GET /Admin/Update.php?page=update&key=17&name=");
    client.print (name);
    client.print ("&temp=");
    client.print (temp);
    client.print ("&light=99 HTTP/1.1");
Logged


Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 148
Posts: 6105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    client.print ("GET /Admin/Update.php?page=update&key=17&name=");
    client.print (name);
    client.print ("&temp=");
    client.print (temp);
    client.print ("&light=99 HTTP/1.1");
That is a five packet send. That is 48 bytes of overhead per packet. Mine is one packet. Some servers have problems with that. And that last should be a client.println().
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19040
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

His original line didn't have println, although you are probably right there. smiley

I'm offering that as an alternative if you didn't want to spend 100 bytes of RAM on a temporary buffer.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19040
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I got the variables wrong, he wanted temp and light, not name and temp, but the general idea is the same.
Logged


Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 148
Posts: 6105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Nick: I got the point. I was offering a reason to spend the 100 bytes of SRAM instead of sending a bunch of packets. Most servers do not have a problem with receiving the GET line in multiple packets, but some do, and I'm not sure which.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks that helps a lot. Should I put my client code in a seperate function and then call it each time I want to upload variables? Then I can stick all my sensor functions within the void loop with a delay at the end.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Then I can stick all my sensor functions within the void loop with a delay at the end.
No don't do that. Look at the blink without delay example.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 148
Posts: 6105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Then I can stick all my sensor functions within the void loop with a delay at the end.
No don't do that. Look at the blink without delay example.
...or look at my client code with the new "wait 30 seconds without delay()" code.  smiley-wink
http://playground.arduino.cc/Code/WebClient
The timeout feature still uses delay(1), but the loop uses millis() now.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I output just a number in php can my arduino read it directly or do I still have to use find string?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 148
Posts: 6105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I output just a number in php can my arduino read it directly or do I still have to use find string?
If you output just a number, it should be the first line after the header. Read until the blank line, and the next line should be your number.
Logged

Offline Offline
Newbie
*
Karma: 3
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi take a look at my project i think can help you

http://www.dataino.it/help/document.php
http://www.dataino.it/help/document_data.php
http://www.dataino.it/help/document_tutorial.php?id=5

Ciao

Logged

Pages: [1]   Go Up
Jump to: