CC3000 client wifi shield receiving request takes too long

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
IL
 
Posts: 3
Joined: Thu Apr 17, 2014 6:25 am

CC3000 client wifi shield receiving request takes too long

Post by IL »

Hello
I am building humanoid robot with 20 servos, the robot using Arduino Mega 2560 and Adafruit CC3000 wifi shield.
My project is to move the robot servos according to commands stored on the server PC computer.
For communication testing, i have installed on the PC WampServer and written a simple php file for the server:

Code: Select all

<?php 
	echo "f";
?>
Eventually, the php would read data from txt file and sending as echo to the arduino.
But, as i was testing the communication, i had issues with the timing of communication.
The problem i have is the long time it takes me to receive one char : about 10 seconds.
I have no idea how to decrease the time taken for me to receive data from server PC.

the arduino code:

Code: Select all

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include<stdlib.h>


// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

// WiFi network (change with your settings !)
#define WLAN_SSID       "MYHOMENETWORK"        // cannot be longer than 32 characters!
#define WLAN_PASS       "MYHOMENETWORKPASSWORD"            //password
#define WLAN_SECURITY   WLAN_SEC_WPA2           // This can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2

// Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2);
                                         
// Local server IP, port, and  (change with your settings !)
  uint32_t ip = cc3000.IP2U32(10,0,0,1);        //********CHANGE IS NEEDED
  int port = 6345;

                                  
  char c;         
  Adafruit_CC3000_Client client; 
  uint16_t len;
  String start_request = "";
  String end_request = "";
  
void setup() {
 
  Serial.begin(115200);

  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }

  // Connect to  WiFi network
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY))
    {
        Serial.println(F("failed!"));
        while(1);
    }

  // Display connection details
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }

  cc3000.printIPdotsRev(ip);
  
  // Prepare request
  start_request = start_request + "\n" + "--AaB03x" + "\nSOMETHING TO SEND\n" + "Content-Transfer-Encoding: binary" + "\n" + "\n";  
  end_request = end_request + "\n" + "--AaB03x--" + "\n";

  len = start_request.length() + end_request.length();

   
}


void loop() {
  
Serial.println("\n\n******START MAIN LOOP*******\n\n");
unsigned long Time1=millis();


  Serial.println("Starting connection to server...");
  client = cc3000.connectTCP(ip, port);
  Serial.print(F("\nTime taken for connection to finnish in ms: "));
  Serial.println(millis()-Time1);  
  
  Serial.println(F("\n\n### SENDING DATA: ###\n\n"));

  unsigned long Time2=millis();   
  if (client.connected()) 
  {
      Serial.println(F("Connected !"));
        client.println(F("GET /arduino-camera-wifi/c.php HTTP/1.1"));
        client.println("Host: localhost");
        client.println("User-Agent: Arduino");
        client.println("Accept: text/html");
        client.println();
  } 
  else 
  {
      Serial.println(F("Connection failed"));    
  }
  Serial.print(F("\nTime taken to send request in ms: "));
  Serial.println(millis()-Time2);  
  
    
  Serial.println(F("\n\n### RECEIVING DATA: ###\n\n"));    
  
  unsigned long Time3=millis();
  
 while (client.connected()) 
 {
  if (client.available()) 
  {
    c=client.read();
    Serial.print(c);
   }    
 }
 
  Serial.println(F("\nTime taken to Receive data in ms: "));
  Serial.print(millis()-Time3);
  Serial.print(F("\n"));
  
  client.close();

  Serial.print(F("\n Total Time taken for main loop in ms: "));      
  Serial.println(millis()-Time1);

}



The answers:

Code: Select all

Initializing...
Started AP/SSID scan




Connecting to MYHOMENETWORK...Waiting to connect...Connected!
Request DHCP
10.0.0.1

******START MAIN LOOP*******


Starting connection to server...


Connect to 10.0.0.1:6345

Time taken for connection to finnish in ms: 9


### SENDING DATA: ###


Connected !

Time taken to send request in ms: 215


### RECEIVING DATA: ###


HTTP/1.1 200 OK
Date: Thu, 17 Apr 2014 10:37:02 GMT
Server: Apache/2.4.4 (Win64) PHP/5.4.12
X-Powered-By: PHP/5.4.12
Content-Length: 1
Content-Type: text/html

f
Time taken to Receive data in ms: 
10014

 Total Time taken for main loop in ms: 10242

As you can see, the time takes me to get answer is huge: about 10 seconds.
Can you help me to understand why it takes that long, and how can i fix it.

thank you.
Last edited by IL on Thu Apr 17, 2014 8:03 am, edited 1 time in total.

IL
 
Posts: 3
Joined: Thu Apr 17, 2014 6:25 am

Re: CC3000 client wifi shield receiving http request takes t

Post by IL »

I have changed the server to XAMPP
used the same php file.

the response time was decreased to 5.5 seconds, but it's still too long.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: CC3000 client wifi shield receiving request takes too lo

Post by adafruit_support_mike »

It looks like you're running into connection latency issues: the time it takes to establish a TCP connection with the server.

Try moving the call to 'connectTCP()' out of your main loop and use a connection with the keep-alive option. That will do all the heavy lifting once, then allow the robot to send additional requests to the server immediately.

IL
 
Posts: 3
Joined: Thu Apr 17, 2014 6:25 am

Re: CC3000 client wifi shield receiving request takes too lo

Post by IL »

Hey,
I am really a noob in this area, and i'm sorry for my questions in advance...
I was looking for some code as an example of something called "keep-alive", but i didn't find any examples of that sort.

does moving the 'connectTCP()' out of the main loop, means to move it to the 'setup()' loop?
how should i use keep-alive for the connection?

the code i have published is a fraction of the main program.
the main program is a huge state machine, each state is a different task - one is communication, one is using SD card, one is moving servos, etc...

how do i use the keep-alive option in a way that won't block the overall sequence of the state machine: only on a certain servos position i pull data from the server.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: CC3000 client wifi shield receiving request takes too lo

Post by adafruit_support_mike »

WRT the connectTCP() call, yes.. move it to your setup() routine.

The 'keep-alive' option is a feature of the HTTP connection that your code negotiates (a fancy way of saying "asks for") when it makes the connection to the server. Just do this when you make a request:

Code: Select all

  /* Try connecting to the website */
  Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
  if (www.connected()) {
    www.fastrprint(F("GET "));
    www.fastrprint(WEBPAGE);
    www.fastrprint(F(" HTTP/1.0\r\n"));
    www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\n"));
    www.fastrprint(F("Connection: keep-alive\n"));  // <-- this does the magic
    www.fastrprint(F("\n"));
    www.println();
  } else {
    Serial.println(F("Connection failed"));    
    return;
  }
There are some other issues you'll have to deal with after the basic setup.. there's a chance that the server will decide the connection has been open long enough and close it, so you'll need to add code that checks the connection and reconnects if necessary. It's also possible for the wifi connection to drop. You'll need to use the 'cc3000.checkConnected()' function to test that, and refresh that connection if necessary too.

Start with a toy program that just does the connection, requests values periodically, drops/restarts the TCP connection, and drops/restarts the wifi connection. All the basic pieces are in the 'webClient' example sketch that came with the CC3000 library, and it will be a lot easier to get used to the connection logic if you don't have a lot of other code in the way. Once you're comfortable making things work in the small version, move the ideas to the main-line code.

User avatar
steviesama
 
Posts: 21
Joined: Thu Sep 24, 2015 2:26 am

Re: CC3000 client wifi shield receiving request takes too lo

Post by steviesama »

I put my connectTCP() call in setup and used Connection: Keep-Alive in the get request, and it still takes approximate 5 seconds. It receives the data immediately, but it holds up the running of code for 5 seconds...any clues?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”