2016-12-18 13 views
0

私は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 
     } 
    } 

); 

} 

私は何か助けていただきありがとうございます。

+0

データを挿入したいのですか、既にコレクションに含まれているデータを変更しようとしていますか? – Mike

+0

コレクションは空です。 MQTTからMongoDBに公開されたセンサーからの温度の読み取り値を格納するためのデータが必要です。たぶん私は私のコレクションの権利を作成していないでしょう。よく分かりません。 – Ekom

答えて

0

使用しているMQTTライブラリはクライアントです。サーバー上で実行されているブローカーが必要です。 Mosca Serverは優れたものであり、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を使用しています。

0

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); 
... 
+0

私はちょうどそれを行ったが、何も変わっていない。コレクション 'test_mqtt'の作成から' mongo'を除いて実行する必要があるのですか? – Ekom

+0

あなたは変数 'test_mqtt'を宣言しているとは思われません。コレクションの名前が実際にtest_mqttであれば、引用符で囲む必要があります。 – hardillb

+0

私は最初に 'test_mqtt'を引用符で囲みました。違いはありませんでした。引用符を戻してください。まだ何も得ていない。 – Ekom