# Callback function arduino

**URL:** <https://discourse.nodered.org/t/callback-function-arduino/27021>\
**Category:** General\
**Created:** [21 May 2020 08:08 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021 "2020-05-21T08:08:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 08:08 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/1 "2020-05-21T08:08:50Z")

</div>

Hi there,  
I am trying to get the payload in the serial monitor of arduino through mqtt and node-red, but although I get the message, the first character is missing! Does anyone know what I am doing wrong?

```auto
void receiveMqttMessage(const char* topic, byte* payload, unsigned int len) {

  if (strcmp(topic, "rs485_tx") == 0) {
    // Send rs485
    int i;
    Serial.println("Message arrived");
    for (i = 0; i < len; i++) {
      char receivedChar = (char)payload[i];
      Serial.print(receivedChar);
      //sendRS485(payload[i], len);
    }
    Serial.println();
  }
}

```

 ![Captura de pantalla de 2020-05-21 10-06-21](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0dd0510fba0b502d4d939d5c118ec2462dc71d39.png) ![Captura de pantalla de 2020-05-21 10-04-16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/59a94c4b9abe3dac6bb4c764ebe0e7b38e15ec29.png) ![Captura de pantalla de 2020-05-21 10-03-44](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d74d61250ee14c08f6d417b867750918348d352c.png) ![Captura de pantalla de 2020-05-21 10-03-30](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fda18087161200c89c7c430d1e0aee153373f720.png)

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 08:12 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/2 "2020-05-21T08:12:02Z")

</div>

As you may guess, this is what I type:

 ![Captura de pantalla de 2020-05-21 10-10-47](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c9fc2b694129fc994149804b9da066e3f16729f.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 May 2020 08:35 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/3 "2020-05-21T08:35:24Z")

</div>

Can you add a Debug node alongside the MQTT node so you can confirm exactly what payload you are passing the node?

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 08:41 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/4 "2020-05-21T08:41:23Z")

</div>

This is what I get:

rs485\_tx : msg.payload : buffer[5]  
buffer[5][raw](http://localhost:1880/#)  
0: 0x0  
1: 0x65  
2: 0x6c  
3: 0x6c  
4: 0x6f

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 08:45 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/5 "2020-05-21T08:45:03Z")

</div>

![Captura de pantalla de 2020-05-21 10-43-53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1bfb96a2da96de42d0d22e32f8d62afb864c676f.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 May 2020 08:45 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/6 "2020-05-21T08:45:14Z")

</div>

You can see that the first byte is `0`. That tells us the issue is in how you are generating the buffer from the received text.

But you don't need to convert the string to a buffer at all - the MQTT node can publish the string for you as-is.

Replace your Function node with a Change node configured to move `msg.payload.rs485text` to `msg.payload` and then pass that to the MQTT Out node.

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 08:58 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/7 "2020-05-21T08:58:05Z")

</div>

Alright, but I want a binary buffer and replacing the function node with a Change node doesn't work with my arduino code

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 May 2020 09:06 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/8 "2020-05-21T09:06:01Z")

</div>

> [@qmf](#):
>
> replacing the function node with a Change node doesn't work with my arduino code

I have assumed you want to type some text into the form ... eg `hello` and have that arrive at the arduino as the ascii data `0x68 0x65 0x6c 0x6c 0x6f`

If you pass a string straight to the MQTT Out node, then it will be published and the arduino will receive it. I've just recreated that here:

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

So when you say you want to binary buffer what exactly do you mean?

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 09:14 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/9 "2020-05-21T09:14:57Z")

</div>

> [@knolleary](#):
>
> I have assumed you want to type some text into the form ... eg `hello` and have that arrive at the arduino as the ascii data `0x68 0x65 0x6c 0x6c 0x6f`

Correct! That's exactly what I want.  
Just the same thing I have, but not loosing the first character

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 May 2020 09:15 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/10 "2020-05-21T09:15:53Z")

</div>

> replacing the function node with a Change node doesn't work with my arduino code

Can you share exactly what you tried that didn't work?

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 09:19 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/11 "2020-05-21T09:19:07Z")

</div>

This is the function:

```auto
void receiveMqttMessage(const char* topic, byte* payload, unsigned int len) {

  if (strcmp(topic, "rs485_tx") == 0) {
    // Send rs485
    int i;
    Serial.println("Message arrived");
    for (i = 0; i < len; i++) {
      char receivedChar = (char)payload[i];
      Serial.print(receivedChar);
      //sendRS485(payload[i], len);
    }
    Serial.println();
  }
}

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 May 2020 09:20 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/12 "2020-05-21T09:20:43Z")

</div>

Yes, you've already shared your arduino code. That isn't what I was asking.

You said using a Change node didn't work - but you haven't said in what way it didn't work and you haven't shared exactly what you tried to do with the Change node.

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 09:29 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/13 "2020-05-21T09:29:22Z")

</div>

Oh sorry! it's just a project and I should do it this way, just fixing what It's wrong! That's why I'm trying to do it with the function node that way

I tried it with the change node:

 ![Captura de pantalla de 2020-05-21 11-26-06](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/10431679ed90879cfe20da39c1ea435b9e71eb90.png)  
And this is what I got:  
11:25:30.241 -\> Message arrived  
11:25:30.274 -\> ello

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 May 2020 09:30 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/14 "2020-05-21T09:30:41Z")

</div>

You've just shown a flow going from MQTT to your dashboard.

That is not the bit of the flow we've been discussing.

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 09:34 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/15 "2020-05-21T09:34:40Z")

</div>

True!!! :

 ![Captura de pantalla de 2020-05-21 11-33-29](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/41a452b6d79caf176fd0b59c941d8ce0027c1fe4.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 May 2020 09:36 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/16 "2020-05-21T09:36:35Z")

</div>

I can see from the label of the Switch node you have not configured it as I had suggested.

Configure it to **move** `msg.payload.rs485text` to `msg.payload`.

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 09:44 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/18 "2020-05-21T09:44:00Z")

</div>

It works and I get the message, but I want it in a buffer with binary data, not a String!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 May 2020 09:47 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/19 "2020-05-21T09:47:56Z")

</div>

> buffer with binary data, not a String!

If the user types in `hello` - what data do you want to arrive at the arduino?

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 09:59 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/20 "2020-05-21T09:59:42Z")

</div>

binary ascii representation, so \<Buffer 00 04 01\> in the console

---

<div class="post-metadata">

**Author:** ![qmf](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@qmf](https://discourse.nodered.org/u/qmf)\
**Post date:** [21 May 2020 10:13 UTC](https://discourse.nodered.org/t/callback-function-arduino/27021/21 "2020-05-21T10:13:20Z")

</div>

Right this datatype:

 ![Captura de pantalla de 2020-05-21 12-11-34](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/4987e6b65bc277f0434cfc55dd849e8f7889eb11.png)

[Next page](https://discourse.nodered.org/t/callback-function-arduino/27021.md?page=2)
