Strange connection error

Hi all

I have two pieces of code that are almost identical. The code is parses an RSS feed (twitter, third party) for specific information. The first piece works fine, but I want to mode the client.connect section from the setup() section to the loop() section. This way I can press a button and the code connects and refreshes.

For some reason, while the first piece works, the second doesn’t. It either doesn’t connect or it connects and the client isn’t available (as indicated by a serial output). Is there any reason why this is happening? I’m super confused.

Thanks for any help

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

byte mac[] = {0x00,0xAA,0xBB,0xCC,0xDE,0x02};
byte ip[] = {192,168,1,13};

char serverName[] = "www.roomj.com";
char inString[12]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

//String location = "/~twitter/feed.php?t=2444966180-9MN4fdFLrAykMBDtu2CrQIHGxVBUo1rChxdN6bJ&s=YbAShP90K88j2vPlMB9Pqj3Hc0oI9DUFwowcL3tbc3mf9 HTTP/1.0";

EthernetClient client;

void setup() {
  Serial.begin(9600);
  Serial.println("initializing...");
  Ethernet.begin(mac, ip);
  delay(1000);
  Serial.println("connecting...");
  
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    client.println("GET http://roomj.com/twitter/feed.php?t=2444966180-9MN4fdFLrAykMBDtu2CrQIHGxVBUo1rChxdN6bJ&s=YbAShP90K88j2vPlMB9Pqj3Hc0oI9DUFwowcL3tbc3mf9 HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
    while(true);
  }
}

void loop() {
  if (client.available()) {
    char c = client.read();
    //Serial.print(c);
  
    if (c == '^' ) { //'<' is our begining character
        startRead = true; //Ready to start reading the part 
    } else if(startRead) {
      if(c != ']') { //'>' is our ending character
        inString[stringPos] = c;
        stringPos ++;
      } else {
        //got what we need here! We can disconnect now
        startRead = false;
        client.stop();
        client.flush();
        Serial.println("disconnecting.");
        Serial.println("ID Read: ");
        Serial.println(inString);
      }
    }
  
    if (!client.connected()) {
      Serial.println();
      Serial.println("disconnecting.");
      client.stop();
      while(true);
    }
  }
}

Not working

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

byte mac[] = {0x00,0xAA,0xBB,0xCC,0xDE,0x02};
byte ip[] = {192,168,1,13};

char serverName[] = "www.roomj.com";
char inString[12]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

//String location = "/~twitter/feed.php?t=2444966180-9MN4fdFLrAykMBDtu2CrQIHGxVBUo1rChxdN6bJ&s=YbAShP90K88j2vPlMB9Pqj3Hc0oI9DUFwowcL3tbc3mf9 HTTP/1.0";

EthernetClient client;

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

void loop() {
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    client.println("GET http://roomj.com/twitter/feed.php?t=2444966180-9MN4fdFLrAykMBDtu2CrQIHGxVBUo1rChxdN6bJ&s=YbAShP90K88j2vPlMB9Pqj3Hc0oI9DUFwowcL3tbc3mf9 HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
    while(true);
  }
  
  if (client.available()) {
    char c = client.read();
    //Serial.print(c);
  
    if (c == '^' ) { //'<' is our begining character
        startRead = true; //Ready to start reading the part 
    } else if(startRead) {
      if(c != ']') { //'>' is our ending character
        inString[stringPos] = c;
        stringPos ++;
      } else {
        //got what we need here! We can disconnect now
        startRead = false;
        client.stop();
        client.flush();
        Serial.println("disconnecting.");
        Serial.println("ID Read: ");
        Serial.println(inString);
      }
    }
  
    if (!client.connected()) {
      Serial.println();
      Serial.println("disconnecting.");
      client.stop();
      while(true);
    }
  }
}

Calling client.connect() on every pass through loop() might not be a good idea. Create a boolean variable. Set it to false initially. Then, call client.connect() only if the value is false. Set it to true as soon as the connection succeeds.

This way I can press a button and the code connects and refreshes.

Why aren't you doing that, then?

thanks for the reply. I was about to put in an if statement for the button press, but I thought I would move the client.connect first just to see if it would work. I know that in it's current state, it would contact the server over and over again, but I just wanted to make sure it works.

Funny thing is that it doesn't connect often, and if it does, it immediately disconnects. I have no idea what's going on. I just moved part of the code from setup to loop. This shouldn't be happening, right?

bump

I don't have the hardware to test your code but, as @PaulS says having repeated (every few microseconds) calls to connect doesn't seem like a good idea.

Put the connect code into its own function then you can call that function from setup() and using a button press.

...R