mqtt communication between node.js and java

前端 未结 2 1389
醉酒成梦
醉酒成梦 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:25

    MQTT message payloads are just byte arrays so you need to be aware of how a message is encoded at both the sending and receiving end. In this case it is not clear exactly what you are sending as the code you have posted is just passing a Java Object.

    The error message in the picture implies that the a Java serialized version of the object is being sent as the message payload. While this will contain the information you need reassembling it in JavaScript will be incredibly difficult.

    Assuming the TempStruct object looks something like this:

    public class TempStruct {
    
      int value = 0;
      String units = "C";
    
      public void setValue(int val) {
        value = val;
      }
    
      public int getValue() {
        return value;
      }
    
      public void setUnits(String unit) {
        units = unit;
      }
    
      public String getUnits() {
        return units;
      }
    }
    

    Then you should add the following method:

    public String toJSON() {
      String json = String.format("{'value': %d, 'units': '%s'}", value, units);
      return json;
    }
    

    Then edit your Java publishing code as follows:

    ...
     this.myPubSubMiddleware.publish("tempMeasurement", newValue.toJSON().getBytes("utf-8"),
                myDeviceInfo);
    ...
    

    And change your JavaScript like this:

    ...
    client.on('message',function(topic,payload){
      console.log("In messgage....");
      var tempStruct = JSON.parse(payload.payloadString)
      console.log("tempValue: "+tempStruct.value);        
    });
    ...
    

    EDIT:

    Added getBytes("utf-8") to the Java code to ensure we are just putting the string bytes in the message.

    EDIT2: Sorry, mixed up the paho web client with the npm mqtt module, the JavaScript should be:

    ...
    client.on('message',function(topic,payload){
      console.log("In messgage....");
      var tempStruct = JSON.parse(payload.toString())
      console.log("tempValue: "+tempStruct.value);        
    });
    ...
    
    0 讨论(0)
  • 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);
    }  
    });
    
    0 讨论(0)
提交回复
热议问题