Working Twitter App to read latest tweets

Part 2 of App:

String readtwitter(unsigned int maxno){
WiFiSSLClient client;
static String id[maxtweets]; // store tweet id's

char c;
String retval = "0";
boolean firstline = true;
String sentence = "";
int S, E;
unsigned int tweet = 0;

digitalWrite(led, HIGH);
lcd.setCursor(9,3); lcd.print(F(" *Reading* "));
Serial.print(F("\nConnecting to ")); Serial.println(LIB_DOMAIN);
  if (client.connect(LIB_DOMAIN, 443) ) {
  Serial.println(F("Connected\n"));
  // Make a HTTP request:
   //client.println("GET /search?q=" + twittername + "HTTP/1.1");
  client.println("GET /" + twittername + " HTTP/1.1");
  client.println("Host: mobile.twitter.com");
  client.println("Connection: close");
  client.println();
  client.flush();
    while (client.connected()) {
      while (client.available()){
      c = client.read();
      sentence = sentence + c;
        if (c == 10){
          if(firstline){
          S = sentence.indexOf(" ");
          E = sentence.indexOf(" ", S + 1);
          retval = sentence.substring(S + 1, E);
          firstline = false;
          }
          // print everything 
          if (maxno == 0) Serial.print(sentence); else S = sentence.indexOf("\"tweet-text\"");  
         
          if ( (tweet < maxno) && (  S >= 0)  ) {
          S = sentence.indexOf("\"", S + 19);
          E = sentence.indexOf("\"", S+1);
          String sid = sentence.substring(S+1, E); // store tweet id as string
          sentence="";
          
            // test sid against id[tweet] to find out if latest tweet has changed
            if(bailout){ // if the tweet id is the same, stop processing   and get out quick
              if(sid == id[tweet]) {
                while(client.connected()) { // discard rest of html
                  if(client.available())  c =  client.read();
                }
              client.stop();
              Serial.println(F("No change in latest tweet"));
              lcd.setCursor(9,3); lcd.print(F(" *NoChange*"));
               digitalWrite(led, LOW);
              return "0";
              }
            }
          id[tweet] = sid;

            while(client.connected() ) {
              if (client.available() ) {
              c = client.read();

                if (c == 10){
                sentence = sentence + c;
                break;
                }
                // reject rogue characters
                if (sentence.length() < maxSlength){ // avoid sentence length failure!
                byte test = c; // had to do this to test for £ (163) and other characters
                  if ( test == 163 || ( test > 31 && test < 127 ) ) {
                  sentence += c; continue;
                  }

                  // Limited French letter conversion:
                  // lcd does not like french e acute, etc  
                  if  (test == 160 || test == 162){
                  sentence += "a"; continue;
                  }
                  if (test == 167) {
                  sentence += "c"; continue;
                  }
                  if  (test >167 && test < 172) {
                  sentence += "e";
                  }
                  //if (test == 153) sentence += "`";   // ` causes more problems than solutions
                }   
              }
            }
          S = sentence.indexOf(">");
          sentence = sentence.substring(S+3); // miss 2 characters after '>' : +3
                 
            if (nolink){ 
            // replace the links, leaving prompts
            S = sentence.indexOf("<a h"); // find link start
              while(S >= 0){
              E = sentence.indexOf(">", S + 6); // look for link end
                if (E == -1){ // not found so sentence truncated
                sentence.remove(S+1); // rest of line
                sentence = sentence + ">\n";
                } else sentence.remove(S+1, E- S); // delete link leaving "<"
              S = sentence.indexOf("<a href="); // look for the next link
              }
            sentence.replace("</a", ""); // amend end of link leaving ">"
            content[tweet] = sentence; // store the tweet for processing
            }else{ // add /n to print each link on a newline
            sentence.replace("<a h", "\n<a h");
            sentence.replace("</a>", "</a>\n");
            }
            if(debug) {
            Serial.print(tweet + 1); Serial.print(" "); Serial.println(id[tweet]);
            Serial.println( sentence );Serial.flush();
            }
          lastTweet = tweet;
          tweet ++;
          }
        sentence = "";
        }
      }
    }
  client.stop();
  // any sentence with length here, probably came from a remote client shut down
    if(debug && sentence.length()) Serial.println(sentence);
    
    if (nolink){
      for(int i = 0; i <= lastTweet; i++){
      sentence = content[i];
        // check for un-terminated message
        //if (sentence.charAt(sentence.length() - 1)  != 10)  sentence = sentence + "\n";
      // swap encoded characters
      sentence.replace("&#38;#38;#10;", "\n"); // linefeed
      sentence.replace("&#38;#38;#39;", "\'");  // '
      sentence.replace("&amp;", "&"); // &
      sentence.replace("&quot;", "\""); // "
      sentence.replace("  ", " "); // multi spaces
      content[i] = sentence; // store tweets in global array
      }
    }
  Serial.println(F("Finished"));  
  lcd.setCursor(9,3); lcd.print(F(" *Finished*"));  
  } else {
  Serial.println(F("Connection failed"));
  lcd.setCursor(9,3); lcd.print(F("  *Failed* "));
  }
digitalWrite(led, LOW);
return retval;
}

void lcdscroll(String message, byte lineno, boolean clearline){
int L = message.length();
  if (clearline && (L < 20) ){
  lcd.setCursor(0, lineno); lcd.print(clearit);
  }
lcd.setCursor(0, lineno); lcd.print(message.substring(0, 20) );
mydelay(1000);
  for (int i = 0; i <  L - 19; i++) {
  lcd.setCursor(0, lineno); lcd.print(clearit);
  mydelay(65); // dark time
  lcd.setCursor(0, lineno); lcd.print(message.substring(i, i + 20) );
  mydelay(chardelay); // bright time
  }
mydelay(1500);
}

void checkbutton() {
  if (digitalRead(button) == LOW){ // backlight control
  lcdlight = !lcdlight;  
    if(lcdlight) lcd.setBacklight(BACKLIGHT_ON); else lcd.setBacklight(BACKLIGHT_OFF);
    while (digitalRead(button) == LOW){
    delay(1);
    }
  }
}

void mydelay(unsigned long elapsed){
unsigned long tt = millis();
  while ( (millis() - tt) < elapsed) checkbutton();
}

void  wifiConnect(){  // returns if wifi connected, with up to 3 attempts
lcd.setCursor(0,1); lcd.print(F("Connecting"));
// check for the presence of the shield:
mystatus = WiFi.status();
  if (mystatus == WL_NO_SHIELD) {
  Serial.println(F("WiFi shield not present"));
  }else{
  // attempt to connect to Wifi network:
  byte attempts = 0;  
    while ( attempts < 3) {
    attempts++;
    Serial.print(F("Attempting to connect to SSID: ")); Serial.println(ssid);
    mystatus = WiFi.begin(ssid);
    unsigned long tt = millis();
      while ((millis()-tt) < 10000){ // up to 10 second wait
      delay(1000);
      mystatus = WiFi.status();
        if ((mystatus == WL_CONNECTED) || (mystatus == WL_NO_SHIELD)) break;
      }
      if ((mystatus == WL_CONNECTED) || (mystatus == WL_NO_SHIELD)) break;
    }
  }
lcd.setCursor(0,1);
  if (mystatus != WL_CONNECTED){
  Serial.println(F("Wifi not available\n"));
  lcd.print(F("Wifi not available"));
    while(1) yield();
  } else{
  lcd.print(F("WiFi Connected"));
  Serial.println(F("Wifi Connected\n"));
  }
}