CC3000 Function not establishing client connection

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tech500
 
Posts: 199
Joined: Wed Dec 04, 2013 3:53 pm

CC3000 Function not establishing client connection

Post by tech500 »

Function, int8_t init_network() appears to not be returning value/s needed to allow client connection. Inside function it appears the WLAN connection is restored; however, I can not establish client connection in the listen function. Using Serial.println statements I have isolated the stopping point in the the listen() function at if(client). Serial.println("client found") does not print. What needs to be corrected? Without the int8_t init_network; listen function work correctly.

init_network function:

Code: Select all

int8_t init_network()
    {
	
	  Adafruit_CC3000_Client  client = cc3000.connectTCP(ip, port);
	  	
      cc3000.reboot();
	  Serial.println("reconnecting to WLAN");
      // Set up the CC3000, connect to the access point, and get an IP address.
      #ifdef DEBUG
      Serial.println(F("Initializing CC3000..."));
      #endif
      
      if (!cc3000.begin())
	  Serial.begin(9600);
		  while (! Serial);
		  delay(500);
	  	  
      /* Attempt to connect to an access point */
		  char *ssid = WLAN_SSID;             /* Max 32 chars */
		  Serial.print(F("\nAttempting to connect to ")); 
		  Serial.println(ssid);

     
      if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
        Serial.println(F("Failed!"));
        while(1);
        return -1;
		}
		
	    delay(15 * 1000);
        
      Serial.println(F("Connected to Wireless Network!"));
      Serial.println(F("Request DHCP..."));
      
      while (!cc3000.checkDHCP())
      {
        delay(100);
      }
	  
	  /* Display the IP address DNS, Gateway, etc. */  
		  while (! displayConnectionDetails()) {
			delay(1000);
		  }
      
      
      
}	  
listen function:

Code: Select all

void listen(){

  // listen for incoming clients and output to Dynamic webpage
  
    if (!cc3000.checkConnected())      // make sure we are still connected to wireless network
      {
	  	  
        if (init_network())    // reconnect to WLAN
        {
          delay(15 * 1000); // if no connection, try again later
          return;
        }
        
     }
	
	 
  Adafruit_CC3000_ClientRef client = server.available();
  
  //Serial.print statement stops here; net Serial.print statement does not print
  //on opening browser to server ip.

  if (client) {
    // an http request ends with a blank line
    Serial.print("client found");
    getDateTime();
    Serial.println(dtStamp);

    boolean currentLineIsBlank = true;
    while (client.connected()) {

      if (client.available()) {

        char c = client.read();
        // buffer first part of HTTP request in HTTP_req array (string)
        // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)

        if (req_index < (REQ_BUF_SZ - 1)) {

          HTTP_req[req_index] = c;          // save HTTP request character
          req_index++;
        }
        // print HTTP request character to serial monitor
        Serial.print(c);

        if (c == '\n' && currentLineIsBlank) {  

          // open requested web page file
          if (StrContains(HTTP_req, "GET / ")){

            // send a standard http response header
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connnection: close");
            client.println();
            // output dynamic webpage
            client.println("<!DOCTYPE HTML>");
            client.println("<html>\r\n");
            client.println("<head><title>Weather Observations</title></head>");
            // add a meta refresh tag, so the browser pulls again every 15 seconds:
            //client.println("<meta http-equiv=\"refresh\" content=\"15\">");
            client.println("<h2>Treyburn Lakes</h2><br />");
            client.println("BANNED, IN 46239<br />");
            client.println("Date,Time:  " + (dtStamp) + " EST<br />");
            client.println("Temperature:  ");
            client.print(DHT.temperature * 9/5 + 32);
            client.print(" F.<br />");
            client.println("Humidity:  ");
            client.print(DHT.humidity);
            client.print(" %<br />");
            client.println("Barometric Pressure:  ");
            client.print((Pressure *  0.000295333727), 3);  //Convert Pascals to inches of Mecury
            client.print( " in. Hg<br />");
            client.println("Barometric Pressure:  ");
            client.print((Pressure) * .01);  //Convert Pascals to millibars
            client.print(" mb.<br />");
            client.println("Atmosphere:  ");
            client.print((Pressure * 0.00000986923267), 3);   //Convert Pascals to Atm (atmospheric pressure) 
            client.print(" atm <br />");
            client.println("Altitude:  ");
            client.print(Altitude * 0.0328084);  //Convert cm to Feet
            client.print(" Feet<br />");
            client.println("<br /><br />");
            client.println("<h2>Collected Observations</h2>");
            client.println("<a href='log.txt' download>Download: Data Collection</a><br />");
            client.println("<br />\r\n");
            client.println("</html>\r\n");
			
          } 

          else if (StrContains(HTTP_req, "GET /log.txt")) {

            webFile = SD.open("log.txt");

            if (webFile) {

              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: application/octet-stream");
              client.println("Content-Disposition: attachment");
              //client.println("Content-Length: ");
              //client.println(webFile.available());
              client.println("Connnection: close");
              client.println();

              //this goes in the function that sends the data
              char buffer[ BUFSIZE ];
              int i;

              while ( webFile.available() ) {
                for (i=0 ; (i < BUFSIZE) && (buffer[ i-1 ] >= 0) ; i++) {
                  buffer[i] = webFile.read();
                }
                client.write( buffer, (buffer[i-1] >= 0) ? i : i-1 ); 
				}
		    }
			// reset buffer index and all buffer elements to 0
            req_index = 0;
            StrClear(HTTP_req, REQ_BUF_SZ); 
			 
			
			webFile.close();
            delay(30);
			break;			
          } 
          
		  else if ((!StrContains(HTTP_req, "GET / "))||(!StrContains(HTTP_req, "GET /log.txt"))){
		  
		  		  
		  // Page Not Found 404
          
            // send a HTTP response header
            client.println("HTTP/1.1 404");
            client.println("Content-Type: text/html");
            client.println("Connnection: close");
            client.println();
            // output webpage
            client.println("<!DOCTYPE HTML>");
            client.println("<html>\r\n");
            client.println("<h2>Page Not Found</h2><br />");    
            client.println("<html>\r\n");
			
    	 }
		 
         // reset buffer index and all buffer elements to 0
            req_index = 0;
            StrClear(HTTP_req, REQ_BUF_SZ);  
            break;			 
        
		}
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }

        else if (c != '\r') {

          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    //delay(10);
    // close the connection:
    client.close();
    delay(10000);
    Serial.println("Ready");
    
    }
  
}
Attachments
listen function.txt
Function used to serve web page
(5.7 KiB) Downloaded 128 times
init_network funtion.txt
Function used to reconnect to WLAN
(1.16 KiB) Downloaded 124 times

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

Re: CC3000 Function not establishing client connection

Post by adafruit_support_mike »

It looks like you're using the wrong class.

If you want the Arduino to act as a server, you need an Adafruit_CC3000_Server object instead of an Adafruit_CC3000_Client.

User avatar
tech500
 
Posts: 199
Joined: Wed Dec 04, 2013 3:53 pm

Re: CC3000 Function not establishing client connection

Post by tech500 »

Adafruit_CC3000_ClientRef client = server.available(); is used in the listen function; on return from the init_network function.

I tried using Adafruit_CC3000_ClientRef client = server.available() instead of Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port) in the init_network function; same result. No Browser, client connection, attempt client connection times out.

Can I use both methods the way I have used them in the functions?

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: CC3000 Function not establishing client connection

Post by tdicola »

Hrm, it's a little tough to tell just from these two functions what might be going wrong. Can you describe what the code is trying to do? Are you trying to reconnect to the wireless network if the connection was lost, or something similar?

User avatar
tech500
 
Posts: 199
Joined: Wed Dec 04, 2013 3:53 pm

Re: CC3000 Function not establishing client connection

Post by tech500 »

Trying to implement Barry914's guard condition. For testing purpose I have used cc3000.disconnect(); in setup to disconnect from wireless network cc3000.checkConnected(); becomes 0 and the init_network() function completes getting ip. Upon return from init_network(), I start instance of Adafruit_CC3000_ClientRef client = server.available(). Trying to establish Browser client connection fails with session time out.

When I do not use cc3000.disconnect() for testing purpose; I am able to establish Browser client connection using the listen() function. Function init_network is skipped since the wireless network is connected.
Attachments
Does_not_work.zip
Complete sketch I am working with -- including the two functions discussed in this thread.
(6.84 KiB) Downloaded 23 times
Last edited by tech500 on Wed Sep 17, 2014 7:39 pm, edited 1 time in total.

User avatar
tech500
 
Posts: 199
Joined: Wed Dec 04, 2013 3:53 pm

Re: CC3000 Function not establishing client connection

Post by tech500 »

I have the function working it appears; I had forgotten to use: server.begin();

init_network function is called here in the listen function

if (!cc3000.checkConnected()) // make sure we are still connected to wireless network
{

if (init_network()) // reconnect to WLAN
{
delay(15 * 1000); // if no connection, try again later
return;
}

}
init_network working function.txt
Working function --guard condition
(1.28 KiB) Downloaded 141 times

User avatar
Barry914
 
Posts: 448
Joined: Sun Dec 08, 2013 1:26 pm

Re: CC3000 Function not establishing client connection

Post by Barry914 »

I posted some changes to that code, don't know if you've seen it. It won't lock up if the network's not available when it tries to connect or reconnect.

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: CC3000 Function not establishing client connection

Post by tdicola »

Yep the updated function looks good. One important thing is calling cc3000.reboot() since that will shut down and start back up the CC3000 and help make sure it's not in bad state. Glad to hear it works for you now.

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

Return to “Other Arduino products from Adafruit”