ethernet shield connects then disconnects... need help asap

so i am working on a weather data streaming from the website weather.gov to get temp and humidity reading then display it on my arduino mega 2560 tft 3.2 lcd screen but its not working out to good...

here is the code i been trying to run and at first its connecting at the void setup then when it gets to the void loop, it just disconnects for no reason

take a look at the code... i hope some one can help me with this issue... all wires are connected and not loose...

#include <UTFT.h>  // used to interface with the TFT display
#include <UTouch.h>  // used to interface with the touch controller on the TFT display
#include <avr/pgmspace.h>
#include <tinyFAT.h> // used to acess the SD card
#include <UTFT_tinyFAT.h>  // used to read .raw images from the SD card
#include <SD.h>    //Used to check for SD card
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>



//LCD TOUCH PANEL and ITDB02 MEGA SHIELD v1.1
//(Mega Shield utilizes pins 5V, 3V3, GND, 2-6, 20-41, & (50-53 for SD Card))
UTFT myGLCD(SSD1289,38,39,40,41);      //Uncomment this line for the SSD1289 TFT Screen
UTouch myTouch (6,5,4,3,2);      //Pins Used for the Touch screen
UTFT_tinyFAT myFiles(&myGLCD);  // start up an instance to read images from the SD card



byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,115);      
//IPAddress server(204,227,127,201); // weather.gov
char server[] = "weather.gov";    // name address for weather.gov

EthernetClient client;
TextFinder xml(client);



//Declare which fonts to be utilized
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];


#define LARGE true
#define SMALL false


void setFont(boolean font, byte cr, byte cg, byte cb, byte br, byte bg, byte bb)
{
  myGLCD.setBackColor(br, bg, bb);               //font background black
  myGLCD.setColor(cr, cg, cb);                   //font color white
  if (font==LARGE)
    myGLCD.setFont(BigFont);                     //font size LARGE
  else if (font==SMALL)
    myGLCD.setFont(SmallFont);
}

void setup() {
  Serial.begin(9600);
  SD.begin();
  //Serial.println("FF Weather Station");
  //Serial.println("connecting...");
  myGLCD.InitLCD(LANDSCAPE);
  myGLCD.clrScr();
  
  myTouch.InitTouch(LANDSCAPE);
  myTouch.setPrecision(PREC_MEDIUM);
  
  file.setSSpin(53);                  // init SD card
  file.initFAT(SPISPEED_VERYHIGH);
  
  
  
  setFont(SMALL, 0, 0, 255, 0, 0, 0);
  myGLCD.print("FF Weather Stn", 1, 1);
  myGLCD.print("connecting...", 1, 13);
  Serial.println("connecting...");
  
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  delay(1000);
  
  if (client.connect(server, 80)) {
    myGLCD.clrScr();
    setFont(SMALL, 0, 255, 0, 0, 0, 0);
    myGLCD.print("Connected!", 1, 1);
    Serial.println("Connected!");
    client.println("GET /MapClick.php?lat=32.715328559000454&lon=-117.1572599319997&site=all&smap=1#.VJRtoXlgY HTTP/1.1"); //Make http request
    client.println("Host: weather.gov");
    client.println();
  } 
  else {
    myGLCD.clrScr();
    setFont(SMALL, 255, 0, 0, 0, 0, 0);
    myGLCD.print("connection failed", 1, 1);
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  
    if(xml.find("Humidity</span>")){ 
         byte test = xml.getValue();
         myGLCD.clrScr();
         setFont(SMALL, 0, 255, 0, 0, 0, 0);
         myGLCD.print("testing: ", 1, 1);  
         myGLCD.printNumI(test, 48, 1);
    }
    setFont(SMALL, 0, 0, 255, 0, 0, 0);
    myGLCD.print("looking for data again in 5 sec", 1, 13);
    Serial.println("looking for data again in 5 sec");
    delay(5000); // check again in 5 seconds
  }
  else {
    Serial.println();
    Serial.println("not connected");
    delay(1000); 
  }
}

does anyone have an answer to this?

Can you put Serial. print statement after each line. Then share me screen shot so we can know where probelm exactly.

A couple things I see.

First, this is no longer correct if using a domain name for a server. The success return is 1. The fail can also return a negative number if the dns resolution failed, which also evaluates to true.

// change this
  if (client.connect(server, 80)) {
// to this
  if (client.connect(server, 80) == 1) {

Second is this. D4 is used by the SD card on the ethernet shield.

UTouch myTouch (6,5,4,3,2);      //Pins Used for the Touch screen

take a look at the code

Are you using a Mega? If not, I'd say there is a 99.99% certainty that you are running out of memory.

Detach the LCD/touch screen. Dump the SD stuff. Write a SIMPLE sketch that connects, gets some data, and disconnects. See if you are having a code problem with the simple sketch, a connectivity problem, or if the problem goes away.

SurferTim:
Second is this. D4 is used by the SD card on the ethernet shield.

UTouch myTouch (6,5,4,3,2);      //Pins Used for the Touch screen

this is true but i didnt plug in pin 4 for the sd card on the ethernet shield as it wasnt being used...

@PaulS - i'm running a arduino mega 2560 so memory is not the issue nor is power the issue. i have tryed running simple pre loaded sketch in the ethernet lib called "webServer" which runs fine with no disconnection or issues and it sends the data to a ip address and refresh it every few secs and it never disconnects and runs fine...

i also tryed running the "webClient" which use the google.com to search for the keyword "arduino" (also located in the ethernet lib examples) and it runs fine, it opens and shows all the data no problem...

i tryed running the "webclient Repeating" and i had issues with this... it wont even connect at all....

here is the Serial print screen shoot of what i am taking about... it seems as if it runs though the loop once then when it trys to run it again after the first time, it just stops connecting...

another issue was some times the ethernet shield connects on start up and some times it doesnt... is this normal? just though i had to ask this too... but majority of the time it runs... 90% run and 10% chance it doesnt on start up or power up

The only time your sketch connects is one time in setup. From then on, the loop section will show "not connected". That is exactly what your sketch is programmed to do.

What do you mean you didn't plug in D4? Do you have a SD card in the shield's slot? If so, it is being used, and you can't use D4 for anything else, like the touch screen.

edit: Did you change the client.connect() return value evaluation? If not, it may be returning a negative number (dns resolution failed), but your sketch will think the connection was successful.

will the lcd shield has a sd card slot and that is what i am using it for... not sure what pin its being used for that sd card slot but my touch screen does work with the lcd screen... for the ethernet shield i didnt plug in the D4 pin as its not needed...

and if your talking about changing the:

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

to

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

then yes i have just done that...

when you said :

The only time your sketch connects is one time in setup. From then on, the loop section will show "not connected". That is exactly what your sketch is programmed to do.

is this the reason why it only connects once? i need it to pull the data every few hours as it will be getting the weather forecast and i like it to keep up to date in real time... with that being said what do i need to drag down to the loop for it to keep it connected?

Does it still show you are connecting with the new evaluation? I see no response from that server on your serial monitor.

Do you have a SD card in the shield's slot. If so, remove it for a test.

Try my client code. It has the correct evaluation and a bunch of other error checking and fault tolerance stuff.
http://playground.arduino.cc/Code/WebClient

SurferTim:
Does it still show you are connecting with the new evaluation? I see no response from that server on your serial monitor.

Do you have a SD card in the shield's slot. If so, remove it for a test.

Try my client code. It has the correct evaluation and a bunch of other error checking and fault tolerance stuff.
Arduino Playground - HomePage

not sure what or how to check if its connecting with the new evalustion. but the serial print are the same? lol

so i took a look at that link and i used the "dhcp for the ip and dns for the server" code and it loads and this is what i get off the Serial.Print

check the txt i attached
and it keeps on going to like pass 7+

test.txt (9.74 KB)

I got it to work with my playground code, but the page is way too big to do anything with. It takes over a minute to display the page on the serial monitor, even at 115200 baud.

What is your goal after downloading that page?

well what i was trying to aim for is to get the xml off the weather.gov for area code 92115 then look for the humidity% value and display that on to the lcd screen or serial print for testing...

i think the links in my sketch might be wrong also lol not sure if i got the url right so that might need to be double checked also

also when i change the:

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

to

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

it doesnt connect with the ethernet and gives me a "connection failed" but when i remove the "== 1" it works

If the evaluation fails using "== 1", but works without it, then the dns resolution is failing.

Here is the code for your location. The baud rate is 115200 or it won't keep up.

/*
   Web client sketch for IDE v1.0.1 and w5100/w5200
   Uses GET method.
   Posted October 2012 by SurferTim
   Last modified September 15, 2013
*/

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

// this must be unique
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your server
char server[] = "forecast.weather.gov";

//Change to your domain name for virtual servers
char serverName[] = "forecast.weather.gov";

// change to your server's port
int serverPort = 80;

EthernetClient client;
int totalCount = 0;
char pageAdd[128];

// set this to the number of milliseconds delay
// this is 60 seconds
#define delayMillis 60000UL

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(115200);

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

  // Start ethernet
  Serial.print(F("Starting ethernet..."));

  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else Serial.println(F("ok"));

  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));
}

void loop()
{
  Ethernet.maintain();

  thisMillis = millis();

  if(thisMillis - lastMillis > delayMillis)
  {
    lastMillis = thisMillis;

    // Modify next line to load different page
    // or pass values to server
    sprintf(pageAdd,"/MapClick.php?lat=32.75870162458136&lon=-117.07362823145701&site=all&smap=1#.VJWsyCeBm0",totalCount);

    // sprintf(pageAdd,"/arduino.php?test=%u",totalCount);

    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    totalCount++;
    Serial.println(totalCount,DEC);
  }    
}

byte getPage(char *ipBuf,int thisPort, char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,thisPort) == 1)
  {
    Serial.println(F("connected"));

    sprintf(outBuf,"GET %s HTTP/1.1",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",serverName);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}

ok so when i upload this sketch it works but displays in serial the xml... is there a way to just make it so that it prints the humidity?

like say

using the textfinder.h

TextFinder xml(client);

then :

if(xml.find("Humidity</span>")){  // then print in serial}

I haven't tried it, but it should work. Here is the playground page on TextFinder.
http://playground.arduino.cc/Code/TextFinder

Below is some weather code from a long time back that has been tweaked some for current libraries and weather site.

//////////////////////////////////////////////
// Get XML formatted data from the web.
// 1/6/08 Bob S. - Created
//
//  Assumptions: single XML line looks like: 
//    <tag>data</tag> or <tag>data 
//
//////////////////////////////////////////////

// Include description files for other libraries used (if any)
#include <SPI.h>
#include <Ethernet.h>

// Define Constants
// Max string length may have to be adjusted depending on data to be extracted
#define MAX_STRING_LEN  20

// Setup vars
char tagStr[MAX_STRING_LEN] = "";
char dataStr[MAX_STRING_LEN] = "";
char tmpStr[MAX_STRING_LEN] = "";
char endTag[3] = {'<', '/', '\0'};
int len;

// Flags to differentiate XML tags from document elements (ie. data)
boolean tagFlag = false;
boolean dataFlag = false;

// Ethernet vars
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
byte server[] = { 140, 90, 113, 200 }; // www.weather.gov

// Start ethernet client
EthernetClient client;

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting WebWx");
  Serial.println("connecting...");
  Ethernet.begin(mac, ip);
  delay(1000);

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");    
    client.println();
    delay(2000);
  } else {
    Serial.println("connection failed");
  }  
}

void loop() {

  // Read serial data in from web:
  while (client.available()) {
    serialEvent();
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("Disconnected");
    Serial.println("==================================");
    Serial.println("");
    client.stop();

    // Time until next update
    //Serial.println("Waiting");
    for (int t = 1; t <= 15; t++) {
      delay(60000); // 1 minute
    }

    if (client.connect(server, 80)) {
      //Serial.println("Reconnected");
      client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");    
      client.println();
      delay(2000);
    } else {
      Serial.println("Reconnect failed");
    }      
  }
}

// Process each char from web
void serialEvent() {

   // Read a char
	 char inChar = client.read();
   //Serial.print(".");
  
   if (inChar == '<') {
      addChar(inChar, tmpStr);
      tagFlag = true;
      dataFlag = false;

   } else if (inChar == '>') {
      addChar(inChar, tmpStr);

      if (tagFlag) {      
         strncpy(tagStr, tmpStr, strlen(tmpStr)+1);
      }

      // Clear tmp
      clearStr(tmpStr);

      tagFlag = false;
      dataFlag = true;      
      
   } else if (inChar != 10) {
      if (tagFlag) {
         // Add tag char to string
         addChar(inChar, tmpStr);

         // Check for </XML> end tag, ignore it
         if ( tagFlag && strcmp(tmpStr, endTag) == 0 ) {
            clearStr(tmpStr);
            tagFlag = false;
            dataFlag = false;
         }
      }
      
      if (dataFlag) {
         // Add data char to string
         addChar(inChar, dataStr);
      }
   }  
  
   // If a LF, process the line
   if (inChar == 10 ) {

/*
      Serial.print("tagStr: ");
      Serial.println(tagStr);
      Serial.print("dataStr: ");
      Serial.println(dataStr);
*/

      // Find specific tags and print data
      if (matchTag("<temp_f>")) {
	      Serial.print("Temp: ");
         Serial.print(dataStr);
      }
      if (matchTag("<relative_humidity>")) {
	      Serial.print(", Humidity: ");
         Serial.print(dataStr);
      }
      if (matchTag("<pressure_in>")) {
	      Serial.print(", Pressure: ");
         Serial.print(dataStr);
         Serial.println("");
      }

      // Clear all strings
      clearStr(tmpStr);
      clearStr(tagStr);
      clearStr(dataStr);

      // Clear Flags
      tagFlag = false;
      dataFlag = false;
   }
}

/////////////////////
// Other Functions //
/////////////////////

// Function to clear a string
void clearStr (char* str) {
   int len = strlen(str);
   for (int c = 0; c < len; c++) {
      str[c] = 0;
   }
}

//Function to add a char to a string and check its length
void addChar (char ch, char* str) {
   char *tagMsg  = "<TRUNCATED_TAG>";
   char *dataMsg = "-TRUNCATED_DATA-";

   // Check the max size of the string to make sure it doesn't grow too
   // big.  If string is beyond MAX_STRING_LEN assume it is unimportant
   // and replace it with a warning message.
   if (strlen(str) > MAX_STRING_LEN - 2) {
      if (tagFlag) {
         clearStr(tagStr);
         strcpy(tagStr,tagMsg);
      }
      if (dataFlag) {
         clearStr(dataStr);
         strcpy(dataStr,dataMsg);
      }

      // Clear the temp buffer and flags to stop current processing
      clearStr(tmpStr);
      tagFlag = false;
      dataFlag = false;

   } else {
      // Add char to string
      str[strlen(str)] = ch;
   }
}

// Function to check the current tag for a specific string
boolean matchTag (char* searchTag) {
   if ( strcmp(tagStr, searchTag) == 0 ) {
      return true;
   } else {
      return false;
   }
}

yea thats where i found the text finder but i'm not sure how to add it into your sketch u posted to get that list of xml...

also what does sprintf pageadd do? never seen it before... cant you use client.println("GET /MapClick.php?lat=32.75870162458136&lon=-117.07362823145701&site=all&smap=1#.VJWsyCeBm0 HTTP/1.1"); or will it not end up the same?

You can use that. I use sprintf because I usually send variable values that change with each request. If yours doesn't change, you are good to go.

I don't use the TextFinder, so I don't know how to set that up in my code.

SurferTim:
You can use that. I use sprintf because I usually send variable values that change with each request. If yours doesn't change, you are good to go.

I don't use the TextFinder, so I don't know how to set that up in my code.

the value will change i assume though out the day so in that cause i should just leave it alone then? i mean its not something that will change ever sec but every 5hrs or so...?

the value will change i assume though out the day so in that cause i should just leave it alone then? i mean its not something that will change ever sec but every 5hrs or so...?

How often the value changes is not relevant. It changes. You need to deal with the fact that it is not constant.