Miramar Beach, Florida
Offline
Faraday Member
Karma: 58
Posts: 3539
|
 |
« Reply #15 on: April 06, 2012, 05:36:46 pm » |
That was my bad.
Which code are you using now? Are you still trying your original code? Or zoomkat's?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #16 on: April 09, 2012, 02:01:54 pm » |
I haven't had a chance to work on this project for the past few days, but I tried using my original code (with a few modifications) as well as zoomkat's. Both seem to give me mixed results as far as connectivity. With zoomkat's, sometimes it connects and downloads a page immediately after I press 'e', other times it takes around 20 seconds before it says connection failed.
I originally had things set up in a for loop so that it would keep trying over and over again. This seemed to provide okay results, but also seemed like a work-around with addressing the actual problem.
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 58
Posts: 3539
|
 |
« Reply #17 on: April 09, 2012, 03:09:54 pm » |
This loads www.google.com every 10 seconds. Don't do that to Google too long. Besides, it is a big page and takes a while to display at 9600 baud. Change the server ip to yours. #include <SPI.h> #include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,2,2); IPAddress gateway(192, 168, 2, 1); IPAddress subnet(255, 255, 255, 0);
IPAddress server(74,125,227,16); // Google
EthernetClient client; int totalCount = 0; int loopCount = 0;
void setup() { Serial.begin(9600);
pinMode(4,OUTPUT); digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet); delay(2000); Serial.println("Ready"); }
void loop() { if(loopCount < 10) { delay(1000); } else { loopCount = 0;
if(!getPage(server,"/")) Serial.print("Fail "); else Serial.print("Pass "); totalCount++; Serial.println(totalCount,DEC); }
loopCount++; }
byte getPage(IPAddress ipBuf,char *page) { int inChar; char outBuf[64];
Serial.print("connecting...");
if(client.connect(ipBuf,80)) { Serial.println("connected");
sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page); client.write(outBuf); } else { Serial.println("failed"); return 0; }
while(client.connected()) { while(client.available()) { inChar = client.read(); Serial.write(inChar); } }
Serial.println();
Serial.println("disconnecting."); client.stop();
return 1; }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #18 on: April 10, 2012, 12:14:19 pm » |
Hmmm, well that seems to work so far. I've only had a few minutes this morning to play, but I haven't gotten any failed attempts (except for when I forgot to change the IP).
So what's different? Why is this one connecting fine whereas the others weren't?
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 58
Posts: 3539
|
 |
« Reply #19 on: April 10, 2012, 01:27:12 pm » |
Hmmm, well that seems to work so far. I've only had a few minutes this morning to play, but I haven't gotten any failed attempts (except for when I forgot to change the IP).
So what's different? Why is this one connecting fine whereas the others weren't?
The others were not mine.  edit: There are some reasons mine works better. I know how tcp works. If you look at the getPage() function, you will notice the "while(client.connected()) while(client.available())" loops. If you are downloading a big page (like Google's home page), you will get several packets. That loop will check client.available(), and for a while, it will be zero. Then it will be about 1400. Your goal is to empty that rx buffer as fast as you can. Get client.available() to zero. Then it may be zero again for a few checks, then it will be 1400 again (another packet). The client.connected() will return true until you have emptied the rx buffer of the final packet, so as long as client.available() returns non-zero, you are connected.
|
|
|
|
« Last Edit: April 10, 2012, 03:31:08 pm by SurferTim »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #20 on: April 10, 2012, 04:08:21 pm » |
Yeah, that's exactly what I was thinking it did...  Thanks so much! I'll keep you posted if I run into errors with this, but so far so good.
|
|
|
|
|
Logged
|
|
|
|
|
0
Online
Tesla Member
Karma: 58
Posts: 6768
Arduino rocks
|
 |
« Reply #21 on: April 10, 2012, 05:35:02 pm » |
The others were not mine. 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. void setup() { Serial.begin(9600);
// disable the SD SPI interface pinMode(4,OUTPUT); digitalWrite(4,HIGH);
// rest of your setup }
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 58
Posts: 3539
|
 |
« Reply #22 on: April 10, 2012, 09:07:46 pm » |
The others were not mine. 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. void setup() { Serial.begin(9600);
// disable the SD SPI interface pinMode(4,OUTPUT); digitalWrite(4,HIGH);
// rest of your setup }
You did not pay attention, because it does have the magic pixie dust! 
|
|
|
|
|
Logged
|
|
|
|
|
0
Online
Tesla Member
Karma: 58
Posts: 6768
Arduino rocks
|
 |
« Reply #23 on: April 10, 2012, 11:19:57 pm » |
You did not pay attention, because it does have the magic pixie dust! Oh, my bad! Just as a test, I removed the pixie dust from your code like below, and ate some salted peanuts instead. The code made 17 successful passes, so salted peanuts work too!  #include <SPI.h> #include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress server(74,125,227,16); // Google
EthernetClient client; int totalCount = 0; int loopCount = 0;
void setup() { Serial.begin(9600);
//pinMode(4,OUTPUT); //digitalWrite(4,HIGH);
Ethernet.begin(mac); delay(2000); Serial.println("Ready"); }
|
|
|
|
« Last Edit: April 10, 2012, 11:21:30 pm by zoomkat »
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 58
Posts: 3539
|
 |
« Reply #24 on: April 11, 2012, 06:17:54 am » |
I used to think that too. Here is an old thread that we both were involved in. Read reply #28. http://arduino.cc/forum/index.php/topic,75324.0.htmlI don't think salted peanuts would have helped there. The magic pixie dust did. The OP's question was why mine was working and the others weren't. This is part of the reason why. edit: If you want to read the posts where I was being you and eating salted peanuts, read replies 7,20,and 26.
|
|
|
|
« Last Edit: April 11, 2012, 06:50:02 am by SurferTim »
|
Logged
|
|
|
|
|
italy
Offline
Full Member
Karma: 2
Posts: 207
Muuuuu
|
 |
« Reply #25 on: May 15, 2012, 05:00:05 am » |
One question? How can we be sure that the program will never stop on the line while(client.connected()
If the client connection does not end we will never go out of the while loop
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 58
Posts: 3539
|
 |
« Reply #26 on: May 15, 2012, 05:50:59 am » |
One question? How can we be sure that the program will never stop on the line while(client.connected()
If the client connection does not end we will never go out of the while loop Good question. There is the possibility of a lockup there if the connection breaks. Here is the thread with the discussion and the patch for a 10 second timeout. http://arduino.cc/forum/index.php/topic,102879.msg778413.html#msg778413
|
|
|
|
|
Logged
|
|
|
|
|
italy
Offline
Full Member
Karma: 2
Posts: 207
Muuuuu
|
 |
« Reply #27 on: May 15, 2012, 05:55:25 am » |
thanks
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #28 on: May 28, 2012, 03:49:56 pm » |
This loads www.google.com every 10 seconds. Don't do that to Google too long. Besides, it is a big page and takes a while to display at 9600 baud. Change the server ip to yours. #include <SPI.h> #include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,2,2); IPAddress gateway(192, 168, 2, 1); IPAddress subnet(255, 255, 255, 0);
IPAddress server(74,125,227,16); // Google
EthernetClient client; int totalCount = 0; int loopCount = 0;
void setup() { Serial.begin(9600);
pinMode(4,OUTPUT); digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet); delay(2000); Serial.println("Ready"); }
void loop() { if(loopCount < 10) { delay(1000); } else { loopCount = 0;
if(!getPage(server,"/")) Serial.print("Fail "); else Serial.print("Pass "); totalCount++; Serial.println(totalCount,DEC); }
loopCount++; }
byte getPage(IPAddress ipBuf,char *page) { int inChar; char outBuf[64];
Serial.print("connecting...");
if(client.connect(ipBuf,80)) { Serial.println("connected");
sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page); client.write(outBuf); } else { Serial.println("failed"); return 0; }
while(client.connected()) { while(client.available()) { inChar = client.read(); Serial.write(inChar); } }
Serial.println();
Serial.println("disconnecting."); client.stop();
return 1; }
The server I'm trying to reach is in a virtual hosting environment so in addition to the ip address, I need to specify the host I want to connect to. Do you have any advice on where that would be included if one wanted to make that tweak?
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 58
Posts: 3539
|
 |
« Reply #29 on: May 28, 2012, 03:59:00 pm » |
It is sent on a line by itself after the GET, followed by a double CR-LF. 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.
|
|
|
|
« Last Edit: May 28, 2012, 04:01:48 pm by SurferTim »
|
Logged
|
|
|
|
|
|