-
Notifications
You must be signed in to change notification settings - Fork 0
/
maincode.js
438 lines (362 loc) · 11.5 KB
/
maincode.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
console.log("BOOT UP");
const RELAY3 = D0;
const RELAY2 = D4;
const RELAY1 = D5;
console.log("CONST DONE");
// The last data that was POSTed to us
var postData = {};
/***
* _ _ _____ __ __ __
* /\ | \ | |_ _| \/ /_ |
* / \ | \| | | | | \ / || |
* / /\ \ | . ` | | | | |\/| || |
* / ____ \| |\ |_| |_| | | || |
* /_/ \_\_| \_|_____|_| |_||_|
*
*
*/
var demo1Steps = [
[1, 0, 0, 1950],
[0, 0, 0, 50],
[0, 0, 1, 1950],
[0, 0, 0, 50]
], currentStep = 0;
/***
* _ _ _____ __ __ ___
* /\ | \ | |_ _| \/ |__ \
* / \ | \| | | | | \ / | ) |
* / /\ \ | . ` | | | | |\/| | / /
* / ____ \| |\ |_| |_| | | |/ /_
* /_/ \_\_| \_|_____|_| |_|____|
*
*
*/
var demo2Steps = [
[0, 1, 0, 4000],
[1, 0, 1, 500],
[0, 0, 1, 500],
[1, 0, 1, 500],
[0, 0, 1, 500],
[1, 0, 1, 500],
[0, 0, 1, 500],
[1, 0, 1, 500],
[0, 0, 1, 500],
[1, 0, 1, 500],
[0, 0, 1, 500],
[1, 0, 1, 500],
[0, 0, 1, 500],
[1, 0, 1, 500],
[0, 0, 1, 500],
[1, 0, 1, 500],
[0, 0, 1, 500],
[1, 0, 1, 500],
[0, 0, 0, 500],
[1, 0, 1, 500],
[0, 0, 0, 500],
[1, 0, 1, 500],
[0, 0, 0, 500],
[1, 0, 1, 500],
[0, 0, 0, 500],
[1, 0, 1, 500],
[0, 0, 0, 50]
];
/***
* _ _ _____ __ __ ____
* /\ | \ | |_ _| \/ |___ \
* / \ | \| | | | | \ / | __) |
* / /\ \ | . ` | | | | |\/| ||__ <
* / ____ \| |\ |_| |_| | | |___) |
* /_/ \_\_| \_|_____|_| |_|____/
*
*
*/
var demo3Steps = [
[0, 0, 0, 100],
[1, 0, 0, 300],
[0, 1, 0, 300],
[0, 0, 1, 300],
[0, 1, 0, 300],
[1, 0, 0, 300],
[1, 0, 0, 200],
[0, 0, 0, 100],
[1, 0, 0, 200],
[1, 1, 0, 200],
[1, 0, 0, 100],
[1, 1, 0, 200],
[1, 1, 1, 200],
[1, 1, 0, 100],
[1, 1, 1, 200],
[0, 1, 1, 200],
[1, 1, 1, 100],
[0, 1, 1, 200],
[0, 0, 1, 200],
[0, 1, 1, 100],
[0, 0, 1, 200],
[0, 0, 0, 200],
[0, 0, 1, 100],
[0, 0, 0, 200],
];
var WIFI_IP = { ip: "192.168.0.200", gw: "192.168.0.1", netmask: "255.255.255.0"};
var wifi = require("Wifi"), connectTries = 0;
console.log("VAR DONE");
/***
* _ _ _____ __ __ __
* /\ | \ | |_ _| \/ /_ |
* / \ | \| | | | | \ / || |
* / /\ \ | . ` | | | | |\/| || |
* / ____ \| |\ |_| |_| | | || |
* /_/ \_\_| \_|_____|_| |_||_|
*
*
*/
function demo1Mode() {
if (postData.demo1) {
var step = demo1Steps[currentStep];
digitalWrite(RELAY1, !step[0]);
digitalWrite(RELAY2, !step[1]);
digitalWrite(RELAY3, !step[2]);
currentStep ++;
if (currentStep >= demo1Steps.length) {
currentStep = 0;
}
setTimeout(demo1Mode, step[3]);
}
}
/***
* _ _ _____ __ __ ___
* /\ | \ | |_ _| \/ |__ \
* / \ | \| | | | | \ / | ) |
* / /\ \ | . ` | | | | |\/| | / /
* / ____ \| |\ |_| |_| | | |/ /_
* /_/ \_\_| \_|_____|_| |_|____|
*
*
*/
function demo2Mode() {
if (postData.demo2) {
var step = demo2Steps[currentStep];
digitalWrite(RELAY1, !step[0]);
digitalWrite(RELAY2, !step[1]);
digitalWrite(RELAY3, !step[2]);
currentStep ++;
if (currentStep >= demo2Steps.length) {
currentStep = 0;
}
setTimeout(demo2Mode, step[3]);
}
}
/***
* _ _ _____ __ __ ____
* /\ | \ | |_ _| \/ |___ \
* / \ | \| | | | | \ / | __) |
* / /\ \ | . ` | | | | |\/| ||__ <
* / ____ \| |\ |_| |_| | | |___) |
* /_/ \_\_| \_|_____|_| |_|____/
*
*
*/
function demo3Mode() {
if (postData.demo3) {
var step = demo3Steps[currentStep];
digitalWrite(RELAY1, !step[0]);
digitalWrite(RELAY2, !step[1]);
digitalWrite(RELAY3, !step[2]);
currentStep ++;
if (currentStep >= demo3Steps.length) {
currentStep = 0;
}
setTimeout(demo3Mode, step[3]);
}
}
// This serves up the webpage itself
function sendPage(res) {
// We're using ES6 Template Literals here to make the HTML easy to read.
var d = `
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, shrink-to-fit=no">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="theme-color" content="#484848" />
<title>The Walking LED</title>
<!-- Font -->
<link href="https://fonts.googleapis.com/css?family=Roboto:400,500&display=swap&subset=latin-ext" rel="stylesheet">
<!-- CSS -->
<link href="https://adamvari.github.io/adamvari/lampa.css?v=0.01" rel="stylesheet">
<!-- Favicon -->
<link rel="shortcut icon" sizes="16x16 32x32" type="image/png" href="https://adamvari.github.io/adamvari/lamp.jpg"/>
</head>
<body class="container">
<div class="inner-wrapper">
<h3 class="title">The Walking LED</h3>
<div class="card">
<form action="#" method="post">
<div class="boxes">
<div class="input-wrapper">
<input type="checkbox" id="led1" class="green" name="led1" value="1" ${postData.led1?"checked":""}>
<label for="led1">The GREEN Lantern</label>
</div>
<div class="input-wrapper">
<input type="checkbox" id="led2" class="red" name="led2" value="1" ${postData.led2?"checked":""}>
<label for="led2">The RED Light District</label>
</div>
<div class="input-wrapper">
<input type="checkbox" id="led3" class="walk" name="led3" value="1" ${postData.led3?"checked":""}>
<label for="led3">WALK The Line</label>
</div>
<div class="input-wrapper">
<input type="checkbox" id="demo1" class="purple" name="demo1" value="1" ${postData.demo1?"checked":""}>
<label for="demo1">COMBO pt 1</label>
</div>
<div class="input-wrapper">
<input type="checkbox" id="demo2" class="purple" name="demo2" value="1" ${postData.demo2?"checked":""}>
<label for="demo2">COMBO pt 2</label>
</div>
<div class="input-wrapper">
<input type="checkbox" id="demo3" class="purple" name="demo3" value="1" ${postData.demo3?"checked":""}>
<label for="demo3">COMBO pt 3</label>
</div>
<div class="btn-wrapper">
<button class="btn">Let it be!</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>`;
res.writeHead(200, {'Content-Type': 'text/html', 'Content-Length':d.length});
res.end(d);
}
// This handles the HTTP request itself and serves up the webpage or a
// 404 not found page
function onPageRequest(req, res) {
var a = url.parse(req.url, true);
if (a.pathname=="/") {
// handle the '/' (root) page...
// If we had a POST, handle the data we're being given
if (req.method=="POST" &&
req.headers["Content-Type"]=="application/x-www-form-urlencoded")
handlePOST(req, function() { sendPage(res); });
else
sendPage(res);
} else {
// Page not found - return 404
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end("404: Page "+a.pathname+" not found");
}
}
// This handles any received data from the POST request
function handlePOST(req, callback) {
var data = "";
req.on('data', function(d) { data += d; });
req.on('end', function() {
// All data received from the client, so handle the url encoded data we got
// If we used 'close' then the HTTP request would have been closed and we
// would be unable to send the result page.
postData = {};
data.split("&").forEach(function(el) {
var els = el.split("=");
postData[els[0]] = decodeURIComponent(els[1]);
});
// finally our data is in postData
// console.log(postData);
// do stuff with it!
if (!postData.demo1 && !postData.demo2) {
digitalWrite(RELAY1, !postData.led1);
digitalWrite(RELAY2, !postData.led2);
digitalWrite(RELAY3, !postData.led3);
}
if (postData.demo1) {
currentStep = 0;
demo1Mode();
}
if (postData.demo2) {
currentStep = 0;
demo2Mode();
}
if (postData.demo3) {
currentStep = 0;
demo3Mode();
}
// call our callback (to send the HTML result)
callback();
});
}
/*
* __ _______ ______ _____ _____ _______ _ _ ______ ______
* \ \ / /_ _| ____|_ _| / ____|__ __| | | | ____| ____|
* \ \ /\ / / | | | |__ | | | (___ | | | | | | |__ | |__
* \ \/ \/ / | | | __| | | \___ \ | | | | | | __| | __|
* \ /\ / _| |_| | _| |_ ____) | | | | |__| | | | |
* \/ \/ |_____|_| |_____| |_____/ |_| \____/|_| |_|
*
*
*/
// This is called when we have an internet connection
function onConnected() {
console.log("SET WIFI STATIC IP!");
wifi.setIP(WIFI_IP, function(err) {
console.log(" CANT SET IP: ");
console.log(err);
});
console.log(" CONNECT to me at http://"+WIFI_IP.ip);
require("http").createServer(onPageRequest).listen(80);
}
console.log("SET WIFI EVENT HANDLER");
wifi.on('connected', onConnected);
console.log("SET WIFI EVENT HANDLER DONE");
// Feltöltés után manuálisan futtatni
// save(); require('ESP8266').reboot()
/*
These lines added by stenza 2020.03.22 during Covid-19 quarantine
Be van égetve wifi config a nonvolatile flash memory-ba. A neve ".wififcg"
----------------------------------------------------------------------------
Tartalma egy JSON:
{
ssid: "Fannet",
password: "Autotransfusio1",
mode: 1, phyMode: 3, sleepType: 2,
hostname: "lampa",
ssidAP: "",
passwordAP: "",
authmodeAP: 0, hiddenAP: 0, channelAP: 1 }
Illetve a .boot0 fájlban is el volt mentve:
require("Storage").write(".boot0", `
WIFI_NAME = "Fannet";
WIFI_PASS = "Autotransfusio1";
`);
Szóval a konfighoz ezt kell kiírni a console-on parancsba csak:
----------------------------------------------------------------------------
require("Storage").write(".wificfg", JSON.stringify({
ssid: "UPC4487795",
password: "2ts2unaEPcnu",
mode: 1, phyMode: 3, sleepType: 2,
hostname: "lampa",
ssidAP: "",
passwordAP: "",
authmodeAP: 0, hiddenAP: 0, channelAP: 1 }))
Esetleg törölni mindent előbb:
----------------------
require("Storage").erase(".wificfg");
require("Storage").erase(".boot0");
Egyéb infó:
----------------------
KIOLVASÁS:
require("Storage").list();
require("Storage").readJSON(".wificfg");
KIÍRÁS:
require("Storage").write(".wificfg", JSON.stringify({
ssid: "UPC4487795",
password: "2ts2unaEPcnu",
mode: 1, phyMode: 3, sleepType: 2,
hostname: "lampa",
ssidAP: "",
passwordAP: "",
authmodeAP: 0, hiddenAP: 0, channelAP: 1 }));
More info here:
https://www.espruino.com/Saving
*/