basic mqtt/http bridge and call from minetest Lua

master
Thierry Chantier 2016-10-01 23:39:35 +02:00
parent c43fa52b95
commit 6defd4ada1
4 changed files with 51 additions and 2 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
server/node_modules
server/db

View File

@ -8,9 +8,9 @@ minetest.register_node("connected:iotswitch", {
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
on_rightclick = function (pos, node)
print("punch")
minetest.after(5, function()
minetest.after(1, function()
http_api.fetch({
url = "http://localhost:3000/resources/feedsonoff",
url = "http://localhost:8088/punch",
timeout = 1
},
function (res)

9
server/package.json Normal file
View File

@ -0,0 +1,9 @@
{
"name": "connectedJS",
"version": "0.1.0",
"dependencies": {
"express": "^4.14.0",
"mqtt": "^2.0.1",
"ponte": "~0.0.16"
}
}

38
server/server.js Normal file
View File

@ -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!');
});