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

nRF Thread network UDP example

Dear Nordic,

Is there any working example of starting comunication over the UDP protocol in thread network?

 

  • Hi,

    Technically all the CoAP and MQTT-SN examples communicate over UDP, but they also includes higher layer application protocols. If you want to communicate purely over UDP packets, there is unfortunately no examples showing this. The CLI example is capable of sending UDP packets, maybe this is suitable for your needs?

    See the CLI UDP reference on GitHub.

    If you want a pure UDP example, this should be quite simple to implement. You can reference the code used in the CLI UDP module, and the OpenThread UDP API reference.

    Best regards,
    Jørgen

  • I have another issue.

    What I have to pass to function "otUdpOpen" in aContext variable using C language?? I passed NULL but I get hard fault.

    Below doc to this function

    otUdpOpen ( otInstance *  aInstance,
    otUdpSocket aSocket,
    otUdpReceive  aCallback,
    void *  aContext 
    )

    Open a UDP/IPv6 socket.

    Parameters
    [in] aInstance A pointer to an OpenThread instance.
    [in] aSocket A pointer to a UDP socket structure.
    [in] aCallback A pointer to the application callback function.
    [in] aContext A pointer to application-specific context.
  • Hi,

    NULL should work fine as an argument to aContext, I believe the Hardfault must come from something else.

    I added the following changes to the coap_client example:

    void udp_receive_handle(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
    {
        char buf[1500];
        int  length;
    
        NRF_LOG_INFO("%d bytes from ", otMessageGetLength(aMessage) - otMessageGetOffset(aMessage));
        NRF_LOG_INFO("addr: %02x%02x:%02x%02x:%02x%02x", aMessageInfo->mPeerAddr.mFields.m8[0],
                                                         aMessageInfo->mPeerAddr.mFields.m8[1],
                                                         aMessageInfo->mPeerAddr.mFields.m8[2],
                                                         aMessageInfo->mPeerAddr.mFields.m8[3],
                                                         aMessageInfo->mPeerAddr.mFields.m8[4],
                                                         aMessageInfo->mPeerAddr.mFields.m8[5]);
    
        NRF_LOG_INFO("addr: %02x%02x:%02x%02x:%02x%02x", aMessageInfo->mPeerAddr.mFields.m8[6],
                                                         aMessageInfo->mPeerAddr.mFields.m8[7],
                                                         aMessageInfo->mPeerAddr.mFields.m8[8],
                                                         aMessageInfo->mPeerAddr.mFields.m8[9],
                                                         aMessageInfo->mPeerAddr.mFields.m8[10],
                                                         aMessageInfo->mPeerAddr.mFields.m8[11]);
    
        NRF_LOG_INFO("addr: %02x%02x:%02x%02x", aMessageInfo->mPeerAddr.mFields.m8[12],
                                                aMessageInfo->mPeerAddr.mFields.m8[13],
                                                aMessageInfo->mPeerAddr.mFields.m8[14],
                                                aMessageInfo->mPeerAddr.mFields.m8[15]);
    
        NRF_LOG_INFO("port: %d ", aMessageInfo->mPeerPort);
    
        length      = otMessageRead(aMessage, otMessageGetOffset(aMessage), buf, sizeof(buf) - 1);
        buf[length] = '\0';
    
        NRF_LOG_HEXDUMP_INFO(buf, length);
    }
    
    /***************************************************************************************************
     * @section Main
     **************************************************************************************************/
    
    int main(int argc, char * argv[])
    {
        log_init();
        scheduler_init();
        timer_init();
    
        thread_instance_init();
        thread_coap_init();
        thread_bsp_init();
    
        otUdpSocket mSocket;
    
        memset(&mSocket, 0x00, sizeof(mSocket));
    
        otError err_code = otUdpOpen(thread_ot_instance_get(), &mSocket, udp_receive_handle, NULL);
        APP_ERROR_CHECK(err_code);
        
    
        otSockAddr sockaddr;
        sockaddr.mPort = 1234;
    
        otIp6AddressFromString("::", &sockaddr.mAddress);
    
        err_code = otUdpBind(&mSocket, &sockaddr);
        APP_ERROR_CHECK(err_code);
      
    
        while (true)
        {
            thread_process();
            app_sched_execute();
    
            if (NRF_LOG_PROCESS() == false)
            {
                thread_sleep();
            }
        }
    }

    I can then send UDP messages from the CLI example using following commands:

    > udp open
    Done
    > udp send fdde:ad00:beef:0:0:ff:fe00:7000 1234 hello
    Done

    The address in above command is the RLOC address of the CLI node that I got from "ipaddr" and "router table" commands (2nd address from "ipaddr" command is full RLOC address, and the last 4 octets are taken from the RLOC16 in "router table" list):

    > router table
    | ID | RLOC16 | Next Hop | Path Cost | LQ In | LQ Out | Age | Extended MAC     |
    +----+--------+----------+-----------+-------+--------+-----+------------------+
    | 28 | 0x7000 |       63 |         0 |     3 |      3 |  14 | b634bfe3eca7ad8f |
    | 51 | 0xcc00 |       63 |         0 |     0 |      0 |   0 | 2ebfa17ed80d3fc4 |
    
    Done
    > ipaddr
    fdde:ad00:beef:0:0:ff:fe00:fc00
    fdde:ad00:beef:0:0:ff:fe00:cc00
    fdde:ad00:beef:0:da3f:c12:a9a2:9ed
    fe80:0:0:0:2cbf:a17e:d80d:3fc4
    Done
    

    The modified CoAP client example will then output the received message on the RTT log:

    <info> app: 5 bytes from 
    <info> app: addr: FDDE:AD00:BEEF
    <info> app: addr: 0000:0000:00FF
    <info> app: addr: FE00:CC00
    <info> app: port: 49154 
    <info> app:  68 65 6C 6C 6F         |hello 

    Best regards,
    Jørgen

  • Hi Jorgen,

    I am Sunil,

    I am looking for the caop UDP example and found this ticket i am able to receive the text which i send from cli using this command

    "udp send fdde:ad00:beef:0:2e44:8255:71b7:94ea 1234 hi"

    and i  receive

    <info> app: 2 bytes from
    <info> app: addr: FDDE:AD00:BEEF
    <info> app: addr: 0000:AE59:F903
    <info> app: addr: E6A1:2955
    <info> app: port: 49154
    <info> app:  68 69                  |hi 

    and my question is

    1. How to send the text message from program using udp

    2. the udp_receive_handle doesn't capture the button action . how to read the message transfer to server when pressing the button 2

  • Hi Jorgen,

    I am Sunil,

    I am looking for the caop UDP example and found this ticket i am able to receive the text which i send from cli using this command

    "udp send fdde:ad00:beef:0:2e44:8255:71b7:94ea 1234 hi"

    and i  receive

    <info> app: 2 bytes from
    <info> app: addr: FDDE:AD00:BEEF
    <info> app: addr: 0000:AE59:F903
    <info> app: addr: E6A1:2955
    <info> app: port: 49154
    <info> app:  68 69                  |hi 

    and my question is

    1. How to send the text message from program using udp

    2. the udp_receive_handle doesn't capture the button action . how to read the message transfer to server when pressing the button 2

Related