Go Down

Topic: Arduino GET data (Read 1 time) previous topic - next topic

Carlo060

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: [Select]
   // 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.

PaulS

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().

SurferTim

I use sprintf. Yours should look something like this:
Code: [Select]
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.

Nick Gammon

Code: [Select]

    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");
http://www.gammon.com.au/electronics

SurferTim


Code: [Select]

    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().

Nick Gammon

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

I'm offering that as an alternative if you didn't want to spend 100 bytes of RAM on a temporary buffer.
http://www.gammon.com.au/electronics

Nick Gammon

I got the variables wrong, he wanted temp and light, not name and temp, but the general idea is the same.
http://www.gammon.com.au/electronics

SurferTim

@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.

Carlo060

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.

PaulS

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.

SurferTim


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.  ;)
http://playground.arduino.cc/Code/WebClient
The timeout feature still uses delay(1), but the loop uses millis() now.

Carlo060

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

SurferTim


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.


Go Up