This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

CLI Example OpenThread 2.0.0 - UDP data transfer from main.c

Hello!
We are working with NRF52840 dongles and want to be able to have them relay data over an OpenThread mesh network through UDP automatically. We have found within the OpenThread API a solid Udp.h library with all the Udp functions we need to create code that runs on the dongles from the main.c.
Below is our code that should broadcast the message: "Hallo" to all nodes that have an open socket on port 1994.
We have read that the ipv6 address ff03::1 is reserved for multicast UDP broadcasting and it works perfectly when manually performed with the CLI udp commands.
CLI:  Udp open, udp send ff03::1 1994 Hallo
With all the nodes that have udp open, udp bind :: 1994, receiving the Hallo message from the sending node.
We are trying to recreate this in the main.c of our nodes so that we can provide the nodes with some intelligence of their own.
This piece of code is run once when the push button on the dongle is pressed.
The code compiles perfectly and we have tested the functions that have a return with the RGB led (green OK, red not) to confirm that there weren't any errors produced (sadly not all functions return a no_error value)
void udpSend(){
    char buf[512];
    otMessageInfo messageInfo;

    strcpy(buf, "Hallo\0");

    memset(&messageInfo, 0, sizeof(messageInfo));

    otIp6AddressFromString("ff03::1\0", &messageInfo.mSockAddr);
    messageInfo.mSockPort = 1994;

    otUdpSocket mySocket;
    otIp6AddressFromString("ff03::1\0", &mySocket.mSockName.mAddress);
    mySocket.mSockName.mPort = 1994;
    otUdpOpen(thread_ot_instance_get(), &mySocket, NULL, NULL);

    otMessage *test_Message = otUdpNewMessage(thread_ot_instance_get(), NULL);

    if (otMessageAppend(test_Message, &buf, sizeof(buf)) == OT_ERROR_NONE){
       nrf_gpio_pin_write(LED2_G, 0);
    }
    else{
       nrf_gpio_pin_write(LED2_R, 0);
    } 

    otUdpSend(&mySocket, test_Message, &messageInfo);

    otUdpClose(&mySocket);
}

Now, we aren't exactly experts, so we are not sure why this isn't working as we had a lot of trouble figuring out how everything is called/initialised.
We hope to create a way to send and receive data through UDP through the code, so that they can operate autonomously.
We would really appreciate it if someone could assist us with our project!
Thanks!
Jonathan
  • Hi Giorgio,

    The softdevice .hex file we used was the mbr_nrf52_2.3.0_mbr.hex located in the Nordic SDK\components\softdevice\mbr\nrf52840\hex.

    We worked on Linux and used the mergehex tool to merge the app and the softdevice hex into one file. If not mistaken it also exists for Windows, but I'm not sure about that.

    Good luck and hope this helps!

    Kind regards,

    Jonathan

  • (UPDATED 02 September 2019)

    Hi,

    I have being used this forum to fix the problem that I had in my code to send UDP message over Thread network. The answers that could read here don´t fix it but after a lot of time reviewing MQTT official example I found my problem, so I write my solution for the future.

    My problem was that when I use otUdpNewMessage(thread_ot_instance_get(), true); function I put NULL as second parameter and after chenge it to "true",  WORKED.

    When I press a button my main code launch this function:

    otError acuario_send_temperature_udp_command(void)
    {
        otError error = OT_ERROR_NONE;
    
        otUdpSocket socket;
        otMessage *udp_message = NULL;
        otMessageInfo messageInfo;
        char buf[10] = "Hello";
        
        memset(&messageInfo, 0, sizeof(messageInfo));
    
        error = otUdpOpen(thread_ot_instance_get(), &socket, NULL, NULL);
    
        error = otIp6AddressFromString("FF03::0001", &messageInfo.mPeerAddr);
        messageInfo.mPeerPort = (uint16_t)1994;
        messageInfo.mInterfaceId = OT_NETIF_INTERFACE_ID_THREAD;
    
        /***
        *
        *   THE NEXT LINE IS SO IMPORTANT, THIS FIX MY PROBLEM. The 
        *   second parameter has to be TRUE
        */
        udp_message = otUdpNewMessage(thread_ot_instance_get(), true);
        
        
        
        otMessageAppend(udp_message, buf, (uint16_t)strlen(buf));
    
        error = otUdpSend(&socket, udp_message, &messageInfo);
    
    
        if(error != OT_ERROR_NONE && udp_message != NULL)
        {
            otMessageFree(udp_message);
        }
    
    
        return error;
    }
    
    
    

    Thank so much I hope to help other people with my solution.

  • Hi,

    I have been trying to send and recieve udp messages just like the rest of you. I tried this with the CLI example out of the Nordic SDK (on the nrf52840 dongle). I have tried out the different solutions which where posted on this forum. After some trying I finally got the otUdpSend to return OT_ERROR_NONE however the other nodes do not recieve the udp message. 

    So my question is are we doing something wrong in the recieve function, because the led never turns on?

    (our second dongle also hast the udpInit function and the send function from Gabridc)

    static otUdpSocket *mySocket;
    
    static void udpInit( otUdpSocket *mySocket )
    {
        otUdpOpen( thread_ot_instance_get(), mySocket, handleUdpReceive, NULL );
    }
    
    void handleUdpReceive( void *aContext, otMessage *aMessage,
                                 const otMessageInfo *aMessageInfo    )
    {
        OT_UNUSED_VARIABLE(aContext);
        OT_UNUSED_VARIABLE(aMessage);
        OT_UNUSED_VARIABLE(aMessageInfo);
    
        bsp_board_led_on(2);
    }

    Kind regards,

    Julian

  • hi,

    I also can't recieve udp messages. Does anyone have a example code.

    kind regards,

    Quinn van der Schaar

Related