Callback function arduino

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?

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-03-44

As you may guess, this is what I type:

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

This is what I get:

rs485_tx : msg.payload : buffer[5]
buffer[5]raw
0: 0x0
1: 0x65
2: 0x6c
3: 0x6c
4: 0x6f

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.

Alright, but I want a binary buffer and 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:

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

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

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?

This is the function:

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();
  }
}

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.

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:


And this is what I got:
11:25:30.241 -> Message arrived
11:25:30.274 -> ello

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

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

True!!! :

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.

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

buffer with binary data, not a String!

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

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

Right this datatype: