Go Down

Topic: Ethernet sheild stops connecting after an extended period (Read 2 times) previous topic - next topic

StuHooper

Loe and behold, I've fucked up again.

Code: [Select]
void Ethercheck(){
//Ethernet Client
   if (client.connected()) {
    if (client.available()) {
      // read incoming bytes:
      char inChar = client.read();
     // Serial.print (inChar);

     // add incoming byte to end of line:
      currentLine[index++] = inChar;
      currentLine[index] = '\0';

      // if you get a newline, clear the line:
      if (inChar == '\n') {
        previousTweet == tweet;
      }
      // if the current line ends with <text>, it will
      // be followed by the tweet:
      if (strncmp(currentLine,"<text>")==0) {
        // tweet is beginning. Clear the tweet string:
        readingTweet = true;
      }
      // if you're currently reading the bytes of a tweet,
      // add them to the tweet String:
      if (readingTweet) {
        if (inChar != '<') {
          tweet[index++] = inChar;
        //  currentLine[index] = '\0';
         
        }
        else {
          // if you got a "<" character,
          // you've reached the end of the tweet:
          readingTweet = false;
           if (tweet != previousTweet){
          Serial.println(tweet);   
          // close the connection to the server:
          Serial.println(F("disconnecting"));
          client.stop();
               
                if (firstTweetRead != false){
                if (strncmp(tweet,"<text>")==0) {
                 
                  //if( waterLevel is higher than said amount){
                  Serial.println (F("Making coffee"));
                  tweetString.begin();
                  tweetString = " Making coffee now, random number: ";
                  randNumber = random(1000);
                  tweetString.print (randNumber);
                 
                  make();
                }

                }
                else{
                 firstTweetRead = true;
                }
                }
         else {
           Serial.println(F("no new tweets"));
           Serial.println(F("disconnecting"));
           client.stop();
         
         }
        }
      }
    }
  }
  else if (millis() - lastAttemptTime > requestInterval) {
    // if you're not connected, and fifteen seconds have passed since
    // your last connection, then attempt to connect again:
    client.flush();
    totalCount++;
    Serial.print (F("Total count:"));
    Serial.println(totalCount);
    connectToServer();
   
  }
 
}





Quote
setup
connecting to server...connected
making HTTP request...
Total count:1
connecting to server...


I'm not sure how to use the strncmp(). I thought this would be correct.
PaulS, Thanks for helping me with every stage of this project. I'd say I was teaching myself but that would be a lie.

PaulS

Code: [Select]
      if (strncmp(currentLine,"<text>")==0) {
It's hard to believe that that even compiles, since strncmp() takes three arguments. The third is the number of characters to compare.

Code: [Select]
      if (readingTweet) {
        if (inChar != '<') {
          tweet[index++] = inChar;
        //  currentLine[index] = '\0';
         
        }

You need a separate index for each array, or, at a minimum you need to reset index to 0 before you use it to index another array.

Uncomment the print that shows what was read. You can't debug serial/client reading code without seeing what you read.

StuHooper

I'm lost, This is printing jibberish.
Code: [Select]
void Ethercheck(){
//Ethernet Client
   if (client.connected()) {
    if (client.available()) {
      // read incoming bytes:
      char inChar = client.read();
      //Serial.print (inChar);

     // add incoming byte to end of line:
      currentLine[currentIndex++] = inChar;
      currentLine[currentIndex] = '\0';
      Serial.print (currentLine);
       
      // if you get a newline, clear the line:
      if (inChar == '\n') {
        previousTweet == tweet;

      }
      // if the current line contains <text>, it will
      // be followed by the tweet:
      if (strncmp(currentLine,"<text>",6)==0) {
        // tweet is beginning. Clear the tweet string:
        readingTweet = true;
      }
      // if you're currently reading the bytes of a tweet,
      // add them to the tweet String:
      if (readingTweet) {
        if (inChar != '<') {
          tweet[tweetIndex++] = inChar;
          tweet[tweetIndex] = '\0';
         
        }
        else {
          // if you got a "<" character,
          // you've reached the end of the tweet:
          readingTweet = false;
         
           if (tweet != previousTweet){
            Serial.println(F("TWEET"));
            Serial.println(tweet);   
            // close the connection to the server:
            Serial.println(F("disconnecting"));
            client.stop();
               
                if (firstTweetRead != false){
                if (strncmp(tweet,makeCoffee, 16)==0) {
                 
                  //if( waterLevel is higher than said amount){
                  Serial.println (F("Making coffee"));
                  tweetString.begin();
                  tweetString = " Making coffee now, random number: ";
                  randNumber = random(1000);
                  tweetString.print (randNumber);
                 
                  make();
                }

                }
                else{
                 firstTweetRead = true;
                }
                }
         else {
           Serial.println(F("no new tweets"));
           Serial.println(F("disconnecting"));
           client.stop();
         
         }
        }
      }
    }
  }
  else if (millis() - lastAttemptTime > requestInterval) {
    // if you're not connected, and fifteen seconds have passed since
    // your last connection, then attempt to connect again:
    client.flush();
    totalCount++;
    Serial.print (F("Total count:"));
    Serial.println(totalCount);
    connectToServer();
   
  }
 
}




hissadshadow

i have same problem,
if you connectinc using DHCP , connection drops after 60 minuts
its easily fix problem
You need to use Ethernet.maintain() every 15-30 minuts


Code: [Select]

unsigned long previousMillis = 0;
int interval = 900000;

void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    Ethernet.maintain();
    delay(100);
  }
}


PaulS

Code: [Select]
      if (inChar == '\n') {
        previousTweet == tweet;

No. An equality operator is NOT what you want to use. You can't assign (=) a char array this way, either. You need strcpy().

At some point, you need to set index and tweetIndex back to 0, and put the NULL in position 0 in each array.

You also need to stop posting snippets. How are we supposed to know how previousTweet, tweet, currentLine, currentIndex, and tweetIndex are defined?

Go Up