2017-01-15 14 views
0

現在、私のスケッチはトピックごとに1つのセンサー値を発行します。私は2つのメッセージのトピック私は私のMongoDBに保存するには、このスケッチを好きになるでしょうアルドゥイーノMQTTはトピックに2つの値を公開します

void loop() 
{ 
    if (!client.connected()) { 
    reconnect(); 
    } 
    client.loop(); 

int chk = DHT.read11(DHT11_PIN); 
    int t = DHT.temperature; 
    int h = DHT.humidity; 

    char buffer[10]; 
    dtostrf(t,0, 0, buffer); 
    client.publish("Sensor/Temperature", buffer); 
    Serial.println(buffer); 
    dtostrf(h,0, 0, buffer); 
    client.publish("Sensor/Humidity",buffer); 
    delay(1000); 
} 

thisなどを公開するようになります。現在、私は1つのトピックと1つのメッセージしか受け付けません。

server.JS

client.on('message', function (topic, message) { 
     var messageObject = { 
      topic: topic, 
      message: message.toString(), 
      Time: new Date() 
     }; 

     collection.insert(messageObject, function(error, result) { 
      if(error != null) { 
       console.log("ERROR: " + error); 
      } 
     }); 
    }); 

は、どのように私は私のArduinoからの単一のトピックに2つのセンサ値をプッシュすることができますか?

ご協力いただきありがとうございます。

+0

あなたが提供したコード例と間違っている何疑問や問題の説明をここで – hardillb

+0

@hardillb更新後の – Ekom

+0

があるように縫い目はありませんか? – hardillb

答えて

0

私はこれに非常に適した解決策を見つけました。うまくいけば、誰かを助けてくれるだろ

void loop() 
{ 
    if (!client.connected()) { 
    reconnect(); 
    } 
    client.loop(); 

int chk = DHT.read11(DHT11_PIN); 
    int t = DHT.temperature; 
    int h = DHT.humidity; 
    snprintf (msg, 75, "temperature %d humidity %d ledStatus %s", t,h,ledStatus); 
    Serial.print("Publish message: "); 
    Serial.println(msg); 
    client.publish("outTopic", msg); 
    delay(6000); 
} 
関連する問題