I have been trying to work out how to maintain a constant connection to the internet for my project when using the arduino WiFi shield (Or at least reconnect). It looses connection randomly (between 30 minutes and several hours).
I am connecting to Ubidots and updating stats every 30 seconds so I don't think it's an idle time issue.
I've also updated the firmware to the latest.
Does anyone have some example code on diagnosing why it's not maintaining a connection? Or a method to reconnect? The green LED is still illuminated and my router shows a connection but the arduino does not respond to any web requests or a ping.
This also occurs when using the example WiFi sketches included in the arduino IDE.
I've also experienced the same issues and a fast way I've found around this is to reboot the Arduino periodically to make sure it stays fresh. I know it's not very elegant, but I recommend it for any system that might be left unattended (such as a measuring device).
I use this function to reset, which I found here in the forums:
Another healthy practice is to avoid the use of string operators and use the function sprintf instead. This minimizes the risk of causing memory problems.
I have had similar issues with Adafruit's CC3000 WiFi modules. It appears there's some kind of network event that causes them to disconnect. I have several of them and they often disconnect in synch. In my sketches I check to see if the device is connected to the WLAN each time through loop(), and if not I initiate a reconnect, starting with a reboot of the device, a device initialization and a connection sequence. Since it's written for the Adafruit device the code would require some tweaking, but here are some snippets which might help. It has a lot of debug fluff that can be stripped out, but unless DEBUG is #defined the code doesn't get compiled. The LEDs monitor the state of the WiFi connection, and one does double duty as a proof-of-life indicator for the rest of the sketch.
void setup()
{
wdt_disable();
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
#ifdef DEBUG
readString.reserve(80);
//enable serial data print
Serial.begin(115200);
delay(5000); // time to start serial monitor
Serial.println(F("v2.0"));
#endif
}
void loop()
{
// the following "gaurd code" does the initial connect to the WLAN and then
// checks the connect state each time throught loop().
// If the CC3000 disconnects from the WLAN it will automatically attempt to
// reconnect, waiting for the WLAN to recover or come back into range if necessary.
if (!cc3000.checkConnected()) // make sure we are 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;
}
// put any code that needs to be run after a sucessful WLAN connect/reconnecrt here
www.begin(); // start the web server
wdt_enable(WDTO_8S);
#ifdef DEBUG
Serial.println("Listening for connections ...");
#endif
}
// The rest of the code goes here
glow_led(); // Proof of life indicator
wdt_reset();
}
bool 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
greenFlag = false; // glowing green off
digitalWrite(redLED, HIGH); // red on
digitalWrite(yellowLED, LOW); // yellow off
if (!cc3000.begin()) // fatal
{ // the code below will never execute since the call hangs when it fails
#ifdef DEBUG
Serial.println(F("Couldn't begin()"));
#endif
while(1);
}
digitalWrite(redLED, LOW); // red off
digitalWrite(yellowLED, HIGH); // yellow on
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY, WLAN_ATTEMPTS))
{
#ifdef DEBUG
Serial.println(F("Failed to connect to AP!"));
#endif
return false; // fail. Leaves yellow LED on
}
digitalWrite(yellowLED, 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
greenFlag = true; // enable glowing greenLED
return true; // success
}
// Glowing status LED
void glow_led(void)
{
if (greenFlag)
{
unsigned long i = millis();
int j = i % 1000;
if (j > 500)
{
j = 500 - j;
}
analogWrite(greenLED, j/2);
}
}