robertsLando/Mqtt2Mqtt

Mqtt2Mqtt client can't publish new topic to Mosquitto Broker

yanpengwuIoT opened this issue · 1 comments

Hi Daniel,

I have a sensor that can publish data to a MQTT broker with topic "deviceId/events/rpc" and a nested JSON object payload. I need to change the topic to "json/apikey/deviceId/attrs" and the payload to a single-level JSON object. I believe Mqtt2Mqtt can help me achieve this perfectly.

Now I setup Mqtt2Mqtt and Mosquitto on a CentOS server, but on Mosquitto:1883 I can't receive anything. My configuration:

  1. In Mqtt2Mqtt->Broker, listening on 1884, when I use mosquito_sub, I can get the data from the sensor.
  2. I created a Maps 'gettest' and just enabled the "Custom Topic" and "use Function", keep "Wildecard From" empty and function with "return {topic: topic, payload: payload}". I think this will forward the original topic and payload to the third-party Broker.
  3. In Mqtt2Mqtt->Clients, set "Host URL:localhost, Port:1883, Reconnected period:3000" and "Maps Get: gettest".
  4. After appling the configuration, I can see "client Connectted" in Mqtt2Mqtt->Broker. Also in MosquittoBroker log, I can see this Mqtt2Mqtt->Client is connected with MosquittoBroker.
  5. But when I subscribe to the original topic on MosquittoBroker:1883, there is nothing received. but on Mqtt2Mqtt :1884, the sensor data still can be got.

Why the data can't be received on MosquittoBroker:1883? Could you help give me some guidance and comments?

In addition, next step I will create a function as below in Maps to do the topic and payload transformation, I hope it can work as I expected :).

          //topic transform;
          var parts = topic.split('/');
          var newTopic = 'json/iothouseapikey/' + parts.reverse().pop() + '/attrs';
          
          //payload transform
          var newPayload = {"src":"defaultSrc", "method":"NotifyStatus", "ts":1659606613.35, "switchId":0, "apower":0};
          const objPayload = JSON.parse(payload);
          
          newPayload.src = objPayload.src;
          newPayload.method = objPayload.method;
          newPayload.ts = objPayload.params.ts;
          newPayload.switchId = objPayload.params['switch:0'].id;
          newPayload.apower = objPayload.params['switch:0'].apower;
          
          return {topic: newTopic , payload: JSON.stringify(newPayload)}

Thank you very much.

Hello again,
It works after I change "Wildecard From" from empty to "+/events/rpc". I thought every incoming data package will trigger the Mqtt2Mqtt.client publishing the new topic and payload to the third-party broker(it's Mosquitto here) when keeping "Wildecard From" as empty , it's a misunderstanding.
It's solved.

Thank you very much.