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.
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().
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).