Using CC3000 with millis - not working

Hello forum members,

I have tried to write a sketch (attached) that logs onto the internet every 5 minutes and uploads float values to Xively.

It does not work...

I have a 5 minute delay using millis (I use this as I want the other part of the final sketch to keep working in the meantime)

  const unsigned long fiveMinutes = 1 * 60 * 1000UL;
  static unsigned long lastSampleTime = 0 - fiveMinutes;
  unsigned long now = millis();
  if (now - lastSampleTime >= fiveMinutes){

When I follow this portion of the sketch with a simple command e.g. serial print the floats, the command is followed every 5 minutes, however when I follow it with the CC3000 code the chip reconnects to the internet as soon as it disconnects.

Can anybody tell me why?

CC3000_debugging_nodata_.ino (3.63 KB)

lastSampeTime can not be negative, since it is a unsigned long. Give it a start value of 0.

Hi Tops

    if (now - lastSampleTime >= fiveMinutes){

        uint32_t ip;

        Serial.println(F("Initializing WiFi chip...")); // Initialize chip
        if (!cc3000.begin()){
            Serial.println(F("Couldn't begin()! Check your wiring?"));
            while(1);
        }
    }

Only the initialisation of the cc3000 is controlled by the timer. The other cc3000 code is executed each time round loop().

Also, after lastSampleTime is initialised, it is never updated. I would expect that, each time 5 minutes is up, you would reset lastSampleTime to the current value of millis().

Regards

Ray

Hackscribble:
Also, after lastSampleTime is initialised, it is never updated. I would expect that, each time 5 minutes is up, you would reset lastSampleTime to the current value of millis().

Strictly speaking that method will cause a slight drift to the timing which will accumulate over time. The "drift-free" method is to add your interval (fiveMinutes) to the value of lastSampleTime when your event is triggered (ie. inside the if clause).

Sam.

Thank you all, oh the perils of copy and pasting code!

The reason it was not working was because I had lost a line of code and added a }.

This is how it looks now:

const unsigned long fiveMinutes = 1 * 60 * 1000UL;
static unsigned long lastSampleTime = 0 - fiveMinutes;
unsigned long now = millis();
if (now - lastSampleTime >= fiveMinutes){
  lastSampleTime += fiveMinutes;
  Serial.println(F("Initializing WiFi chip...")); // Initialize chip
  if (!cc3000.begin()){
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
    }
  
  Serial.print(F("Connecting to WiFi network ..."));
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("done!"));

BTW yes I know my five minutes is currently set as one minute - it makes the debugging swifter!

So now I have combined the working debugging sketch with my main sketch but it hangs each time after sending the put request to Xively.

Any more ideas?

CC3000_debugging_nodata_.ino (3.6 KB)

it hangs each time after sending the put request to Xively.

What do you get on serial monitor from the debug print statements?

Adafruit_CC3000_Client client;
XivelyClient xivelyclient(client);

Any change if you move these instantiations out of loop() and put them before setup() as global instances?