# MQTT To ASCII over TCP/IP

**URL:** <https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438>\
**Category:** General\
**Created:** [29 May 2021 05:01 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438 "2021-05-29T05:01:15Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)\
**Post date:** [28 June 2021 12:58 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/21 "2021-06-28T12:58:27Z")

</div>

This is a function prior to the "buffer parser" node. It sets the values in the proper object. You can see I am setting a value in a variable and using the variable in the object. It gets the payload which is a buffer type, sets the object value and passes this with the original payload untouched.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0fa6fb1e0a7191151b0e8d33e6fbbd884dda5c56.png)

```auto
var buffer=msg.payload;
var bufferlen = buffer.length;
var mymsg= {
    options: {
        resultType: "object", 
        msgProperty: "payload"},
    items: [
        {
            name: "unit1",
            type: "hex",
            offset: 0,
            length: bufferlen
        }
	]
}
msg.spec=mymsg;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 June 2021 13:14 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/22 "2021-06-28T13:14:59Z")

</div>

@tree-frog

If the purpose of the dynamic `spec` (above) is to convert the full input buffer to a HEX string, you can achieve this in the UI also. Just specify the `length` as `-1`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/105ae406659fdaa9e9d89a9e4ae51490c5ad7de7.png)

From the built in doc...

> - length (Number|optional) The quantity of items to be returned. e.g. 6 bools or 12 floats or 34 int32s. NOTE: setting `length` to -1 will attempt to read all bytes from offset to the end. NOTE: If source data does not have enough bytes, the operation will fail.`

The salient detail being **"setting `length` to -1 will attempt to read all bytes from offset to the end"**

_PS: Just ignore this if I have misinterpreted_

---

<div class="post-metadata">

**Author:** ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)\
**Post date:** [28 June 2021 14:00 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/23 "2021-06-28T14:00:00Z")

</div>

Steve,  
To be honest, I missed that particular entry and went for the spec to get how to modify it on the fly. Being handy to be able to set the object correctly and manage it.

Yet, I am amazed at my inability to grasp these details when i scan over, instead of read...  
Thanks.

---

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [2 July 2021 22:11 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/24 "2021-07-02T22:11:30Z")

</div>

Where do you put the code ? in the buffer-parser

---

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [2 July 2021 22:20 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/25 "2021-07-02T22:20:57Z")

</div>

I did try using the dynamic spec but not getting the right value  
I the case in the image ambient temp is 10, But not getting that result.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2b5d822cbc9641966a4157c902add596a18a1055.jpeg)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 July 2021 22:25 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/26 "2021-07-02T22:25:19Z")

</div>

Can you copy the payload that goes into the buffer parser (use the copy value but not appears under your mouse when you have over the debug message)

Paste it into a reply.

Also explain which bytes refer to this temperature value.

---

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [2 July 2021 22:33 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/27 "2021-07-02T22:33:12Z")

</div>

This is the Values coming from TCP device  
[67,72,78,44,49,58,65,77,66,84,69,77,80,44,49,48,48,13,10]

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [2 July 2021 22:48 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/28 "2021-07-02T22:48:52Z")

</div>

Looks like a string to me. Try feeding into a function.  
msg.payload = msg.payload.toString();  
return msg;

---

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [2 July 2021 22:59 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/29 "2021-07-02T22:59:10Z")

</div>

So here is the result when I set the buffer parser to ASCII

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/7/c7fa80c87aedbbc0663813fd463ff5b91ead4c1c.jpeg)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/4633ccb59f7c1ddf9b9ac3ae1615be670c93c216.jpeg)

---

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [2 July 2021 23:01 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/30 "2021-07-02T23:01:51Z")

</div>

How would I set it so the payload is always the value part of the string ? in the case for reading ambient temp it it 10, see image

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/0/d0500818b6245baf08e063ebb68e7cb8f1babe60.jpeg)

---

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [2 July 2021 23:29 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/31 "2021-07-02T23:29:28Z")

</div>

Interesting when I try a function node I get (see image)  
Now I just need to set it so I get just 100 and then drop off the last zero so its 10

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/99f77f8d0bf0f0bafdfaac286794f03644dd40eb.jpeg)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [31 August 2021 23:30 UTC](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438/32 "2021-08-31T23:30:18Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/mqtt-to-ascii-over-tcp-ip/46438.md?page=1)
