Commit 7acaa4e3 authored by Max-Ferdinand Suffel's avatar Max-Ferdinand Suffel
Browse files

add Tessel code for SoundBrain

parent 67e4d335
var net = require('net');
var host = '69.91.157.20'; // Use tessel wifi -l in order to get the latest host IP.
var port = 6000;
var client = new net.Socket();
client.connect('6000', host, function () {
console.log('Connected to: ' + host + ':' + port);
});
client.on('data', function(data) {
console.log('data: ' + data);
});
client.on('close', function() {
console.log('Connection closed');
});
\ No newline at end of file
{
"name": "TesselSoundBrain",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
// TCP server stuff
var net = require('net');
var server;
var sockets = {}, nextSocketId = 0;
// Wifi stuff
var wifi = require('wifi-cc3000');
var network = 'University of Washington';
var pass = '';
var security = 'unsecured';
var timeouts = 0;
function connect(){
wifi.connect({
security: security
, ssid: network
, password: pass
, timeout: 120 // 120 seconds
});
}
wifi.on('connect', function(data){
console.log("Wifi connection established", data);
if (data.ssid) {
server = net.createServer();
server.listen(6000);
console.log('Server listening on ' + server.address().address + ': '+ server.address().port);
server.on('connection', function (sock) {
console.log('Connected: ' + sock.remoteAddress + ':' + sock.remotePort);
sock.on('data', function(data) {
console.log('Data ' + sock.remoteAddress + ':' + data);
});
sock.on('close', function(data) {
console.log('Closed: ' + sock.remoteAddress + ':' + sock.remotePort);
});
});
}
});
wifi.on('disconnect', function(data) {
console.log("Wifi connection destroyed", data);
if (server) server.close();
powerCycle();
})
wifi.on('timeout', function(err){
// tried to connect but couldn't, retry
console.log("Wifi connection timeout, try to reconnect");
if (server) server.close();
timeouts++;
if (timeouts > 2) {
// reset the wifi chip if we've timed out too many times
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("Wifi connection error", err);
if (server) server.close();
});
// reset the wifi chip progammatically
function powerCycle(){
// when the wifi chip resets, it will automatically try to reconnect
// to the last saved network
wifi.reset(function(){
timeouts = 0; // reset timeouts
console.log("Wifi power cycling done");
// give it some time to auto reconnect
setTimeout(function(){
if (!wifi.isConnected()) {
// try to reconnect
connect();
}
}, 20 *1000); // 20 second wait
})
}
// Connect to Wifi and start server.
connect();
// server.on('connection', function (socket) {
// // Add a newly connected socket
// var socketId = nextSocketId++;
// sockets[socketId] = socket;
// console.log('socket', socketId, 'opened');
// // Remove the socket when it closes
// socket.on('close', function () {
// console.log('socket', socketId, 'closed');
// delete sockets[socketId];
// });
// // Extend socket lifetime for demo purposes
// socket.setTimeout(4000);
// });
// // Count down from 10 seconds
// (function countDown (counter) {
// console.log(counter);
// if (counter > 0)
// return setTimeout(countDown, 1000, counter - 1);
// // Close the server
// server.close(function () { console.log('Server closed!'); });
// // Destroy all open sockets
// for (var socketId in sockets) {
// console.log('socket', socketId, 'destroyed');
// sockets[socketId].destroy();
// }
// })(10);
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment