-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
130 lines (119 loc) · 3.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SET THESE VARIABLES
var siteToMonitor = 'https://tessel.io/';
var wifiSettings = {
ssid: "technicallyWifi", // Your wifi network
password: "scriptstick", // Your wifi password
timeout: 40
};
// Require node libraries
var tessel = require('tessel');
var relaylib = require('relay-mono');
var wifi = require('wifi-cc3000');
var http = require('http');
// Local vars
var timeouts = 0; // Counts wifi timeouts
// Connect hardware
var relay = relaylib.use(tessel.port['A']);
relay.on('ready', function relayReady () {
// Connect wifi
checkConnection();
});
// Main function: runs when everything is ready
function main() {
console.log('Ready!');
checkSite();
}
// Check status of website
function checkSite() {
console.log('Pinging website...');
http.get(siteToMonitor, function(res) {
console.log("Got response: " + res.statusCode);
// Turn light on/off according to result
if(res.statusCode == 200 || res.statusCode == 302) {
relay.turnOff(1, function (err) {
if(err) {
console.log(err);
systemError();
}
});
} else {
relay.turnOn(1, function (err) {
if(err) {
console.log(err);
systemError();
}
});
}
// Repeat every few seconds
setTimeout(checkSite, 10000);
}).on('error', function(e) {
console.log("Got error: " + e.message);
systemError();
});
}
function checkConnection () {
if (wifi.isConnected()) {
console.log('Connected.');
// When wifi is connected, run the main function
main();
} else {
console.log('Connecting...');
wifi.connect(wifiSettings, function (err, res) {
if(err) {
console.log('Error connecting:', err);
systemError();
}
// Try again
checkConnection();
});
}
}
// Automatic wifi handling
wifi.on('disconnect', function () {
console.log('Disconnected.');
systemError();
checkConnection();
});
wifi.on('timeout', function(err){
// Tried to connect but couldn't; retry
console.log("Timed out connecting to wifi");
timeouts++;
if (timeouts > 2) {
// Reset the wifi chip if we've timed out too many times
powerCycle();
} else {
// Try to reconnect
checkConnection();
}
});
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);
systemError();
});
// Reset the wifi chip progammatically
function powerCycle(){
// When the wifi chip resets, it will automatically try to reconnect to the last saved network
console.log('Cycling power...');
wifi.reset(function(){
timeouts = 0; // Reset timeouts
console.log("Done power cycling, waiting to reconnect...");
// Give it some time to auto reconnect
setTimeout(function(){
if (!wifi.isConnected()) {
// Try to reconnect
connect();
}
}, 20 *1000); // 20 second wait
});
}
// Whenever this system isn't working, flash the light
function systemError() {
relay.toggle(1);
setTimeout(function () {
relay.toggle(1);
}, 2000);
}