私はDHT11温度湿度センサーで作業しています。私は転送プロトコルとしてMQTTを使用していますが、すべてが大丈夫です。しかし、私はMongoDBにデータを格納するのに少し問題があります。 MQTTブローカーは公開されたメッセージを見ますが、私のデータベースでセンサー・データを見つけることができません。私はMongoDBでコレクションを作成しましたが、残念ながらデータは収集されません。Arduino 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
}
}
);
}
私は何か助けていただきありがとうございます。
データを挿入したいのですか、既にコレクションに含まれているデータを変更しようとしていますか? – Mike
コレクションは空です。 MQTTからMongoDBに公開されたセンサーからの温度の読み取り値を格納するためのデータが必要です。たぶん私は私のコレクションの権利を作成していないでしょう。よく分かりません。 – Ekom