mqtt communication between node.js and java

前端 未结 2 1409
醉酒成梦
醉酒成梦 2021-01-16 21:51

Goal is to send data using mqtt protocol. Java project (tempSensor) produce tempvalue using mqtt protocol and node.js which subscribe tempvalue using mqtt. Both node.js and

2条回答
  •  感情败类
    2021-01-16 22:34

    Finally i am able to solve the problem.On the receiving side, we need to convert byte in to readable string and than parse readable using JSON parser. Solution is shown below:

    var mqtt=require('mqtt');
    var client=mqtt.connect('mqtt://test.mosquitto.org:1883');  
    client.subscribe('tempMeasurement');
    client.on('message',function(topic,payload){
    if(topic.toString()=="tempMeasurement"){
    console.log("Message received");
    var data =payload.toString('utf8',7);
    var temp=JSON.parse(data);
    console.log(temp.tempValue,temp.unitOfMeasurement);
    //console.log(data);
    }  
    });
    

提交回复
热议问题