cgulliver
cgulliver

Reputation: 59

Cylon with MQTT Passing Sensor Data

This is probably easy but I could not locate a solution online. I am working on a weather station project with Cylon and MQTT and attempting to pass a variable into an MQTT push but it is passing the literal text. The publish is successful but it just has "msg" instead of the sensor data. Here is the snippet..

Cylon.robot({
  connections: {
    edison: { adaptor: 'intel-iot' }
  },

  devices: {
    bmp180: { driver: 'bmp180' }
  },

  work: function(my) {
    my.bmp180.getTemperature(function(err, val) {
      if (err) {
        console.log(err);
        return;
      }

      console.log("\tTemp: " + val.temp + " C");
        
        var msg = { "temperature" : val.temp,
                    "pressure" : val.press,
                    "altitude" : val.alt
                    };
        var msgPressure = { "pressure" : val.press };
        var msgAltitude = { "altitude" : val.alt };
        device
            .on('connect', function() {
                console.log('connect');
                device.subscribe('weather/push');
                device.publish('weather/push', JSON.stringify({ msg: 1}));
                });

        device
            .on('message', function(topic, payload) {
                console.log('message', topic, payload.toString());
                });
        
        
    });
}
      
}).start();

Thank you

Upvotes: 0

Views: 76

Answers (1)

hardillb
hardillb

Reputation: 59751

JSON.stringify({msg:1}) will generate a string that looks like this: {'msg': 1}

You probably want JSON.stringify(msg) in your publish line to send the msg object.

Upvotes: 2

Related Questions