JustAnthony
JustAnthony

Reputation: 1

Multiple SUB in a mqtt JS file

So, im making a project for a subject, and i have a file (weather.js) that publishes two variables, one in local/temperature and another in local/time. I'm making a file that subscribes to both of them and operates with the values (blinds.js), but it mixes them. I send temperature and the hour at the same time, and in the subscriber it gives the 1st value to the temperature local variable (inside blinds.js) and then to the time local variable. What can i do?

Here's the weather.js file.

    //Variables
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://localhost:1884')
var topic_temp = 'local/temperature'
var topic_time = 'local/time'


//Publisher (topic)
client.on('connect',() => {
    setInterval(() => {
        console.log('--------------------');
        //creating random temperatures between -5 and 5
        var temperature = Math.floor((Math.random() * 30) + 5);
        var msg1 = temperature.toString();

        //sending the temperature values
        client.publish(topic_temp,msg1);
        console.log('Temperature is ' + msg1 + 'ºC');

        //Console log sent message
        console.log('Temperature sent!');

        setTimeout(function(){
            //creating random time value 24h format
            var time = Math.floor((Math.random() * 24));
            var msg2 = time.toString();

            //sending time value
            client.publish(topic_time,msg2);
            console.log('Time is ' + msg2 + 'h');

            //Console log sent message
            console.log('Time sent!');
            console.log('--------------------');
        }, 3000);


},15000)
})

And this is the blinds.js file.

//Variables
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://localhost:1884')

var topic_sub1 = 'local/temperature'
var msg1 = false;
var topic_sub2 = 'local/time'
msg2 = false;

var topic_post = 'appliances/blinds'

var blinds = false;

//Subscribing to the topic_sub1
client.on('connect',() => {
    client.subscribe(topic_sub1)
})

//Receiving temp messages
client.on('message',(topic_sub1,temperature) => {
    if (msg2 == false) {
        msg1 = true;
        temp = temperature.toString();
        console.log('--------------------');
        console.log(temp + 'ºC');
    }
})

//Subscribing to the topic_sub2
client.on('connect',() => {
    client.subscribe(topic_sub2)
})

//Receiving time messages
client.on('message',(topic_sub2,times) => {
    if (msg1) {
        msg2 = true;
        time = times.toString();
        console.log(time + 'h');
        console.log('--------------------');
        blind();
    }
})


//blinds function
function blind()    {
    if (temp > 28 || time < 9) {
        blinds = true;
        console.log('Blinds are CLOSED');
    }else if (temp > 28 || time > 19){
        blinds = true;
        console.log('Blinds are CLOSED');
    }else{
        blinds = false;
        console.log('Blinds are OPEN');
    }

    //sending the temperature values
    client.publish(topic_post,blinds.toString());

    msg2 = false;
    msg1 = false;
}

Upvotes: 0

Views: 189

Answers (1)

hardillb
hardillb

Reputation: 59658

You can't add multiple client.on('message') listeners, there can only be one.

So just add an if statement in the callback to check what topic the message arrived on.

client.on('message', (topic, message) => {
   if (topic == topic_sub1) {
     //handle temp
   } else if ( topic == topic_sub2) {
     //handle time
   }
})

Upvotes: 0

Related Questions