Last active
September 10, 2018 14:15
-
-
Save ElectricImpSampleCode/a7c11ad21bf080cc4602 to your computer and use it in GitHub Desktop.
Demonstrates how you can use server.setsendtimeoutpolicy()’s RETURN_ON_ERROR option by using a server.onexpecteddisconnect() callback.
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
// Set the timeout policy to RETURN_ON_ERROR, ie. to continue running at disconnect | |
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 10); | |
// Configure Pin 1 to drive the connection status LED | |
hardware.pin1.configure(DIGITAL_OUT); | |
hardware.pin1.write(0); | |
// Create an array of WiFi networks, each defined by a table | |
connections <- [ | |
{ ssid = "SSID1", pw = "SSID1PW" }, | |
{ ssid = "SSID2", pw = "SSID2PW" } | |
]; | |
currentConnection <- 0; | |
function changeWifi(ssid, pw, callback) { | |
// Disconnect from current network (if required). | |
// Try connecting with the supplied ssid / pw | |
// Execute the callback | |
if (server.isconnected()) { | |
// Server is connected, so make sure all the pending data has been sent to the server... | |
server.flush(30); | |
// ... and disconnect from it | |
server.disconnect(); | |
} | |
// Re-configure the imp's WiFi and attempt to connect | |
imp.setwificonfiguration(ssid, pw); | |
server.connect(callback, 30); | |
} | |
// This function doesn't use the result parameter, but the parameter is required | |
// because the function is also the callback from a server.connect() | |
function connectOrFailToNextConnection(result = null) { | |
if (!server.isconnected()) { | |
// Server is not connected | |
if (currentConnection >= connections.len()) { | |
// We've tried all the available networks without success, | |
// so put the imp to sleep for 10 minutes | |
currentConnection = 0; | |
imp.deepsleepfor(600); | |
} else { | |
// If there are still connections to try, grab current ssid and pw, | |
// and increment currentConnection | |
// for the next attempt (if connection fails) | |
local ssid = connections[currentConnection].ssid; | |
local pw = connections[currentConnection].pw; | |
currentConnection++; | |
// Attempt to connect | |
changeWifi(ssid, pw, connectOrFailToNextConnection); | |
} | |
} else { | |
// The imp is connected, so turn the LED on | |
hardware.pin1.write(1); | |
} | |
} | |
// Define a function to handle disconnections | |
function disconnectHandler(reason) { | |
if (reason != SERVER_CONNECTED) { | |
// Turn LED off | |
hardware.pin1.write(0); | |
// Loop through connections until we connect | |
currentConnection = 0; | |
connectOrFailToNextConnection(); | |
} | |
} | |
// Register the disconnection handler | |
server.onunexpecteddisconnect(disconnectHandler); | |
// On boot, make sure we're connected or try connecting | |
connectOrFailToNextConnection(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment