Last active
July 25, 2017 12:51
-
-
Save jenschr/8ff93c0b6f7fd74ce6eefb0b3263c549 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 = PA2; | |
int LED_BLUE_PIN = PA3; | |
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"); | |
// Print all software versions | |
Feather.printVersions(); | |
// 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(); | |
Feather.setDisconnectCallback(disconnect_feather_callback); | |
// Tell the HTTP client to auto print error codes and halt on errors | |
http.err_actions(true, true); | |
// 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"); | |
delay(100); | |
setRGB(0,1,0); // when connected, show green color | |
} else { | |
Serial.println("reconnect!"); | |
http.flush(); | |
delay(100); | |
http.stop(); | |
delay(100); | |
Feather.disconnect(); | |
delay(100); | |
setRGB(1,0,0); // when disconnected, show red color | |
connectAP(); | |
} | |
counter = 0; | |
} | |
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(); | |
http.stop(); | |
} | |
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); | |
// 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 "' ... "); | |
if ( Feather.connect(WLAN_SSID, WLAN_PASS) ) | |
{ | |
Serial.println("Connected!"); | |
} | |
else | |
{ | |
Serial.printf("Failed! %s (%d)", Feather.errstr(), Feather.errnum()); | |
Serial.println(); | |
} | |
Serial.println(); | |
return Feather.connected(); | |
} | |
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