Problem getting cc3000 to work

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

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Problem getting cc3000 to work

Post by Brewington »

Hi,

I am having a strange problem trying to operate the cc3000 (firmware 1.24) on a Mega2560 as an echo server. The Arduino IDE is version 1.0.5-r2.

I have run the various example scripts for the cc3000 (buildTest, etc) and everything worked fine. The shield and library are working correctly.

However, when I run the echoServer sketch I get strange behavior. The problem: when the client program sends data the sketch freezes . Before the client program sends data I see the "Checking echoServer" message as seen in the Serial Console output below. When data is received from the client program the sketch "freezes" on the if statement marked as "A" in the code. I do see the appropriate (I think) values for theClient.Connected and theClient.Available. The sketch never reaches the println "Client exists", and the LED remains on; the code never reaches the point "B" where the LED is turned off.

I have slightly modified the original sketch to
- add print statements and to light pin 13 LED for debugging; the code is shown below.
- change the name of the client variable to theClient, just in case the compiler was getting confused.
- I tried the test -->if (theClient.available())<-- instead of -->if (theClient)<-- just to see if it makes a difference - it doesn't.

Modified Code:

Code: Select all

void loop(void)
{
  char s[100];
  // Try to get a client which is connected.
  Serial.print("Checking echoServer.available...");
  Adafruit_CC3000_ClientRef theClient = echoServer.available();
  sprintf(s, "OK %d %d Avail %d", iter, theClient.connected(), theClient.available());
  Serial.println(s);
  iter++;
  digitalWrite(13,HIGH);
  if (theClient) {                                                     [color=#FF0000]// A - Freeze point[/color]
     // Check if there is data available to read.
     digitalWrite(13,LOW);                                       [color=#FF0000]// B - Never gets here[/color]
     Serial.println("Client exists");
     if (theClient.available() > 0) {
       // Read a byte and write it to all clients.
       uint8_t buf[256];
       uint8_t ch = theClient.read();
       theClient.write(ch);
       Serial.println(".");
     }
  }
  delay(500);
  digitalWrite(13,LOW);
  delay(300);
}
Output from Serial console:
Checking echoServer.available...OK 18 0 Avail 0 <-- LED 13 blinking, as expected
Checking echoServer.available...OK 19 0 Avail 0
Checking echoServer.available...OK 20 0 Avail 0
Checking echoServer.available...OK 21 0 Avail 0
Checking echoServer.available...OK 22 0 Avail 0
Checking echoServer.available...OK 23 0 Avail 0
Checking echoServer.available...OK 24 0 Avail 0
Checking echoServer.available...OK 25 1 Avail 1 <--- data sent from Client program

Nothing appears after this, and LED 13 stays on

I don't know what else to test - this "seems" pretty simple, I don't know why the sketch would freeze.

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: Problem getting cc3000 to work

Post by Brewington »

Additional information / confusion.

1) I put a different check for the variable theClient after it is returned from
theClient = echoServer.available();

sprintf(s,"Client is %s",(theClient == NULL)? "NULL" : "NotNull");
Serial.println(s);

theClient is NULL whenever data is not returned, then becomes NotNull when data is received.
a) How do the calls theClient.available() and theClient.connected() return values when theClient is NULL?

b) Of course, the if statement still hangs if I change the statement to
if (theClient != NULL)

2) I enabled the cc3k_Printer logic, which looks like it prints debug type messages. I see a couple of extra statements during initialization, but nothing extra when the main loop is running. Still hangs at the if statement.

3) I read on another thread to check the Vin voltage on the Vin pin, it should be 5 volts.
I checked mine, and it reads 9.87 Volts.

My input power is from both the USB port and a separate 12V 800mA wall wart.

Am I about to blow yet another Arduino board? I thought I was supposed to be regulated to 5 volts:(

I tried the sketch again with only the USB connected (no separate power) and a) the sketch still hangs at the if statement, and b) the voltage on Vin is 4.27 volts.
My understanding is that less than 5 volts is expected to cause flakey behavior in the cc3000. I don't know what to do about the power.

And, of course, the sketch still doesn't run.

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

Re: Problem getting cc3000 to work

Post by adafruit_support_mike »

My guess is that you're seeing a memory crash due to the creation of the 256-character buffer a few lines down from the freeze point.

This is one of those places where human-readable code and the machine-executable output aren't in sync. Modern versions of C allow you to declare variables wherever you want, but in older versions you had to list them all at the beginning of the function. The new way makes reading code easier, but doesn't match what the CPU is actually doing.

C variables have 'block' scope, meaning they only have meaning between the nearest open/close brace pair around them (called 'a block'). The block can't execute code unless it has storage space for the variables, so the compiler silently shuffles all the allocation steps to the beginning of whatever block it considers most efficient.

Try measuring free RAM test as shown in this tutorial:
https://learn.adafruit.com/memories-of-an-arduino

and see if you're getting close to the limits before that block executes.

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: Problem getting cc3000 to work

Post by Brewington »

OK, I tried a couple of things. Nothing fixed the hang.

1) I moved the 256 char buffer to be at the top of the loop, before any code.

2) I added the freeRam function from the article (although afterward I see that this function is already available, used in the setup function).

It shows 6471 bytes available during the setup loop, and 6361 bytes available during the loop function. This value does not change when theClient variable gets loaded (when data is received). The value does not change in each loop iteration, so nothing is "eating" stack/heap (like a memory leak).

3) I changed all my strings to use the F() macro. This slightly altered the values (setup shows 6571, loop shows 6457) but I think I am well away from a collision.

I am running a Mega 2560, so I think I should have plenty of memory?

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

Re: Problem getting cc3000 to work

Post by adafruit_support_mike »

Yeah, you have SRAM to spare.. so much for that guess. ;-)

As an aside -- the ~10v reading you're getting on Vin isn't a problem. Vin is connected to the DC barrel jack upstream of the Arduino's 5v regulator. The voltage you get there is raw output from the power supply. The 5v pin is the one connected below the regulator. As long as that is between 4.75v and 5.25v, the electronics will be happy.

Try commenting out everything in the if() block except the digitalWrite() that turns the LED off. It would be extremely wierd for the code to hang on mere examination of a pointer, but we haven't ruled it out.

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: Problem getting cc3000 to work

Post by Brewington »

Good to know about the voltage; I was worried I had already fried something.

OK, I commented out the code inside the if statement, leaving only the digitalWrite to turn off the LED. I also removed the freeRam function, and commented out the 256 byte buffer.

It still hangs; the LED stays on.

Here is the minimal form of the code, which still hangs with the LED on:

Code: Select all

#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "utility/debug.h"
#include "utility/socket.h"

// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIVIDER); // you can change this clock speed

#define WLAN_SSID       "DoubleSlit"           // cannot be longer than 32 characters!
#define WLAN_PASS       "Brew&Grace1552"
int iter;        // temp - brew

// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

#define LISTEN_PORT           7    // What TCP port to listen on for connections.  The echo protocol uses port 7.

Adafruit_CC3000_Server echoServer(LISTEN_PORT);

void setup(void)
{
  Serial.begin(115200);
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }
  
  Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }
   
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  

  echoServer.begin();
  
  pinMode(13,OUTPUT);
  iter = 0;
}

void loop(void)
{
  // Try to get a client which is connected.
  Adafruit_CC3000_ClientRef theClient = echoServer.available();
  Serial.println(iter);
  iter++;
  digitalWrite(13,HIGH);
  if (theClient != NULL) {
     digitalWrite(13,LOW);
  }
  delay(500);
  digitalWrite(13,LOW);
  delay(300);
}

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

Re: Problem getting cc3000 to work

Post by adafruit_support_mike »

Okay, let's confirm the negative.. pull this out completely:

Code: Select all

  if (theClient != NULL) {
     digitalWrite(13,LOW);
  }
and make sure the code just loops forever.

If that's the case, try reducing the test to:

Code: Select all

  if ( theClient ) {
     digitalWrite(13,LOW);
  }
and see if that has any effect.

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: Problem getting cc3000 to work

Post by Brewington »

Unfortunately I am out of town for 2 weeks. I will try these as soon as I return. I believe I have done both of these, but I will double check.

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

Re: Problem getting cc3000 to work

Post by adafruit_support_mike »

Whenever you can. ;-)

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: Problem getting cc3000 to work

Post by Brewington »

OK, back in town for a day.

I commented out the entire IF block. In this case the Arduino code runs fine - it does not lock up when the client program sends data.

I returned the IF block to action, and changed the IF statement to
IF (theClient)
This should be the same construct as IF (theClient != NULL), correct? Anyway, at this point the Arduino sketch hangs again when the client program sends data (when theClient returns a non-NULL pointer). The LED does not continue to flash, and the iteration counter does not continue incrementing in the Serial console window.

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

Re: Problem getting cc3000 to work

Post by adafruit_support_mike »

Brewington wrote:I commented out the entire IF block. In this case the Arduino code runs fine - it does not lock up when the client program sends data.
Okay, that's what we expected, but for something as wierd as this it's good to get confirmation.
Brewington wrote:I returned the IF block to action, and changed the IF statement to
IF (theClient)
This should be the same construct as IF (theClient != NULL), correct?
Yup.. the two expressions are logically identical.
Brewington wrote:Anyway, at this point the Arduino sketch hangs again when the client program sends data (when theClient returns a non-NULL pointer). The LED does not continue to flash, and the iteration counter does not continue incrementing in the Serial console window.
Okay, this confirms the problem occurs when the program looks at the client pointer, which is freaky.

I can only think that the compiler has optimized the code in a way that creates an unexpected connection between that test and something that can fail, but darned if I know what it is. I'm gonna have to pass this over to our CC3000 gurus.

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

Re: Problem getting cc3000 to work

Post by tdicola »

Do you mind posting the full code for the sketch? Sorry it's a little tough to follow what was tried and changed. One thing to note though, there actually is a subtle difference between these statements:

Code: Select all

if (theClient) {
...

Code: Select all

if (theClient != NULL) {
...
The Adafruit_CC3000 client class has a boolean operator which allows the compiler to convert it into a true/false value in an if statement, so the first example is actually calling the bool operator (which internally calls the connected() function on the client) and not checking for a null / non-null value. It's a subtle difference but might be why you see some odd differences in the two if checks.

For the bigger issue of locking up on send/receive one thing to check, how much data are you sending the echo server? By default the CC3000 can only handle about 90 characters at a time either sending or receiving, so if you try to send too big a message it will lock up in a loop trying to prevent itself from overflowing memory.

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: Problem getting cc3000 to work

Post by Brewington »

My post on Oct 17 shows the exact sketch. In the last iterations I modified that sketch as requested - in one case removing the small if block, in the other case just changing the form of the if statement.

The client program allows me to type in a string which it sends. I have only been trying 2-5 characters, certainly nothing near 90 characters.

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

Re: Problem getting cc3000 to work

Post by tdicola »

Thanks for confirming, that helps rule out a lockup in the transmit side from a buffer overflowing. Let's try upgrading to the latest firmware version 1.13 next, since version 1.11 (reported as 1.24 in buildtest) had some known issues from TI around internal buffer exhaustion that would lock up the host driver code.

Check out this page with the steps for upgrading the firmware: https://learn.adafruit.com/adafruit-cc3 ... e-upgrades The basic idea is to load the driverpatch_1_13 example on the device, then open the serial console and type something when it prompts you to start the upgrade. After upgrading run the buildtest sketch again to make sure it works (might need to reset the Arduino too) and then try the echo server example again.

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: Problem getting cc3000 to work

Post by Brewington »

I have been away from this project for awhile, finally got some time to resume.

Bottom Line - it is now working:)

First, I replaced the wall wart power supply with a 10A 12V unit. Adjusted the output from 7 to 12 Volts; no joy, the echoServer/cc3000 continued to fail. BuildTest, etc continue to be fine.

I then updated the following 3 pieces:

1) the cc3000 library; there doesn't seem to be any version information on the library so I can't say from what version to what version. Whatever was current on 1/29/2015. There were some notes about changes for cc3000 firmware 1.14.

2) the Arduino IDE from 1.0.5 to 1.0.6.

3) the cc3000 firmware from 1.11 to 1.14.

Now, the echoServer runs fine.

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

Return to “Arduino Shields from Adafruit”