Skip to content

Commit

Permalink
example: Add HTTP to MQTT bridge
Browse files Browse the repository at this point in the history
It was used along luftdaten sensor, using custom API using endpoint:

    /mqtt/broker.hivemq.com/1883/luftdaten.info,sensors,0

    mosquitto_sub -v -h broker.hivemq.com -p 1883 -t luftdaten.info,sensors,0
    {... ,"sensordatavalues":[{ ... "value":"42.00" ...

If needed replace broker/port, with your sensorid (not 0):

https://meine.luftdaten.info/sensors# (# : it's a number)

Note that for now only IoT.js runtime is supported in this example.

Relate-to: opendata-stuttgart/sensors-software#33
Change-Id: I8b75cc0311a5dac4c05b697ea32c17a1e3f18a60
Signed-off-by: Philippe Coval <p.coval@samsung.com>
  • Loading branch information
rzr committed Jun 18, 2019
1 parent 60ef06c commit 0ef4de5
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions example/server-mqtt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* -*- mode: js; js-indent-level:2; -*-
* SPDX-License-Identifier: MIT
*
* Copyright 2018-present Samsung Electronics France SAS, and other contributors
*
* This Source Code Form is subject to the terms of the MIT Licence
* If a copy of the MIT was not distributed with this file
* You can obtain one at:
* https://spdx.org/licenses/MIT.html
*/

var console = require('console');
// Disable logs here by editing to '!console.log'
var verbose = !console.log || function () {};

var Express = null;
try {
Express = require('../iotjs-express');
} catch (err) {
Express = require('iotjs-express');
}

var mqtt = require('mqtt');

function publish(options, topic, message) {
if (typeof message === 'object') {
message = JSON.stringify(message);
}
this.client = new mqtt.connect(options, function () {
this.publish(topic, message);
verbose(message);
});
}

function App() {
var http = require('http');
var port = 8888;
if (process.argv[2]) {
port = Number(process.argv[2]);
}
var app = new Express();
var self = http.createServer(app.request);
console.log('Listening on:\nhttp://localhost:' + port);

app.get('/.well-known/security.txt', function(req, res) {
return res.end('Contact: https://www.npmjs.com/~rzr\n');
});

app.get('/', function(req, res) {
res.json({Usage: '/mqtt/:host/:port/:topic/:message'});
});

app.get('/mqtt/:host/:port/:topic/:message', function(req, res) {
publish({
host: req.params.host,
port: Number(req.params.port)
}, req.params.topic, req.params.message);

return res.json({topic: req.params.topic});
});

app.post('/mqtt/:host/:port/:topic', function(req, res) {
publish({
host: req.params.host,
port: Number(req.params.port)
}, req.params.topic, req.body);

return res.json({topic: req.params.topic});
});

app.put('/mqtt/:host/:port/:topic', function(req, res) {
publish({
host: req.params.host,
port: Number(req.params.port)
}, req.params.topic, req.body);

return res.json({topic: req.params.topic});
});

self.listen(port);
}

// Main App
if (module.parent === null) {
App();
}

0 comments on commit 0ef4de5

Please sign in to comment.