# How to make an IoT protocol (parsing, answering)?

**URL:** <https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691>\
**Category:** General\
**Created:** [7 February 2019 07:41 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691 "2019-02-07T07:41:14Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 February 2019 09:54 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/21 "2019-02-10T09:54:09Z")

</div>

> [@jycemea](#):
>
> Is it possible to treat the extraction id substring, only with positions in AAA123456\* and define the code is always between the **4th** character and the **9th** character

In javascript in a function node you can use the substring method

> **[JavaScript String substring() Method](https://www.w3schools.com/jsref/jsref_substring.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 February 2019 10:24 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/22 "2019-02-10T10:24:20Z")

</div>

> [@jycemea](#):
>
> I don't know how to write the right code for the answer including **CodeAnswer,YearMonthDayHourMinutesSeconds**

You can use node-red-contrib-moment. Something like this

```auto
[{"id":"a9156097.d9e12","type":"moment","z":"514a90a5.c7bae8","name":"","topic":"","input":"nothing","inputType":"msg","inTz":"Europe/London","adjAmount":0,"adjType":"days","adjDir":"add","format":"[BBB,]YYYYMMDDHHmmss","locale":"en_GB","output":"payload","outputType":"msg","outTz":"ETC/GMT","x":328.5,"y":1280,"wires":[["48354d8a.2ae5f4"]]}]

```

Alternatively you could do it in a function node in javascript using `new Date()` and then the various access methods to get the components. Then convert to strings and concatenate.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 February 2019 14:51 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/23 "2019-02-10T14:51:13Z")

</div>

> [@jycemea](#):
>
> **20190207083012** is the YearMonthDayHourMinuteSecond

That's a strange format, I wonder how it will deal with daylight saving time changes...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 February 2019 15:00 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/24 "2019-02-10T15:00:53Z")

</div>

My suggested solution provides it in UTC, or at least that is what I intended, I can't actually test that easily as we are on UTC at the moment. Whether that is what the device actually wants I don't know. I suspect they are just using it instead of a GUID, so it doesn't actually matter.

---

<div class="post-metadata">

**Author:** ![jycemea](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jycemea](https://discourse.nodered.org/u/jycemea)\
**Post date:** [10 February 2019 16:40 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/25 "2019-02-10T16:40:07Z")

</div>

whaouh!!! 🤩

It is like you take me as someone qualified on Node-Red.... Just use it, you can do this or you can do that .... but if you don't tell me how... this will be complicate :))) Your Date/Time formater seems to be adapted... but how can I implement it in the scene with the answer **IWBP00...........................#**?

I don't know Node-Red 🙂

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 February 2019 17:01 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/26 "2019-02-10T17:01:45Z")

</div>

> [@jycemea](#):
>
> but how can I implement it in the scene with the answer **IWBP00**

Not sure what you mean by that, what exactly do you want to see?  
Did you look at my suggestion and see what it produces (by feeding in a message and looking at the output with debug node, after installing node-red-contrib-moment obviously) then looking at the info panel for the moment node so that you can understand what mine does? Once you understand what mine does you may be able to work out how to change it to do what you want.

---

<div class="post-metadata">

**Author:** ![jycemea](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jycemea](https://discourse.nodered.org/u/jycemea)\
**Post date:** [10 February 2019 17:09 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/27 "2019-02-10T17:09:16Z")

</div>

Yes I saw. Your solution is perfect... except I don't know how to include it with the answer...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 February 2019 17:10 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/28 "2019-02-10T17:10:17Z")

</div>

You still haven't said exactly what you want the message to be

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 February 2019 17:16 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/29 "2019-02-10T17:16:07Z")

</div>

If you just need to change the text round the date string then you just need to change the format string in the moment node.

---

<div class="post-metadata">

**Author:** ![jycemea](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jycemea](https://discourse.nodered.org/u/jycemea)\
**Post date:** [11 February 2019 07:38 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/30 "2019-02-11T07:38:29Z")

</div>

Hi Colin,  
wonderfull ! Solved with your moment node !!

ONLY 3 REQUESTS / ANSWER TO SOLVE AND IT IS FINISHED!!! BUT THEY ARE THE MOST DIFFICULT... OF COURSE! ☹

**ARE YOU STILL WITH ME FOR THE 3 LAST TO DECODE, PLEASE??**

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 February 2019 08:32 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/31 "2019-02-11T08:32:14Z")

</div>

There isn't much point keeping posting the whole flow, please just ask specific questions that can be addressed. So tell us what the remaining issues are.

---

<div class="post-metadata">

**Author:** ![jycemea](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jycemea](https://discourse.nodered.org/u/jycemea)\
**Post date:** [11 February 2019 08:59 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/32 "2019-02-11T08:59:14Z")

</div>

Right, I understand it is heavy... I erased the solved ones. Here, it is going to be harder : **watch=\>server / server =\>watch**

1/ Below are just the nodes concerned. I need to split the incoming message and to convert/decode as the notice explains (after the message below).

2/ I also need to send an answer to the watch as the notice said...  
**2 options:** or the answer is \ ***IWBP02#** and I solved it  
**but when**  
the reply flag is **1** , the server needs to respond: **IWBP02F16D3357025E5753715C3A535753776D275953903100300037003900F753#** which is the UNICODE encoded address to setup the address in the watch!!

**An here I don't know how to do this!!**

```auto
[{"id":"38fd6d46.07d862","type":"tab","label":"IWAP02","disabled":false,"info":""},{"id":"4a330da4.5ac7b4","type":"inject","z":"38fd6d46.07d862","name":"TEST IWAP02#","topic":"","payload":"IWAP02,zh_cn,0,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|367 5|9,9520|3676|8,9520|3677|7,4,1|D8-24-BD-79-FA-1F|59&2|3C-46-D8-6D-CE-01|81&3|0C-4C-39-1A-7C-65|69&4|70-A8-E3-5D-D7-C0|65#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":114,"y":132.7999906539917,"wires":[["b13eed31.409e9"]]},{"id":"b13eed31.409e9","type":"switch","z":"38fd6d46.07d862","name":"SORT INCOMING","property":"payload","propertyType":"msg","rules":[{"t":"regex","v":"^IWAP02","vt":"str","case":false}],"checkall":"true","repair":false,"outputs":1,"x":379.00000381469727,"y":184.80000114440918,"wires":[["59e2af21.74ffa"]]},{"id":"168c17f3.e614a8","type":"comment","z":"38fd6d46.07d862","name":"INCOMING MESSAGES FROM THE WATCH","info":"","x":177.3000030517578,"y":22,"wires":[]},{"id":"e6d39b5d.4017e8","type":"comment","z":"38fd6d46.07d862","name":"OUTGOING MESSAGES TO THE WATCH","info":"","x":644,"y":302.80002880096436,"wires":[]},{"id":"f8900c11.a7614","type":"change","z":"38fd6d46.07d862","name":"IWBP02# ","rules":[{"t":"set","p":"payload","pt":"msg","to":"IWBP02# ","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":544,"y":363.8000297546387,"wires":[["2817006d.81197"]]},{"id":"2817006d.81197","type":"debug","z":"38fd6d46.07d862","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":854.3000106811523,"y":354.2000045776367,"wires":[]},{"id":"17d7a98f.89c3c6","type":"debug","z":"38fd6d46.07d862","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":883.300012588501,"y":184.18750190734863,"wires":[]},{"id":"59e2af21.74ffa","type":"switch","z":"38fd6d46.07d862","name":"IWAP02 ANSWER OPTION","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"IWAP02,zh_cn,0","vt":"str"},{"t":"cont","v":"IWAP02,zh_cn,1","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":636.3000068664551,"y":184.40001487731934,"wires":[["f8900c11.a7614","17d7a98f.89c3c6"],["17d7a98f.89c3c6","21466e3.35e1792"]]},{"id":"21466e3.35e1792","type":"change","z":"38fd6d46.07d862","name":"IWBP02 ADDRESS","rules":[{"t":"set","p":"payload","pt":"msg","to":"ADDRESS NEEDED","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":575.0000076293945,"y":411.0000057220459,"wires":[["2817006d.81197"]]},{"id":"9cd8efdf.edd82","type":"inject","z":"38fd6d46.07d862","name":"TEST IWAP02_ADDRESS","topic":"","payload":"IWAP02,zh_cn,1,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|367 5|9,9520|3676|8,9520|3677|7,4,1|D8-24-BD-79-FA-1F|59&2|3C-46-D8-6D-CE-01|81&3|0C-4C-39-1A-7C-65|69&4|70-A8-E3-5D-D7-C0|65#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":139,"y":244.00000190734863,"wires":[["b13eed31.409e9"]]}]

```

**IWAP02 INCOMING MESSAGES FROM THE WATCH to decode**

IWAP02,zh\_cn,0,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|3675|9,9520|3676|8,9520|3677|7,4,1|D8-24-BD-79-FA-1F|59&2|3C-46-D8-6D-CE-01|81&3|0C-4C-39-1A-7C-65|69&4|70-A8-E3-5D-D7-C0|65#

where **IWAP02** = command  
**zh\_cn** : language notice  
**0:reply flag** ，the server doesn’t reply address when it’s 0 and replies address when it’s 1.  
**7: 7** sets of bases  
**460** : MCC is country code  
**0** : MNC operator code  
**9520|3671|10** : LAC|CID|dbm shows a set of base information and the numbers should correspond to the number of bases.  
1 **0** ：dbm is signal strength, multiple bases signal strength is suggested to be  
arranged from high to low then downloaded, signal strength are using absolute values: 150 – abs(dbm)  
**4** : sets of wifi  
**1|D8-24-BD-79-FA-1F|59** : ssid|mac address| signal strength，signal strength are using absolute  
values:150 – abs(signal strength)

**platform responds:**

**OUTGOING MESSAGES TO THE WATCH**  
platform replies:  
**IWBP02#**

**If the reply flag is 1, the server has to setup the address to the watch:**  
**IWBP02F16D3357025E5753715C3A535753776D275953903100300037003900F753#** UNICODE.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 February 2019 11:12 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/33 "2019-02-11T11:12:36Z")

</div>

> [@jycemea](#):
>
> If the reply flag is 1, the server has to setupe the address to the watch:  
> IWBP02F16D3357025E5753715C3A535753776D275953903100300037003900F753# UNICODE.

A couple of questions  
Is that a fixed reply or is some of it variable?  
When you say it is unicode I am not sure exactly what you mean. Can you give more detail? Perhaps show the byte sequence you expect for the start of the message.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 February 2019 11:22 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/34 "2019-02-11T11:22:58Z")

</div>

If you really want to decode something like this, you need to set up something to monitor the traffic between the watch and the LIVE server. This will tell you exactly what happens and when. You can use a "proxy" service for this, there are plenty of tutorials on the internet.

Just watch out for the fact that, if the server operates in a Chinese language, any unicode text coming back may be in a double-byte format.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 February 2019 11:26 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/35 "2019-02-11T11:26:49Z")

</div>

I think he has already done that. He isn't trying to decode that string, he is trying to build it to send back.

---

<div class="post-metadata">

**Author:** ![jycemea](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jycemea](https://discourse.nodered.org/u/jycemea)\
**Post date:** [11 February 2019 11:56 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/36 "2019-02-11T11:56:39Z")

</div>

The strings are coming in alphanumeric, no HEX or Unicode. I just need to split, name and convert the incoming datas.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 February 2019 12:21 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/37 "2019-02-11T12:21:38Z")

</div>

I thought you said you didn't know how to build the Unicode string. You didn't answer my questions

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 February 2019 13:39 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/38 "2019-02-11T13:39:23Z")

</div>

To split it I think the easiest way will be to use a function node. Have you done any programming? You are going to need to learn some javascript.  
you can first split it at the commas then where appropriate split the elements at the `|` symbols, building a javascript object with the results. So something like (untested)

```auto
let answer = {}
let elements = msg.payload.split(",") // this gives an array containing the bits
// elements[0] is IWAP02 so we can ignore that
answer.language = elements[1]
node.warn(`language is: ${answer.language}`) // for testing display it in the debug pane
answer.flag = elements[2]
node.warn(`flag is: ${answer.flag}`)
answer.baseCount = elements[3]
// you can work out how to put the node.warn statements in yourself
answer.country = elements[4]
answer.operator = elements[5]
// now need to get the bases
let i;
answer.bases = []
for (i=6, i<6+answer.baseCount, i++) {
  let baseElements = elements[i].split("|")
  answer.push({lac: baseElements[0], cid: baseElements[1], dbm: baseElements[2]})
}
// the rest needs to go in here
msg.payload = answer
return msg

```

That should be a start for splitting it up. Get that going then see if you can handle the rest. No doubt there are syntax errors in what I have posted but it will be good practice fixing them.

---

<div class="post-metadata">

**Author:** ![jycemea](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jycemea](https://discourse.nodered.org/u/jycemea)\
**Post date:** [11 February 2019 14:15 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/39 "2019-02-11T14:15:08Z")

</div>

Of course, I don't know! 🙂 50% of what I built here is because you helped me Colin!!  
I'm always using the same protocol to go forward :  
I'm trying to build a solution with my knowledges. When I can't find a way, I'm asking here.  
For this raison, I'm updating my posts till the state "solution".

---

<div class="post-metadata">

**Author:** ![jycemea](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jycemea](https://discourse.nodered.org/u/jycemea)\
**Post date:** [11 February 2019 14:29 UTC](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691/40 "2019-02-11T14:29:14Z")

</div>

Colin...  
You give me the flight manual ... during the flight!!!! 😄  
You just told me to squawk Mode Charlie and to reduce my speed at 255, altitude 1200... also asked me to wilco and enter in stanby mode...

Of course, I have to deduct where is the airport without  
landmarks neither a radar... in the dark :)))

No way to use this in my schema!!

[Previous page](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691.md?page=1)

[Next page](https://discourse.nodered.org/t/how-to-make-an-iot-protocol-parsing-answering/7691.md?page=3)
