Skip to content

Instantly share code, notes, and snippets.

@myconode
Last active September 16, 2015 03:20
Show Gist options
  • Save myconode/a50a89c78d3c87eba738 to your computer and use it in GitHub Desktop.
Save myconode/a50a89c78d3c87eba738 to your computer and use it in GitHub Desktop.
Tessel Wifi & Network Test
// Tessel Wifi & Network Test
// Author: Daniel Christopher <thelostspore@gmail.com>
// Public Domain
//
// 1- Attempt to connect to Internet, retry as necessary
// 2- Make an HTTP GET
//
// Combines Tessel [wifi examples](http://start.tessel.io/wifi)
// Wifi Access
var network = '#####'; // put in your network name here
var netpass = '#####'; // put in your password here, or leave blank ('') for unsecured
var security = 'wpa2'; // other options are 'wep', 'wpa', or 'unsecured'
// Tessel Firmware
var wifi = require('wifi-cc3000');
// Node built-in
var http = require('http');
// References
var timeouts = 0;
var power_cycles = 0;
var connection_attempts = 0;
function connect(){
// Only make connection attempt when necessary
if (!wifi.isConnected()){
console.log("\nAttempt to Connect, #" + (++connection_attempts));
wifi.connect({
security : security,
ssid : network,
password : netpass,
timeout : 24 // seconds
});
// wait an additional few seconds because the wifi chip does not appear to
// to be abiding by the timeout set in the wifi object
delay(10000);
}else{
console.log("\nAlready Connected!\n");
}
}
// Initial attempt
connect();
// wifi event-handler registration
wifi.on('connect', function(data){
console.log("Wifi connected", data);
// reset on success
connection_attempts = 0;
// http://httpstat.us/ is a service for generating HTTP codes
// Here, make request to see if Tessel is online
var statusCode = 200;
http.get("http://httpstat.us/" + statusCode, function (res) {
var buffer = [];
// response callback
res.on('data', function (data) {
// success
buffer.push(new Buffer(data));
console.log('Received: ', new Buffer(data).toString());
return;
});
}).on('error', function (e) {
console.log("HTTP Error: ");
console.log(e.message);
return;
});
// Terminate
return;
});
wifi.on('disconnect', function(data){
console.log("Wifi disconnected", data);
// reset and reconnect
powerCycle();
})
wifi.on('timeout', function(err){
// tried to connect but couldn't, retry
console.log("Wifi time out, #" + (++timeouts));
if (timeouts > 2) {
// reset the wifi chip
powerCycle();
} else {
// try to reconnect
connect();
}
});
wifi.on('error', function(err){
// one of the following happened
// 1. tried to disconnect while not connected
// 2. tried to disconnect while in the middle of trying to connect
// 3. tried to initialize a connection without first waiting for a timeout or a disconnect
console.log("error emitted", err);
// do not give up
powerCycle();
// ...unless we've tried 10+ times
// See Troubleshooting section
if(power_cycles > 10){
console.log(err);
console.log("\n More than 10 failed attempts.");
console.log("See Troubleshooting section.");
return;
}
});
// reset the wifi chip progammatically
function powerCycle(){
// when wifi chip resets, it tries to reconnect to the last saved network
console.log("\nPower cycling, #" + (++power_cycles));
wifi.reset(function(){
console.log("Power cycling complete\n");
timeouts = 0; // reset timeouts
// wait for a bit
delay(10000);
// attempt to connect if necessary
if (!wifi.isConnected()){ connect(); }
});
}
// - lite version of [ErikDubbelboer's sleep](github.com/ErikDubbelboer/node-sleep)
function delay(ms) { var e = new Date().getTime() + (ms); while (new Date().getTime() <= e) {; }; }
// Troubleshooting Wifi
// --------------------
//
// Check to see if Tessel is connected to wifi using Tessel CLI, `tessel wifi -l`.
// If there is no IP address, the Tessel is not connected to Wifi.
//
// Try power cycling your Tessel and then run the tessel wifi connection command again.
//
// Remove saved wifi profiles => `tessel stop; tessel wifi -d`
//
// Move closer to the router.
//
// Reset the router.
//
// Make sure the router has b/g mode enabled and isn't using channels 12, 13, or 14.
//
// https://github.com/tessel/t1-docs/blob/master/wifi.md#connecting-to-wifi-from-js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment