Arduino wifi shield - absurdly slow... ??? (what am I goofing up?)

I'm using the most recent Arduino wifi shield on top of an Arduino Uno. I'm using the current wifiserver to communicate with an html page on another computer via XMLHTTPRequest - the Arduino reads servo settings (from the url) and returns some sensor readings (in JSON) - this all works mostly fine and dandy.

  • My Arduino is amazingly slow. It's taking, on average 1.3seconds to reply to a wifi html request. Very little variance over 200+ tests. My code is based strongly on the example code - my code is at bottom

WIFI - start searching
Attempting to connect to SSID: TRENDnet
SSID: TRENDnet
IP Address: 192.168.1.100
signal strength (RSSI):-25 dBm
Msg:1, elapsed: 2651 ms
Msg:2, elapsed: 1323 ms
Msg:3, elapsed: 1323 ms
Msg:4, elapsed: 1324 ms

  • the wifi seems flaky. After awhile it seems to just give up the ghost. Earlier I found that I was using lots of String manipulation and possibly running out of space - changed everything to fixed char buffers and the early problems went away. Is there an upgrade to the wifi firmware I should be flashing?

My code follows...

char inBuff[80];                 // global

void vWifiActiveState() {        // Called directly from loop() when we have an active wifi connection  
  // Following code based strongly on example
  WiFiClient client = server.available();    // listen for incoming clients
  if (client) {
    int index=0;
    int lineNo=0;
    boolean weLike = false;  // only respond to messages that pertain to us (i.e., would like to ignore favicon.ico requests if possible...
    
    //  Serial.println("new client: ");    // try to reduce extru cruft to speed things up???
    
    long start = millis();
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();      // read character 
    //    Serial.print(c);
        if ( c != '\r' ) {           // ignore \r characters
          inBuff[index++] = c;       // save line in buffer
          if ( index >= 80 )         // ignore really long lines
            index=70;
          if ( c == '\n' )           // make into true string
            inBuff[index] = 0; 

          if (c == '\n' && (index <= 1)) {  // http request ends with a blank line, implies time for us to respond - always send back our readings            
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Access-Control-Allow-Origin: *");    // to allow XMLHTTPRequest       
            client.println("Connnection: close");
            client.println();
            if ( weLike )  {           
              int anaValue1 = analogRead(pinRelativeWind1);
              int anaValue2 = analogRead(pinRelativeWind2);
              int iWater1 = digitalRead(pinWater1);
              int iWater2 = digitalRead(pinWater2);
              
              client.print("{\"ArdRelativeWind1\":");
              client.print(anaValue1);
              client.print(",\"ArdRelativeWind2\":");
              client.print(anaValue2);
              client.print(",\"ArdWater1\":");
              client.print(iWater1);
              client.print(",\"ArdWater2\":");
              client.print(iWater2);
              client.println("}");
            } else {
              Serial.println("WE DO NOT LIKE!!"); 
            }  
            break;
          }
          if (c == '\n') {
            if ( lineNo == 0 )  {    // first line - this holds the GET url ...           
              char *get = strstr( inBuff, aGet);
              char *set = strstr( inBuff, aSet);             
              if ( get )  {                // just get values
                weLike = true;
              } else if ( set )  {    // read value to set servos
                 char *rudder = strstr( inBuff, aSetRudder);
                 char *sail = strstr( inBuff, aSetSail);
                 int iRudder = atoi( rudder + 7 );  // move past the 'rudder='
                 int iSail = atoi( sail + 5 );      // move past the 'sail='
                 if ( (iRudder>=0) && (iSail>=0) )  {  // dbl check for good values
                   vServosOut( iRudder, iSail );
                 }     
              }
            } else {
                 // skip line - other heading information
            }
           
            lineNo++;
            index=0;
          }
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    //  Serial.println("client disonnected");
      
    Serial.println("Msg:"+String(++iMsgCount)+",  elapsed: "+String(millis()-start)+" ms");
    iBlinkAction = !iBlinkAction; 
    digitalWrite(pinLEDAction,iBlinkAction?HIGH:LOW);  // flips every time a request is received
  }
}

Earlier I found that I was using lots of String manipulation and possibly running out of space - changed everything to fixed char buffers and the early problems went away.

    Serial.println("Msg:"+String(++iMsgCount)+",  elapsed: "+String(millis()-start)+" ms");

You have a rather strange definition of everything. Each time through the function you create and destroy two String objects, completely unnecessarily. The serial port doesn't care if you send the data using one print() call or a dozen.

strange...

You got me - :slight_smile: - though I put this line in just before submitting to get some output to show in my submission.