basic mqtt/http bridge and call from minetest Lua
parent
c43fa52b95
commit
6defd4ada1
|
@ -0,0 +1,2 @@
|
||||||
|
server/node_modules
|
||||||
|
server/db
|
4
init.lua
4
init.lua
|
@ -8,9 +8,9 @@ minetest.register_node("connected:iotswitch", {
|
||||||
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
|
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
|
||||||
on_rightclick = function (pos, node)
|
on_rightclick = function (pos, node)
|
||||||
print("punch")
|
print("punch")
|
||||||
minetest.after(5, function()
|
minetest.after(1, function()
|
||||||
http_api.fetch({
|
http_api.fetch({
|
||||||
url = "http://localhost:3000/resources/feedsonoff",
|
url = "http://localhost:8088/punch",
|
||||||
timeout = 1
|
timeout = 1
|
||||||
},
|
},
|
||||||
function (res)
|
function (res)
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"name": "connectedJS",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.14.0",
|
||||||
|
"mqtt": "^2.0.1",
|
||||||
|
"ponte": "~0.0.16"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
var mosca = require('mosca')
|
||||||
|
|
||||||
|
var moscaSettings = {
|
||||||
|
port: 1883
|
||||||
|
};
|
||||||
|
|
||||||
|
var server = new mosca.Server(moscaSettings);
|
||||||
|
server.on('ready', setup);
|
||||||
|
|
||||||
|
server.on('clientConnected', function(client) {
|
||||||
|
console.log('client connected', client.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
// fired when a message is received
|
||||||
|
server.on('published', function(packet, client) {
|
||||||
|
console.log('Published', packet.payload);
|
||||||
|
});
|
||||||
|
|
||||||
|
// fired when the mqtt server is ready
|
||||||
|
function setup() {
|
||||||
|
console.log('Mosca server is up and running')
|
||||||
|
}
|
||||||
|
|
||||||
|
var mqtt = require('mqtt')
|
||||||
|
//var client = mqtt.connect()
|
||||||
|
var client = mqtt.connect({ port: 1883, host: '127.0.0.1', keepalive: 10000});
|
||||||
|
|
||||||
|
var express = require('express');
|
||||||
|
var app = express();
|
||||||
|
|
||||||
|
app.get('/punch', function (req, res) {
|
||||||
|
res.send('Punch!!!');
|
||||||
|
client.publish('punch', 'message root punch')
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(8088, function () {
|
||||||
|
console.log('Example app listening on port 8088!');
|
||||||
|
});
|
Loading…
Reference in New Issue