简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM