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
  • UPDATE (11-01-2019):

    We have rewritten some of the code and every step of the UDP process returns OK.

    Sadly, the other nodes do not receive te message.


    void udpSend2(){
        const char buf[100] = "Hallo\0";
        otMessageInfo messageInfo;
        otInstance *myInstance;
        myInstance = thread_ot_instance_get();
        otUdpSocket mySocket;
    
        memset(&messageInfo, 0, sizeof(messageInfo));
    
        otIp6AddressFromString("ff03::1", &messageInfo.mPeerAddr);
        messageInfo.mPeerPort = 1994;
        messageInfo.mInterfaceId = OT_NETIF_INTERFACE_ID_THREAD;
    
        otCliUartOutputFormat(otThreadErrorToString(otUdpOpen(myInstance, &mySocket, NULL, NULL)));
    
        otMessage *test_Message = otUdpNewMessage(myInstance, NULL);
    
        otCliUartOutputFormat(otThreadErrorToString(otMessageAppend(test_Message, buf, (uint16_t)strlen(buf))));
    
    	otError errorUdpSend = otUdpSend(&mySocket, test_Message, &messageInfo)
        
        otCliUartOutputFormat(otThreadErrorToString(errorUdpSend));
    
        otCliUartOutputFormat("Done\0");
    
        otCliUartOutputFormat(otThreadErrorToString(otUdpClose(&mySocket)));
    	
    	if(errorUdpSend != OT_ERROR_NONE && test_Message != NULL)
    	{
    		otMessageFree(test_Message);
    	}
    }

    We tested the code with the CLI_UDP_Example_Modified (thanks to Jonathan H u-i) and it worked. The only thing that seems to be different is the way the Instance is handled. Is there a problem with our instance calling or?

    Thanks!

    Jonathan Overes

  • Hi,

    I'm have the same issue, but I'm working on mqttsn_client_subscriber example. I've added cli module and it works from cli, but sending udp with code (I connected it to a button) doesn't - on the same time. I have tried with/without scheduler to call it in the main loop and other different approaches, but nothing helps. I'm unable to send udp outside the cli.

    Best Regards

    Maciej Bozyk

  • Hi,

    I also had this problem. I have rebuild openthread libraries (infocenter.nordicsemi.com/.../thread_intro.html and now udp packets are working. Additionaly, mqtt have now wrong usage of otUdpNewMessage in 2 parameter (don't remember exactly).

    Best Regards

    Maciej Bozyk

  • Hi,

    I don't know which problem causes the Nordic CLI Example to not receive the messages. We switched to the OpenThread CLI example that you can build with the nRF52840 (if that is the board you are using) makefile that is available in the official OpenThread github repo. This CLI does support the UDP communication from within the code, instead of manually sending messages through the CLI.

    Hope that helped!

    Kind regards,

    Jonathan

  • Hi Jonathan.O,

    As you mentioned in above comment that,

    "This CLI does support the UDP communication from within the code, instead of manually sending messages through the CLI" -  

    Can you please provide example or link of this.

    Thanks,
    Kartik
Related