Node, gateway, mqtt, nodejs to mongodb

Just got a node working with a short node.js script to read the data from the mqtt queue and save it in a mongodb collection. Pics of the dead bug analog temp sensor, an arduino pro-mini (which LMiC TTN demo fills to 98% of program capacity!) with RFM95W module in the cap of a pill box:


node.js script to stuff the temp reading in a mongodb collection:

var mqtt=require(‘/usr/lib/node_modules/mqtt’)
var mongodb=require(‘/usr/lib/node_modules/mongodb’);
var mongodbClient=mongodb.MongoClient;
var mongodbURI=‘mongodb://mqtt:password@192.168.1.75:27017/data’

function insertEvent(topic,payload) {
mongodbClient.connect(mongodbURI, function(err,db) {
if(err) { console.log(err); return; }
else {
var obj = JSON.parse(payload.toString());
var buf = new Buffer(obj[‘data’], ‘base64’);
var key = topic.replace(///g,‘_’);
collection = db.collection(key);
collection.update(
{ _id:key },
{ $push: { events: { event: { value:buf.toString(), when:new Date() } } } },
{ upsert:true },
function(err,docs) {
if(err) { console.log(“Insert fail” + err); } // Improve error handling
else { console.log(“Update callback - closing db”);
db.close() }
});
}
});
}

client=mqtt.connect(‘mqtt://croft.thethings.girovito.nl’)
client.on(‘connect’,function() {
// client.subscribe(‘gateways/801F02FFFF7E56D1/status’);
client.subscribe(‘nodes/02E00202/packets’);
});

client.on(‘message’,insertEvent);

2 Likes

nice… with a GP antenna… that’s different :sunglasses:

@cswiger, very nice antenna indeed!

Did you pack a battery in the bottle?

No but should - just alligator clips for various power sources. Bucking regulator for max efficiency, about 3mA resting current. There’s a red led always on the Arduino pro-mini I’m tempted to remove!

2 Likes

Mounted on a Flying Carpet :joy:

1 Like