Skip to content

Instantly share code, notes, and snippets.

@jenschr
Created July 25, 2017 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenschr/72c027128f27c9ff94dd67d733a93b47 to your computer and use it in GitHub Desktop.
Save jenschr/72c027128f27c9ff94dd67d733a93b47 to your computer and use it in GitHub Desktop.
Test script for checking if the WICED is able to reconnect when a connection is lost
/*
* This example shows that the WICED is not capable of
* recovering from a network fallout. It will keep
* returning the error:
*
* SDEP_CMD_TCP_WRITE failed, Error: NOTUP (9)
*
* even if it can reconnect.
*
* To test - update wifi-credentials below and connect
* to an AP that is easy to get to (like your phone).
*
* After pulling down the first test page, turn off the
* AP and see the error occur the first time. After you
* see the error, turn the access point back on. It
* will never recover from the error and will keep
* printing the error on the Serial monitor.
*
* Uses a RGB LED on pins PA1,PA2,PA3 for feedback
*/
#include <adafruit_feather.h>
#include <adafruit_http.h>
#include <iwdg.h>
#define WLAN_SSID "MySSID"
#define WLAN_PASS "MyPassword"
#define SERVER "wifitest.adafruit.com" // The TCP server to connect to
#define PAGE "/testwifi/index.html" // The HTTP resource to request
#define PORT 80 // The TCP port to use
#define USER_AGENT_HEADER "curl/7.45.0"
// RGB LED for feedback
int LED_RED_PIN = PA1;
int LED_GREEN_PIN = PA3;
int LED_BLUE_PIN = PA2;
bool setup_rgb = false;
AdafruitHTTP http;
int counter = 0;
// turn on to recover after hang
boolean useWDT = false;
void setup() {
Serial.begin(115200);
// The only time the LED is blue is on startup/restart
// If we ever see a blue LED after starting up the first
// time, it means that the watchdog kicked in to restart
setRGB(0,0,1);
Serial.println("HTTP Get Example (Callback Based)\r\n");
// Try to connect to an AP
while ( !connectAP() )
{
delay(500); // delay between each attempt
}
setRGB(0,1,0); // when connected, show green color
// Connected: Print network info
Feather.printNetwork();
// Print all software versions
Feather.printVersions();
Feather.setDisconnectCallback(disconnect_feather_callback);
// Tell the HTTP client to auto print error codes and halt on errors
http.err_actions(true, false);
// Set the callback handlers
http.setReceivedCallback(receive_callback);
http.setDisconnectCallback(disconnect_callback);
if( useWDT )
{
iwdg_init(IWDG_PRE_128, 11500000);
}
}
void loop() {
if(counter == 1000000*5)
{
setRGB(1,0,1); // blink purple to show that we're sending
if( Feather.connected() == true ){
// Connect to the HTTP server
Serial.printf("Connecting to %s port %d ... ", SERVER, PORT);
http.connect(SERVER, PORT); // Will halt if an error occurs
Serial.println("OK");
// Setup the HTTP request with any required header entries
http.addHeader("User-Agent", USER_AGENT_HEADER);
http.addHeader("Accept", "text/html");
http.addHeader("Connection", "keep-alive");
boolean rez = http.get(PAGE); // Will halt if an error occurs
Serial.println("OK");
setRGB(0,1,0); // when connected, show green color
}
else if( Feather.connected() == false )
{
Serial.println("reconnect!");
setRGB(1,0,0); // when disconnected, show red color
connectAP();
}
counter = 0;
}
if( useWDT )
{
iwdg_feed();
}
counter++;
}
void receive_callback(void)
{
// If there are incoming bytes available
// from the server, read then print them:
while ( http.available() )
{
int c = http.read();
Serial.write( (isprint(c) || iscntrl(c)) ? ((char)c) : '.');
}
}
/**************************************************************************/
/*!
@brief TCP/HTTP disconnect callback
*/
/**************************************************************************/
void disconnect_callback(void)
{
Serial.println();
Serial.println("--------------------------");
Serial.println("DISCONNECTED HTTP CALLBACK");
Serial.println("--------------------------");
Serial.println();
}
void disconnect_feather_callback(void)
{
Serial.println();
Serial.println("-----------------------------");
Serial.println("DISCONNECTED FEATHER CALLBACK");
Serial.println("-----------------------------");
Serial.println();
// Alert the user we failed
setRGB(1,1,1);
delay(500);
if( useWDT )
{
// force a restart really quickly
iwdg_init(IWDG_PRE_128, 115);
while(1);
}
}
bool connectAP(void)
{
// Attempt to connect to an AP
Serial.print("Please wait while connecting to: '" WLAN_SSID "' ... ");
int errornum = 0;
if ( Feather.connect(WLAN_SSID, WLAN_PASS) )
{
Serial.println("Connected!");
delay(500);
errornum = 1;
}
else
{
errornum = Feather.errnum();
Serial.printf("Failed! %s (%d)", Feather.errstr(), errornum);
Serial.println();
if( errornum == 4 )
{
Feather.disconnect();
delay(500);
}
}
Serial.println();
return errornum;
}
void setRGB(int r, int g, int b)
{
if(!setup_rgb){
pinMode(LED_RED_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
pinMode(LED_BLUE_PIN, OUTPUT);
setup_rgb = true;
}
digitalWrite(LED_RED_PIN, r);
digitalWrite(LED_GREEN_PIN, g);
digitalWrite(LED_BLUE_PIN, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment