CC3000 losing connection issue

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
gutsjet
 
Posts: 9
Joined: Wed May 28, 2014 12:56 am

CC3000 losing connection issue

Post by gutsjet »

Hi, I'm trying to solve and understand the problem I'm having with my project. I've been reading many posts at the forum but could not solve my problem. Basically I want to send data from adafruit's 9dof IMU and GPS using arduino nano v3.1 and cc3000 (1.28 firmware), and the entire system is powered from an external battery (the arduino is not powering anything). I've set a local host that will get the data and save in a database. My code is based on the Wi-Fi Weather Station example to send my data (using send request etc).

I need data from IMU to be sent every 60ms and GPS every 3sec. For the IMU I send a request to the server every half second roughly. This means that I'm reading data eight times (8x60=480ms), inserting into a char array (that I prealocate in the beginning) then call send_request(). The size of the char array I'm allocating is 300, which is larger than my IMU worst case scenario data size (which is around 200 characters).
I tweaked the send_request function so I can send data in small packets (90 characters).

I am able to send data and read it but the problem I'm having is that the connection drops very frequently at random times (usually around a minute or less). I don't know if there is something to do but once I was able to collect data for almost 10min then it dropped. This test was done in a different network than the one I usually test, but the security was the same.

Looking at the arduino memory using free_ram, in all points I've placed it, the result was around 300. However, sometimes I have the led L on the arduino board flashing quickly and the reset button doesn't work - I have to unplug and plug the power back again. This strange behavior sometimes can be an indication of a memory issue, according to some post I've read, but I'm not sure.

Can you help me or give me any insight of what would be my issue? The program always freezes in the send_request function but in random points.

I appreciate any help.
Thanks

Code: Select all

/* 
*  Simple WiFi weather station with Arduino, the DHT11 sensor & the CC3000 chip
*  Part of the code is based on the work done by Adafruit on the CC3000 chip & the DHT11 sensor
*  Writtent by Marco Schwartz for Open Home Automation
*/

// Include required libraries
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <Adafruit_L3GD20_U.h>
#include <Adafruit_9DOF.h>

#include <avr/wdt.h> // for watchdog timer

/*Assigning a unique ID to IMU sensors*/
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
Adafruit_L3GD20_Unified       gyro  = Adafruit_L3GD20_Unified(20);
//Adafruit_LSM303_Mag_Unified   mag   = Adafruit_LSM303_Mag_Unified(30302);


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

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

// Create CC3000
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2);
                                         
// Local server IP, port, and repository (change with your settings !)
uint32_t ip = cc3000.IP2U32(10,1,10,62);
int port = 12345;

static unsigned long time=0;     // IMU counter
static unsigned long time_gps=0; // GPS counter

int i=0;
int l=0;
static char data[300];   // Data buffer
static int counter = 0;

volatile int WD_counter;   // for watchdog timer
volatile int countmax = 10; // Timer expires after...
                            // if 8 sec interval is selected below
                            // 8 x 10 = 80 sec... 
                            // about 4 x the normal duration of 21 secs

void setup(void)
{
  Serial.begin(9600);
  
//   ***********************************
  // Initialise the CC3000 module
  if (!cc3000.begin())
  {
    while(1);
  }

  // Connect to  WiFi network
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
    
  // Check DHCP
//  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }
//  **********************************
 
  accel.begin();
  gyro.begin(); 
 
}

void loop(void)
{   
    if(millis()-time>=60){     // IMU data collection every 60ms
      time=millis();  

//      Serial.println(free_ram());
      get_IMU();               // Function used to collect IMU data
      i += 1;
      watchdogEnable();        // Watchdog -> reset arduino if gets stuck
      if(i == 8){          //Sending data every 484ms approx. 8->289 - 60
        if(millis() - time_gps >= 3000){ // Collecting GPS data every 5min
          get_GPS();           // Function to collect GPS data
          time_gps = millis();
        }
        
//       Serial.println(free_ram());

        send_request();         // Function to send data to website - cc3000
    
//      Serial.println(free_ram());            

        counter = 0;
        memset(data, 0, sizeof(data));
        
        i=0;
      }      
    }
}

// ******************************
// Function to send a TCP request and get the result as a string
void send_request (void) {  
    char temp[90];
    int s = sizeof(data)/90;
    char *temp1;       
  // Connect    
    Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);     
    // Send request and data transfer
    if (client.connected()) {
      client.fastrprint("GET /home/saveactivitydata/"); 
      client.fastrprint("?data=");    
      for(int j=0; j<s; j++){         // For loop to brake data in small packets
        strncpy(temp, data+(90*j), 90);
        temp[90] = '\0';
        temp1=temp;
        client.fastrprint(temp1);
      }
      client.fastrprint("&id=1");
      client.fastrprint(F("\r\n"));

      // continue populating the http request header
      client.fastrprint(F(" HTTP/1.1\r\nHost: "));
      client.fastrprint(F("10.1.10.62:12345"));
      client.fastrprint(F("\r\nUser-Agent: "));
      client.fastrprint(F("CC3000_Wifi_Test"));
      client.fastrprint(F("\r\nConnection: close\r\n"
                       "Content-Type: application/x-www-form-urlencoded;charset=UTF-8\r\n"
                       "Content-Length: "));      
      client.println(sizeof(data));
//      Serial.println("Connected & Data sent");
    } 
    else {
//      Serial.println(F("Connection failed"));    
    }

    while (client.connected()) {
      while (client.available()) {   
      // Read answer
      char c = client.read();         
//      Serial.print(c);
      }
//      Serial.println(F(" "));
    }
    client.close();   
}
// ******************************


// Function to get IMU data
void get_IMU(void){
  int dat;
  String IMU="";
//      Serial.println(free_ram());  
    sensors_event_t event;
    accel.getEvent(&event);   
//      Serial.println(free_ram());    
    dat = 100*event.acceleration.x;
    IMU+=String(dat)+";";
    dat = 100*event.acceleration.y;
    IMU+=String(dat)+";";
    dat = 100*event.acceleration.z;
    IMU+=String(dat)+";";  
//      Serial.println(free_ram());        
    gyro.getEvent(&event);
    dat = 100*event.gyro.x;
    IMU+=String(dat)+";";
    dat = 100*event.gyro.y;
    IMU+=String(dat)+";";
    dat = 100*event.gyro.z;
    IMU+=String(dat)+";";  
//      Serial.println(free_ram());        
    for (int k = 0; k < IMU.length(); k++){
      data[counter] = IMU[k];
      counter += 1;
    } 
//      Serial.println(free_ram());        
}

void get_GPS(void){
  char temp_gps='\0'; 
  l=0;
  if(Serial.available()){
    if(Serial.find("$GPGGA,")){
      if(Serial.find(",")){
        while(Serial.available()<24);
        while(l<24){
          temp_gps=Serial.read();
          if(temp_gps=='.'){data[counter+l]=',';}
          else if (temp_gps==','){data[counter+l]=';';}
          else data[counter+l] = temp_gps;     
          l++;
          data[counter+l]='\0';        
        }          
      }
    }
  }
}


void watchdogEnable()
{
  WD_counter=0;
  cli();                              // disable interrupts

  MCUSR = 0;                          // reset status register flags

                                      // Put timer in interrupt-only mode:                                        
  WDTCSR |= 0b00011000;               // Set WDCE (5th from left) and WDE (4th from left) to enter config mode,
                                      // using bitwise OR assignment (leaves other bits unchanged).
  WDTCSR =  0b01000000 | 0b100001;    // set WDIE (interrupt enable...7th from left, on left side of bar)
                                      // clr WDE (reset enable...4th from left)
                                      // and set delay interval (right side of bar) to 8 seconds,
                                      // using bitwise OR operator.

  sei();                              // re-enable interrupts
  

  // delay interval patterns:
  //  16 ms:     0b000000
  //  500 ms:    0b000101
  //  1 second:  0b000110
  //  2 seconds: 0b000111
  //  4 seconds: 0b100000
  //  8 seconds: 0b100001

}

ISR(WDT_vect) // watchdog timer interrupt service routine
{
  WD_counter+=1;

  if (WD_counter < countmax)
  {
    wdt_reset(); // start timer again (in interrupt-only mode)
  }
  else             // then change timer to reset-only mode with short (16 ms) fuse
  {
    
    MCUSR = 0;                          // reset flags

                                        // Put timer in reset-only mode:
    WDTCSR |= 0b00011000;               // Enter config mode.
    WDTCSR =  0b00001000 | 0b000000;    // clr WDIE (interrupt enable...7th from left)
                                        // set WDE (reset enable...4th from left), and set delay interval
                                        // reset system in 16 ms...
                                        // unless wdt_disable() in loop() is reached first
  }
}


// Function to measure ram size
//int free_ram () 
//{
//  extern int __heap_start, *__brkval; 
//  int v; 
//  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
//}

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

Re: CC3000 losing connection issue

Post by adafruit_support_mike »

The trouble with the LED and boot behavior might be a symptom of something in the hardware connections. Post a photo of your setup and we'll see if we can find anything.

User avatar
gutsjet
 
Posts: 9
Joined: Wed May 28, 2014 12:56 am

Re: CC3000 losing connection issue

Post by gutsjet »

Thanks for the response, attached are the pictures from my hardware.
At first I thought it was a problem with the powerboost 500 that I'm using to step up the voltage to power my system. However, this problem is happening also when I remove the battery and powerboost and power everything from the USB port.

And what about losing connections, any suggestions?

Thank you for your help!
Attachments
photo 2.JPG
photo 2.JPG (147.77 KiB) Viewed 858 times
photo 1.JPG
photo 1.JPG (150.72 KiB) Viewed 858 times

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

Re: CC3000 losing connection issue

Post by adafruit_support_mike »

Any problem serious enough to interfere with the reset button would probably kill the CC3000's wifi connection as well.

The reset button is wired to a non-maskable interrupt on the microcontroller, so for it to fail, one of two things must be happening:

1) The chip is damaged and can't reset properly

2) The reset does actually work, but the Arduino is falling into a fast cycle of reset/reboots and another press on the button is just lost in the shuffle.

Since the Arduino does work some of the time, I'm inclined to think it's option 2.

The LED marked L is connected to the SCK line, which will go active when the system tries to talk to SPI peripherals like the CC3000. The SPI clock runs much faster than a human can see, so a visible blink pattern would be consistent with option 2 as well.

Try putting your nano on a separate power supply from all the other devices and see if that changes anything.

User avatar
gutsjet
 
Posts: 9
Joined: Wed May 28, 2014 12:56 am

Re: CC3000 losing connection issue

Post by gutsjet »

I tried to power the nano with a different power supply but then the cc3000 never got to connect with the wifi. I was thinking that maybe because the two systems (arduino + sensors) have different grounds, this may be an issue and that's why I never get connection.

One thing that happened was when I touch the CC3000 IRQ leg with a osciloscope probe, the system became more stable meaning the connection is dropping now around 5 minutes. I was thinking that maybe was because of the capacitance or impedance of the probe... I don't know why would this interfere but it seems is doing something (I might be wrong)...

I'm running the system now with a battery powering all the devices for about 4 hours, with the connection dropping around 5 minutes and that behave from the L led didn't happen so far.

So, as I cannot see any more clear indication from the hardware side, could it be a problem with my code now?

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: CC3000 losing connection issue

Post by Franklin97355 »

You need to have a common ground between all parts of the system physically connected to each other. In order to communicate they need a "signal return" link.

User avatar
gutsjet
 
Posts: 9
Joined: Wed May 28, 2014 12:56 am

Re: CC3000 losing connection issue

Post by gutsjet »

Yes, I did that and still the same. The cc3000 keeps dropping the connection around 5min but no strange behavior of the L led anymore.

I tried to see where the code stops when the connection drops and is inside my send_request function at random points. Usually is around the client.fastrprint when I populate the http request.
Is there an issue with the way I'm passing the data or may be something else?

User avatar
gutsjet
 
Posts: 9
Joined: Wed May 28, 2014 12:56 am

Re: CC3000 losing connection issue

Post by gutsjet »

Now I think I've narrowed down the problem. Mike you were right about the L led problem. It was because of the fast reset/reboot cycle. The reason why sometimes I was entering this "state" is because of the watchdog timer function. Basically, what was happening is that inside my send_request function, the "cc3000.connectTCP(ip, port);" function sometimes takes more than 8 seconds (which is the value that watchdog timer resets) to respond. Thus it calls the interrupt and the system resets. Somehow, sometimes this was taking the entire system to a reset/reboot fast cycle.

Now my questions are: why the "cc3000.connectTCP(ip, port);" function sometimes takes 20 seconds to respond? Is this an issue with the server/webclient or is a problem with the cc3000 chip? Is this a common behave?

On my code I removed the watchdog function and included this part on my setup(void) after setting all cc3000 parameters, like the suggestion given by tdicola http://forums.adafruit.com/viewtopic.php?f=22&t=46901

Code: Select all

 unsigned long aucDHCP = 14400;
unsigned long aucARP = 3600;
unsigned long aucKeepalive = 10;
unsigned long aucInactivity = 20;
if (netapp_timeout_values(&aucDHCP, &aucARP, &aucKeepalive, &aucInactivity) != 0) {
 Serial.println("Error setting inactivity timeout!");
}

User avatar
gutsjet
 
Posts: 9
Joined: Wed May 28, 2014 12:56 am

Re: CC3000 losing connection issue

Post by gutsjet »

So after leaving the system running overnight the cc3000.connectTCP(ip, port) function still kept blocking my code every 5-10 seconds. In other words, roughly after 10 seconds the system had to wait around 10-20 sec for a response, then it ran for another 10 seconds and waited and so on. This scenario was using a CC3000 with an external antenna.

Now comes a weird thing that I realized just now. When running the buildtest sketch with an external antenna the RSSI of my network was around 41. When I plugged in a cc3000 with a built in antenna the RSSI was 70! Shouldn't this be the other way around? Shouldn't the antenna give me a higher RSSI?

After seeing these strange values of RSSI I decided to replace the cc3000 with one that had a built in antenna. This new board gave a much better result regarding how often this waiting time for a response from cc3000.connectTCP(ip, port) function happened. So looking from a RSSI perspective and assuming this connectTCP function has to do with this value, it is reasonable, in a way, to expect such behavior. However, again, shouldn't the antenna give a better range?

I have this antenna: 2.4GHz Dipole Swivel Antenna with RP-SMA - 2dBi, the uFL connector, and the adapter (all from adafruit)
As I purchased only boards with built in antennas, I have removed it and soldered the uFL connector. I checked my solder and everything looks fine.
Attachments
photo (2).JPG
photo (2).JPG (143.42 KiB) Viewed 761 times

User avatar
gutsjet
 
Posts: 9
Joined: Wed May 28, 2014 12:56 am

Re: CC3000 losing connection issue

Post by gutsjet »

Just figured out the problem with the antenna. Forgot to move around some of the components. Now it is working. But just a following question: the RSSI using the antenna should be just a little higher than with the built in antenna or should be much higher? I'm getting for the external antenna 83 and for the built-in 78.

The system is a little but more stable but still has some "waiting periods" due to the connectTCP function. Any clues on that?

A last question regarding returning policy: I've purchased 15 cc3000 with the built in antenna. I've made changes in 6 of them. Is there a way to send the rest back to adafruit and replace them by the ones with the uFL connector? I read about adafruit's returning policy and I think this will be very unlikely to happen... Just wondering...

Thanks

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

Re: CC3000 losing connection issue

Post by adafruit_support_mike »

gutsjet wrote:But just a following question: the RSSI using the antenna should be just a little higher than with the built in antenna or should be much higher? I'm getting for the external antenna 83 and for the built-in 78.
The best you can say about RSSI is "more is (probably) better."

None of the standards define a relationship between RSSI value and any physical value.. not even a specific scale (linear, logarithmic, etc). If we treat the numbers you're seeing as decibels, the improvement is 5db, or about 3x stronger on a linear scale. That's the normal range for an external antenna.
gutsjet wrote:The system is a little but more stable but still has some "waiting periods" due to the connectTCP function. Any clues on that?
Probably need more data. The best thing I can suggest is to get a copy of Wireshark (http://www.wireshark.org) and snoop the packets as the CC3000 tries to connect to the server. That will give you a better idea of what's happening and/or going wrong.
gutsjet wrote:A last question regarding returning policy: I've purchased 15 cc3000 with the built in antenna. I've made changes in 6 of them. Is there a way to send the rest back to adafruit and replace them by the ones with the uFL connector?
If the ones you want to exchange are unopened, or basically in the same condition as when we shipped them, we can do a swap. We'll need to ding you for shipping both ways, but otherwise there's no problem.

Send a note to [email protected] with a link to this thread and the folks there will arrange the details.

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

Return to “Other Arduino products from Adafruit”