MQTT how to filter topic?

Forum Home Forums Communicating with Devices MQTT how to filter topic?

Tagged: 

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #12734
    KevinVt
    Participant

    Dear Mikhail,

    I used MQTT client to subscribe one topic, like “report”, and can get data from MQTT server, but there is heartbeat data also use topic name “report”, so I can get two json message sometimes, the problems is when heartbeat message arrive, the channels data was set to null, I had used js sentence to filter data, but it did not work.

    Is there any method to filter mqtt message, only get ‘real’ data and get rid of heartbeat data?

    #12735
    Mikhail
    Moderator

    Hello,

    In such situation, using JS is the right method. If it doesn’t work, provide more info.

    #12744
    KevinVt
    Participant

    Hello Mikhail,

    Below 2 messages are got from MQTT Server, these topics are same, and type are different.
    ——————————————————————————–
    Message1:
    Topic=【report/allpoints】 msg=【{“saleid”:”10100001″,”gateid”:”10100001001″,”type”:”report”,”time”:”2023-06-01 13:10:00″,”sequence”:”22″,”source”:”da”,”meter”:[{“id”:”1″,”status”:”1″,”name”:”VrtDev1″,”values”:{“Pf”:”0.287000″,”Uab”:”391.900024″,”Ubc”:”392.300018″,”Uca”:”391.799988″,”Ia”:”0.400000″,”Ib”:”0.000000″,”Ic”:”0.400000″,”P”:”0.000000″,”Q”:”-0.160000″,”S”:”0.160000″,”EPI”:”2257.599854″,”EPE”:”10.800000″,”EQL”:”93.199997″,”EPIJ”:”0.000000″,”EPIF”:”0.000000″,”EPIP”:”0.000000″,”EPIG”:”0.000000″,”MDMINUTE”:”0.000000″,”MDHOUR”:”0.000000″,”MDDAY”:”0.000000″,”MDMONTH”:”0.000000″,”MD”:”0.000000″,”UaTHD”:”2.210000″,”UbTHD”:”2.510000″,”UcTHD”:”2.780000″,”IaTHD”:”0.000000″,”IbTHD”:”0.000000″,”IcTHD”:”0.000000″,”VUB”:”0.220000″,”CUB”:”100.000000″,”Pa”:”0.000000″,”Pb”:”0.000000″,”Pc”:”0.040000″,”IL”:”0.000000″,”EQC”:”980.000000″}}]}】 qos level= 【AtMostOnce】
    Message2:
    Topic=【report/allpoints】 msg=【{“saleid”:”10100001″,”gateid”:”10100001001″,”type”:”heart_beat”,”operation”:”notify”,”time”:”2023-06-01 13:10:07″}】 qos Level=【AtMostOnce】

    I wrote below js code to filter the message,
    —————-js code ————————

    let data = JSON.parse(payload);

    if (data.type == “report”) {

    // set values
    setValue(0, new Date(data.time).getTime());
    setValue(1, data.sequence);

    setValue(2, data.meter[0].values.Pf);
    setValue(3, data.meter[0].values.Uab);
    setValue(4, data.meter[0].values.Ubc);
    setValue(5, data.meter[0].values.Uca);

    }
    else
    {
    log(“Ignore HearBeat Data…”);
    }
    ————————————————

    It can print the log “Ignore HearBeat Data…” when got message with type “heart_beat”, and at same time, all channels value were set to null.

    I hope to get the channel value when got message with type of “report” and keep channel value unchange when got meesage with type “heart_beat”.

    Hope you can understand what I said.

    Thanks a lot.

    #12746
    Mikhail
    Moderator

    Hello,

    First of all, I would log some message if data.type == "report" to check whether that section is executed.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.