-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-server.js
45 lines (33 loc) · 973 Bytes
/
http-server.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
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://broker.hivemq.com:1883/');
var express = require("express");
var app = express();
/* serves main page */
app.get("/", function(req, res) {
res.send("connected");
});
app.post("/start", function(req, res) {
/* some server side logic */
res.send("OK");
client.publish('ctrl_hyperion_9127','start');
});
app.post("/stop", function(req, res) {
/* some server side logic */
res.send("OK");
client.publish('ctrl_hyperion_9127','stop');
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
client.on('connect', function () {
console.log("MQTT connected");
//client.subscribe('presence')
//client.publish('presence', 'Hello mqtt')
})
client.on('message', function (topic, message) {
console.log("MQTT message received:"+message);
// message is Buffer
//console.log(message.toString())
//client.end()
})