Go Down

Topic: Inconsistent HTTP Connnection (Read 13059 times) previous topic - next topic

StefanoDiMera


It is sent on a line by itself after the GET, followed by a double CR-LF.
Code: [Select]
  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");
    // Insert it here.
    sprintf(outBuf,"GET %s HTTP/1.0\r\nHost: www.mydomain.com\r\n\r\n",page);
    client.write(outBuf);
  }


edit: Insure outBuf has sufficient memory allocated to hold all that.


Thank you! Have to head to a Memorial Day event but will hopefully get to tinker with that upon return this evening!

SurferTim

This format is a bit different that you will see in examples. The reason I use this is it is easier to pass GET variables attached to the page. The sprintf function makes it easy to format a character array.

You could use the standard three client.println() calls to do that, but bear in mind each call to client.print(), client.println, or client.write() is sent in its own packet.

Code: [Select]
client.println("GET / HTTP/1.0");
client.println("Host: www.mydomain.com");
client.println();


Why send three packets? The client.write(outBuf) sends all that in one packet. Clean.

StefanoDiMera


This format is a bit different that you will see in examples. The reason I use this is it is easier to pass GET variables attached to the page. The sprintf function makes it easy to format a character array.

You could use the standard three client.println() calls to do that, but bear in mind each call to client.print(), client.println, or client.write() is sent in its own packet.

Code: [Select]
client.println("GET / HTTP/1.0");
client.println("Host: www.mydomain.com");
client.println();


Why send three packets? The client.write(outBuf) sends all that in one packet. Clean.



Ah! So you're saying that I'd use:

Code: [Select]

char outBuf[1000];
sprintf(outBuf,"GET / HTTP/1.0 Host: www.mydomain.com");
// This call sends the whole thing as one packet
client.write(outBuf);


...?

PaulS

Quote
Ah! So you're saying that I'd use:

A 1000 character buffer is using half the SRAM on a 328 based Arduino. Not likely to succeed in too many sketches. Are you really writing 1000 characters to the buffer?

Doing so won't result in a single packet, as packets are not that large.
The art of getting good answers lies in asking good questions.

zoomkat

The below might also work for a single packet.

Code: [Select]
client.println("GET / HTTP/1.0\r\nHost: www.mydomain.com\r\n");
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

StefanoDiMera

Thanks very much Zoomkat & PaulS. Both helpful comments.

It turns out that my issue was something much more elementary (and embarrassing). I reviewed SurferTim's code in Reply #17 back in April, and noticed something in his code that mine didn't have:

Code: [Select]
  Serial.println("disconnecting.");
  client.stop();


Once I added that to mine, it's worked 100% of the time.  :)

Priot to adding that, I was creating connections but never closing them, so when subsequent attempts to connect were made, they only worked once in a while (presumably after the original, successful connection had closed because it just timed out!).

Thanks guys!!

jazztime

this code is working good, but my problem is i need to use domain as server address. how can i do that

SurferTim

Insure you have a reliable dns server selected in your static IP settings, or use dhcp to get your network settings. Then use this. A return value of 1 is the only success value. Any other return value is a fail.
Code: [Select]
char server[] = "www.mydomain.com";

 if(client.connect(server,80) == 1) {

PhamDinh

I can't beleve this code is yours! It doesn't contain the magic pixie dust (below) that makes all things work!  :)  I notice the code does have a fat 2000ms delay that has been used in older code.

Code: [Select]
void setup()
{
   Serial.begin(9600);

   // disable the SD SPI interface
   pinMode(4,OUTPUT);
   digitalWrite(4,HIGH);

   // rest of your setup
}

The auto reconnection was fixed when use client.connect() in loop. Thank you SurferTim.....i can't believe too, i lost three day to find it. And ignored this.

SurferTim

You are welcome. Sometimes you just need a little magic pixie dust! :)

Go Up