admin管理员组

文章数量:1305370

I'm playing around with MQTT and MQTT.js. I have running a MQTT broker and now I want to subscribe to multiple topics. One topic is no problem, but multiple.

I have these two topics:

'sensor/esp8266-1/humidity'
'sensor/esp8266-1/temperature'

and I with this piece of code I subscribe on these two topics

var mqtt = require('mqtt');
var client  = mqtt.connect('mqtt://10.0.0.18');


client.subscribe('sensor/esp8266-1/humidity');
client.subscribe('sensor/esp8266-1/temperature');

client.on('message', function(topic, message, packet) {
    console.log(packet)
});

With this code console.log returns me the following

Packet {
  cmd: 'publish',
  retain: false,
  qos: 0,
  dup: false,
  length: 35,
  topic: 'sensor/esp8266-1/temperature',
  payload: <Buffer 32 31 2e 32 30> }
Packet {
  cmd: 'publish',
  retain: false,
  qos: 0,
  dup: false,
  length: 32,
  topic: 'sensor/esp8266-1/humidity',
  payload: <Buffer 34 31 2e 30 30> }

This looks at the first very good, but how can I get the temperature/humidity data from that?

I tried it with this

console.log(packet.payload.toString())

But now I got every time temperature and humidty without which I know what number means.

In the End I would like to get two variables (temperature/humidity) fill with the right data. Later I want to concat the two variables and store these to an SQL Database.

I'm playing around with MQTT and MQTT.js. I have running a MQTT broker and now I want to subscribe to multiple topics. One topic is no problem, but multiple.

I have these two topics:

'sensor/esp8266-1/humidity'
'sensor/esp8266-1/temperature'

and I with this piece of code I subscribe on these two topics

var mqtt = require('mqtt');
var client  = mqtt.connect('mqtt://10.0.0.18');


client.subscribe('sensor/esp8266-1/humidity');
client.subscribe('sensor/esp8266-1/temperature');

client.on('message', function(topic, message, packet) {
    console.log(packet)
});

With this code console.log returns me the following

Packet {
  cmd: 'publish',
  retain: false,
  qos: 0,
  dup: false,
  length: 35,
  topic: 'sensor/esp8266-1/temperature',
  payload: <Buffer 32 31 2e 32 30> }
Packet {
  cmd: 'publish',
  retain: false,
  qos: 0,
  dup: false,
  length: 32,
  topic: 'sensor/esp8266-1/humidity',
  payload: <Buffer 34 31 2e 30 30> }

This looks at the first very good, but how can I get the temperature/humidity data from that?

I tried it with this

console.log(packet.payload.toString())

But now I got every time temperature and humidty without which I know what number means.

In the End I would like to get two variables (temperature/humidity) fill with the right data. Later I want to concat the two variables and store these to an SQL Database.

Share Improve this question edited Nov 8, 2017 at 17:43 greenchapter asked Nov 8, 2017 at 17:14 greenchaptergreenchapter 1361 gold badge2 silver badges13 bronze badges 7
  • What do you mean by "code is not very functionable." The code subscribes to 2 topics, which is exactly what you asked it to do – hardillb Commented Nov 8, 2017 at 17:21
  • Also have you read the doc? It is very clear on how to subscribe to multiple topics and even offers 3 different ways to do it – hardillb Commented Nov 8, 2017 at 17:23
  • Yes but I cant find something, where do you find that? – greenchapter Commented Nov 8, 2017 at 17:25
  • npmjs./package/… and you haven't answered the first question – hardillb Commented Nov 8, 2017 at 17:27
  • Yes I have seen this, but how can I read every single payload package? – greenchapter Commented Nov 8, 2017 at 17:30
 |  Show 2 more ments

1 Answer 1

Reset to default 7

You've not said how you want to uses these 2 values but the following is the simplest way to start.

var mqtt = require('mqtt');
var client  = mqtt.connect('mqtt://10.0.0.18');

var temperature;
var humidity;

client.subscribe('sensor/esp8266-1/humidity');
client.subscribe('sensor/esp8266-1/temperature');

client.on('message', function(topic, message, packet) {
  if (topic === 'sensor/esp8266-1/temperature') {
    temperature = message;
  }

  if (topic === 'sensor/esp8266-1/humidity') {
    humidity = message;
  }
});

You can make it a little simpler by using a single wildcard subscription:

client.subscribe('sensor/esp8266-1/+');

Which will subscribe to all topics that start with sensor/esp8266-1/

EDIT: Now we have finally thrashed out what you wanted to ask (NOT CLEAR IN THE QUESTION)

client.on('message', function(topic, message, packet) {
  if (topic === 'sensor/esp8266-1/temperature') {
    temperature = message;
  }

  if (topic === 'sensor/esp8266-1/humidity') {
    humidity = message;
  }

  if (temperature && humidity) {
     //do database update or print
     console.log("----");
     console.log("temp: %s", temperature);
     console.log("----");
     console.log("humidity: %s", humidity);
     //reset to undefined for next time
     temperature = undefined;
     humidity = undefined;
  }
});

本文标签: javascriptMQTTjs multiple subscriptionStack Overflow