-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
59 lines (38 loc) · 1.17 KB
/
app.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
const mqtt = require("mqtt");
const express = require("express");
const client = mqtt.connect('mqtt://broker.hivemq.com')
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static( __dirname + "/public"));
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
});
let name;
let passwd;
app.post("/", function(req, res){
name = req.body.name;
passwd = req.body.passwd;
res.redirect("/main");
});
let command;
app.post("/main", function(req, res){
command = req.body.command;
if(command == "on" || command == "ON"){
client.publish("iot", "on");
res.send("LED STATE : ON");
}else if(command == "off" || command == "OFF"){
client.publish("iot", "off");
res.send("LED STATE : OFF");
}
});
app.get("/main", function(req, res){
if(passwd == "raspian1155"){
res.sendFile(__dirname + "/public/main.html");
}else{
res.write("Wrong password, YOU " + name);
res.send();
}
});
//Server listning...
app.listen(process.env.PORT || 8001);