I'm not sure if this the right place to post this, or even if it's the right thing to post, but I've been banging my head against the wall trying to figure out why the little green light won't go on. The code segment below is from a wireless project that's supposed to send a text message via Amazon's Simple Notification Service. There's some code that checks for a WLAN connection, and if it has been lost it does a reboot of the Adafruit CC3000 wireless breakout. Once a connection is established the code makes an NTP server query, and if successful publishes a connection message. There are 3 LEDs: a red LED that is lit at startup and gets turned off if the CC3000 is initialized successfully; a yellow LED that gets lit whle waiting for an IP from DHCP, and a green LED that gets lit, and stays lit after a successful NTP query. The problem is that if the connection is lost and is reestablished, the green LED is supposed to go out, and it is supposed to light again after the next NTP query. It all works as it's supposed to with the exception of the green LED not going back on. I can't use the serial monitor for debugging, but when I was testing it on the bench it seemed to work OK, but I added the LED code after it was installed. I concluded either it never queries the NTP server upon a reconnect, or it does but it doesn't light the light. I have been staring at this code for days, no luck. Anybody want to look this over and see what I've done wrong? I'd really appreciate the help.
The MCU is a Micro. I do get connection messages when it connects or reconnects to the WLAN, the green light comes on after a cold boot, and everything else does what it's supposed to. I've waited a couple of days for the green LED to come on after a reconnect, but it just doesn't happen.
#define SWITCH_CHECK_SECONDS 1 // How long to wait (in seconds) between switch state checks.
#define LOOP_COUNT 12 * 3600 / SWITCH_CHECK_SECONDS - 1 // Get NTP time every 12 hours
void setup(void)
{
pinMode(switchPin, INPUT_PULLUP); // LOW on this pin triggers message generation
pinMode(error1LED, OUTPUT); // Set up the status lamps
pinMode(error2LED, OUTPUT);
pinMode(readyLED, OUTPUT);
digitalWrite(error1LED, LOW); // red: Attemptiing to initialize CC3000
digitalWrite(error2LED, LOW); // yellow: Attempting to connect to WLAN
digitalWrite(readyLED, LOW); // green: WLAN connected and got NTP Time
#ifdef DEBUG
// Set up the serial port connection.
Serial.begin(115200);
delay(5000); // Some time to get the monitor up
Serial.println(F("SMS Mailbox v2"));
#endif
// set up wireless network connection, query NTP server
init_network(); // Sets status lights, should go from red to yellow
}
void loop(void)
{
if (!cc3000.checkConnected()) // make sure we are still connected to wireless network
{
if (!init_network()) // try a cold start connect to WLAN
{
delay(15 * 1000); // if we couldn't connect, try again later
return;
}
}
if (countdown == 0) // Time to query NTP server?
{
if (!queryTimeServer()) // This will turn on green LED if successful
{
cc3000.reboot();
return; // unable to get NTP server after repeated tries, give up
}
countdown = LOOP_COUNT;
}
else
{
countdown--; // Don't poll; use math to figure current time
}
// Update the current time: last NTP time + timer0 - (timer0 at time of NTP query)
unsigned long currentTime = lastPolledTime + (millis() - sketchTime) / 1000;
if (newConnection) // Publish a message if we have just connected to the WLAN
{
snsPublish(SNS_TOPIC_ARN, "Mailbox%20on%20line", currentTime);
newConnection = 0;
}
// the rest of my application code goes here
delay(SWITCH_CHECK_SECONDS * 1000); // This value pretty much sets the rep rate of loop()
}
int8_t init_network(void)
{
cc3000.reboot();
// Set up the CC3000, connect to the access point, and get an IP address.
#ifdef DEBUG
Serial.println(F("Initializing CC3000..."));
#endif
digitalWrite(readyLED, LOW);
digitalWrite(error1LED, HIGH);
digitalWrite(error2LED, LOW);
if (!cc3000.begin()) // fatal
{
#ifdef DEBUG
Serial.println(F("Couldn't begin()"));
#endif
while(1);
}
digitalWrite(error1LED, LOW); // red off
digitalWrite(error2LED, HIGH); // yellow on
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
#ifdef DEBUG
Serial.println(F("Failed to connect to AP!"));
#endif
return 0; // zero = failure. also leaves yellow LED on
}
digitalWrite(error2LED, LOW); // successful connect so turn off yellow LED
#ifdef DEBUG
Serial.println(F("Connected to Wireless Network!"));
Serial.println(F("Request DHCP..."));
#endif
while (!cc3000.checkDHCP())
{
delay(100);
}
#ifdef DEBUG
Serial.println(F("Got IP"));
#endif
newConnection = 0xff; // set trigger for connect message
return -1; // non-zero = success
}
int8_t queryTimeServer(void)
{
uint8_t cnt = 30;
unsigned long t = getTime();
while (t == 0 && cnt != 0)
{
// Failed to get time, try again every 10 seconds.
delay(10 * 1000);
t = getTime();
cnt--;
}
if (!cnt) // give up after 5 minutes
{
return 0;
}
// sucess!
lastPolledTime = t;
sketchTime = millis();
digitalWrite(readyLED, HIGH); // turn on green LED
#ifdef DEBUG
Serial.println(F("Got NTP Time"));
Serial.print(F("Current UNIX time: "));
Serial.print(t);
Serial.println(F(" (seconds since 1/1/1970 UTC)"));
Serial.println(F("Ready..."));
#endif
return -1;
}