-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.js
200 lines (149 loc) · 4.39 KB
/
webserver.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// required modules
var http = require('http').createServer(handler);
var fs = require('fs');
var io = require('socket.io')(http)
// required module for sending code trough pins and via RF Module
var rpi433 = require('rpi-433-v3'),
rfSniffer = rpi433.sniffer({
pin: 2, //Sniff on GPIO 2 (or Physical PIN 13)
debounceDelay: 500 //Wait 500ms before reading another code
}),
rfEmitter = rpi433.emitter({
pin: 0, //Send through GPIO 0 (or Physical PIN 11)
pulseLength: 322 //Send the code with a 350 pulse length
});
http.listen(8433); // Server listens to port 8080
// Handles request from client, sends index.html
function handler(req, res) {
fs.readFile(__dirname + '/index.html', function (err, data) {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/html' });
return res.end("404 Not Found");
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
return res.end();
});
}
// object containing on and off codes from it's receivers
// the codes were sniffed with the rfSniffer module at the top by pressing the buttons on the remote
var sockets = {
a: {
on: '1115473',
off: '1115476'
},
b: {
on: '1118545',
off: '1118548'
},
c: {
on: '1119313',
off: '1119316'
},
d: {
on: '1119505',
off: '1119508'
},
e: {
on: '1119553',
off: '1119556'
},
f: {
on: '1131857',
off: '1131860'
}
};
// Astablishes a socket connection with client
io.sockets.on('connection', function (socket) {
var switcher = false;
// A receiver -----------------------------------------------------
socket.on('a_switcher', function (data) {
switcher = data;
if (switcher) {
// if true, turn on
rfEmitter.sendCode(sockets.a.on, function (error, stdout) {
if (!error) console.log(stdout);
});
} else {
// if false, turn off
rfEmitter.sendCode(sockets.a.off, function (error, stdout) {
if (!error) console.log(stdout);
});
}
});
// B receiver -----------------------------------------------------
socket.on('b_switcher', function (data) {
switcher = data;
if (switcher) {
// if true, turn on
rfEmitter.sendCode(sockets.b.on, function (error, stdout) {
if (!error) console.log(stdout);
});
} else {
// if false, turn off
rfEmitter.sendCode(sockets.b.off, function (error, stdout) {
if (!error) console.log(stdout);
});
}
});
// C receiver -----------------------------------------------------
socket.on('c_switcher', function (data) {
switcher = data;
if (switcher) {
// if true, turn on
rfEmitter.sendCode(sockets.c.on, function (error, stdout) {
if (!error) console.log(stdout);
});
} else {
// if false, turn off
rfEmitter.sendCode(sockets.c.off, function (error, stdout) {
if (!error) console.log(stdout);
});
}
});
// D receiver -----------------------------------------------------
socket.on('d_switcher', function (data) {
switcher = data;
if (switcher) {
// if true, turn on
rfEmitter.sendCode(sockets.d.on, function (error, stdout) {
if (!error) console.log(stdout);
});
} else {
// if false, turn off
rfEmitter.sendCode(sockets.d.off, function (error, stdout) {
if (!error) console.log(stdout);
});
}
});
// E receiver -----------------------------------------------------
socket.on('e_switcher', function (data) {
switcher = data;
if (switcher) {
// if true, turn on
rfEmitter.sendCode(sockets.e.on, function (error, stdout) {
if (!error) console.log(stdout);
});
} else {
// if false, turn off
rfEmitter.sendCode(sockets.e.off, function (error, stdout) {
if (!error) console.log(stdout);
});
}
});
// F receiver -----------------------------------------------------
socket.on('f_switcher', function (data) {
switcher = data;
if (switcher) {
// if true, turn on
rfEmitter.sendCode(sockets.f.on, function (error, stdout) {
if (!error) console.log(stdout);
});
} else {
// if false, turn off
rfEmitter.sendCode(sockets.f.off, function (error, stdout) {
if (!error) console.log(stdout);
});
}
});
});