How to call function that processes downlink message

Hello all, I’ve been stuck on this issue for some time now. In the ESP32_LoRaWAN library how can I call the “downLinkDataHandle(McpsIndication_t *mcpsIndication)” function in another function, I’ve never dealt with pointers before and I have no idea how to call this function to be able to use the “LoRa_data” variable. It’s is declared as a String globally. please help, thank you.

void  downLinkDataHandle(McpsIndication_t *mcpsIndication)

{
LoRa_data = “”;
lora_printf("+REV DATA:%s,RXSIZE %d,PORT %d\r\n", mcpsIndication->RxSlot ? “RXWIN2” : “RXWIN1”, mcpsIndication->BufferSize, mcpsIndication->Port);
lora_printf("+REV DATA:");
app(mcpsIndication->Buffer[0]);

for (uint8_t i = 0; i < mcpsIndication->BufferSize; i++)
{
lora_printf("%02X", mcpsIndication->Buffer[i]);
LoRa_data = LoRa_data + (String)(char)mcpsIndication->Buffer[i];
}
lora_printf("\r\n");
Serial.println(LoRa_data);
}

Hello,
That function gets called when the Lora Radio received a downlink, which is handled in the Radio driver… and it is not to be called by your code…

How can I use the data being sent during downlink? I know there is a case switch function, but it there a way to store the downlink message into a global variable such as a String?

You can rewrite this function downlinkdatahandle to achieve your goals

Will I be able to save the String “LoRa_data” globally? If I can’t call that function then how to turn that local variable to a global one that can be used in the entire sketch?

Calling this function is the easiest way to modify. Other methods will be more complex.

Sorry I’m pretty new to programming, I know how to call a normal function like
Myfunc()

But for this one
downLinkDataHandle(McpsIndication_t *mcpsIndication)

I’m not sure how to call this function without errors since it has values being passed

Hi @grizzlyalleyne,
You do not call this function from your code, this function gets invoked when your node receives a downlink from the server. with that said i will assume that you are using TTN and show you how to invoke the function from TTN.
basically you login to TTN console and then click on Applications next you pick your application and click on it, now you click on End devices from the left side Menu, next click on the node in question next click on Messaging and then Downlink here you will have a choice to send a byte to your node or a JSON string… mind you to keep things small below 50 bytes, my recommendation is send a byte. once you click Schedule Downlink. things will be ready … as soon as your node sends an Uplink it will be notified by the server that a downlink is scheduled and will send it to the node and that’s when you function above will get invoked and you will receive the bytes.
so let’s assume we are sending he bytes as shown in the snap shot below: 01 00 00 00 01

to receive the above bytes and interpret them you can use the following code:

void downLinkDataHandle(McpsIndication_t *mcpsIndication)
{
    printf("\n\t downLinkDataHandle: ACK Data received\n");
    printf("\n\t+REV DATA:%s,\n\tRXSIZE %d,\n\tPORT %d, RSSI: %d,\n\t SNR: %d,\n\t DATA_RATE:%d,\r\n",
           mcpsIndication->RxSlot ? "RXWIN2" : "RXWIN1", mcpsIndication->BufferSize, mcpsIndication->Port,
           mcpsIndication->Rssi, (int)mcpsIndication->Snr, (int)mcpsIndication->RxDoneDatarate);
    if (mcpsIndication->RxData == true)
    {
        printf("+REV DATA:");
        for (uint8_t i = 0; i < mcpsIndication->BufferSize; i++)
        {
            printf("%02X", mcpsIndication->Buffer[i]);
        }
        // since the payload is 4 bytes let's reassemble it.
        uint32_t data;
        data = mcpsIndication->Buffer[1] << 24 | mcpsIndication->Buffer[2] << 16 | mcpsIndication->Buffer[3] << 8 | mcpsIndication->Buffer[4];
        if (mcpsIndication->Port == 1) // this is what TTN calls FPort can be used as case switch as well, valid values are 1 to 254
        {
            switch (mcpsIndication->Buffer[0])
            {
            case 0x01: // 01 00 00 00 00
            {
                printf("the data we received is %d", data);
                break;
            }
            case 0x02: // 02 00 00 00 00
            {
                printf("the data we received is %d", data);
                break;
            };
            default:
                printf("unknown code received %d", downLinkConfig.code);
                break;
            }
        }
    }
}

I hope this helps,
Jay