我正在研制DHT11温湿度传感器。我使用MQTT作为我的传输协议,一切看起来都很好。但是,我在MongoDB中存储数据时遇到了一些小问题。MQTT代理会看到已发布的消息,但我似乎无法在数据库中找到传感器数据。我在MongoDB中创建了这个集合,但不幸的是它没有收集任何数据。
var mqtt = require('mqtt'); //includes mqtt server
var mongodb = require('mongodb'); // includes mongoDB
var mongodbClient = mongodb.MongoClient; //initialises the mongoDB client
var mongodbURI = 'mongodb://localhost:27017/local'; //activating the MongoDB port 27017, here local is the name of the database
var deviceRoot = "demo/status/temperature"; //deviceroot is topic name given in arduino code
var collection,client; //initialise collection and client
mongodbClient.connect(mongodbURI, setupCollection); //connect the database with collecion
function setupCollection(err, db) {
if(err) throw err;
collection=db.collection(test_mqtt); //name of the collection in the database
client=mqtt.connect({ host: 'localhost', port: 1883 }); //connecting the mqtt server with the MongoDB database
client.subscribe(deviceRoot+"+"); //subscribing to the topic name
client.on('message', insertEvent); //inserting the event
}
//function that displays the data in the MongoDataBase
function insertEvent(topic,message) {
var key=topic.replace(deviceRoot,'');
collection.update(
{ _id:key },
{ $push: { events: { event: { value:message, when:new Date() } } } },
{ upsert:true },
function(err,docs) {
if(err) {
console.log("Insert fail");// Improve error handling
}
}
);
}如果我能得到任何帮助,我将不胜感激。
发布于 2017-04-19 02:46:06
您使用的MQTT库是客户端。您需要在服务器上运行的代理。Mosca服务器是一个好的,它支持MongoDB和WebSockets的开箱即用。这是我目前在IOT项目中使用的。
典型的设置如下所示:
const mosca = require('mosca');
function setupMqttServer() {
var mongoUrl = "mongodb://127.0.0.1:27017/mqtt";
var moscaSettings = {
port: 1883,
backend: {
type: 'mongo',
url: mongoUrl,
pubsubCollection: 'moscadata',
mongo: {}
},
persistence: {
factory: mosca.persistence.Mongo,
url: mongoUrl
}
};
this.server = new mosca.Server(moscaSettings);
this.server.on('ready', function () {
console.log('Mosca server is up and running');
});
this.server.on('clientConnected', function (client) {
console.log('client connected', client.id);
});
this.server.on('published', (packet, client) => {
console.log('Message received : ', `topic-${packet.topic}`, `payload-${packet.payload.toString()}`);
// You can process received message here.
});
this.server.on('subscribed', function (topic, client) {
console.log('subscribed : ', topic);
});
this.server.on('unsubscribed', (topic, client) => {
console.log('unsubscribed : ', topic);
});
this.server.on('clientDisconnecting', (client) => {
console.log('clientDisconnecting : ', client.id);
});
this.server.on('clientDisconnected', (client) => {
console.log('clientDisconnected : ', client.id);
});
}我还使用mqtt-regex来解析主题。
发布于 2016-12-18 14:03:01
mqtt.connect()是异步的,因此在调用client.subscribe()时客户端还没有连接。
您需要添加一个client.on('connect')回调并将订阅代码放入其中。
...
client=mqtt.connect({ host: 'localhost', port: 1883 }); //connecting the mqtt server with the MongoDB database
client.on('connect',function(){
client.subscribe(deviceRoot+"+");
});
client.on('message',insertEvent);
...https://stackoverflow.com/questions/41205591
复制相似问题