forked from mdopp/simple-sonoff-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonoff.setupdevice.js
89 lines (80 loc) · 3.07 KB
/
sonoff.setupdevice.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
const http = require('http');
const url = require('url');
const fs = require('fs');
var request = require('request');
var WiFiControl = require('wifi-control');
//config parameters for SONOFF
var apSSID = "ITEAD-10";
var apPWD = "12345678";
config = JSON.parse(fs.readFileSync('./sonoff.config.json'));
// Initialize wifi-control package with verbose output
WiFiControl.init({
debug: true
});
//initialize the SONOFF after it has been found
// - set the wlan-network that the sonoff should use
// - set server to which the sonoff should connect (instead of its original cloud)
var _initDevice = () => {
http.get('http://10.10.7.1/device', (res) => {
if (res.statusCode !== 200) {
console.log('Unable to connect to the target device. Code: ' + res.statusCode);
res.resume();
return;
}
res.setEncoding('utf8');
var data = '';
res.on('data', (c) => data += c);
res.on('end', () => {
var response = JSON.parse(data);
var device = {
deviceid: response.deviceid,
apikey: response.apikey
};
console.log('device: ' + JSON.stringify(device));
//send configuration to device, so that it will use that server as its cloud
request.post('http://10.10.7.1/ap', {
json: true, body: {
"version": 4,
"ssid": config.router.SSID,
"password": config.router.password,
"serverName": config.server.IP,
"port": config.server.httpsPort
}
}, (err, response, body) => {
if (!err && response.statusCode == 200) {
console.log(JSON.stringify(response) + "\t" + body);
}
else {
console.log('Unable to configure endpoint ' + err);
}
});
});
}).on('error', (e) => {
console.log(`Unable to establish connection to the device: ${e}`);
});
};
// ----------------------------------------------------------------
// run .....
// - scan for SONOFF wlan = ITEAD-100xxxxxx
// - connect to SONOFF
// - setup SONOFF to use local PC as cloud
// -----------------------------------------------------------------
var find = setInterval(() => {
WiFiControl.scanForWiFi(function (err, response) {
if (err) console.log(err);
var apNet = response.networks.find(n => n.ssid.startsWith(apSSID));
if (!apNet) {
console.log('ERR | ' + Date.now() + ' | Sonoff is not in pairing mode. Please, Long press until led start blinking fast.');
} else {
console.log('OK | Sonoff found in pairing mode.');
apSSID = apNet.ssid;
clearInterval(find);
apNet.password = apPWD;
WiFiControl.connectToAP(apNet, function (err, response) {
if (err) console.log(err);
console.log('OK | Sonoff paired.', response);
_initDevice();
});
}
});
}, 500);