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
Parents Reply Children
  • 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
  • Hi,

    We are currently developing an application using the nRF52840 Dongle and we found the same issue.
    Udp messages are correctly sent using CLI example ( under usb_bootloader folder) but when we try to modify the code to send the same message nothing happens, even if no errror is encountered by calling OpenThread API. 

    We decided to rebuild the libraries as you suggested but we encountered some problems including them inside Segger Embedded studio project (we try to modify the same example above).

    Can you provide us with some more information about your procedures? Do you have any code example can we can study to learn and resolve our issues?

    Edit: we resolved problems related to dependencies but nothing changed.

    No error during code but UDP messages sent only through CLI, not working by code.

    Any ideas?



    Thanks in advance! Best regards.

  • Hi Giorgio,

    I've rebuild OT libraries following this guide https://www.nordicsemi.com/DocLib/Content/SDK_Doc/Thread_SDK/v2-0-0/thread_intro?12#thread_qsg_libraries (in case of any problems, look on official OT guide: https://openthread.io/guides/build). Only problem I've found was that otUdpNewMessage() takes different 2 argument which I've changed in MQTT.

    I've made a script for easy OT libs update, just clone OT repo in the same library that you have SDK.

    #!/bin/sh
     
    FLAGS= \
    COMMISSIONER=1 \
    JOINER=1 \
    COAP=1 \
    DNS_CLIENT=1 \
    MTD_NETDIAG=1 \
    BORDER_ROUTER=1 \
    MAC_FILTER=1 \
    UDP_PROXY=1 \
    BORDER_AGENT=1 \
    USB=1 \
    
     
    SDK_DIR=`pwd`
    cd ../openthread/
    OT_DIR=`pwd`
    ./script/bootstrap
    ./bootstrap
    make -f examples/Makefile-nrf52840 $FLAGS
    mv $OT_DIR/output/nrf52840/lib/libopenthread-nrf52840-sdk.a $OT_DIR/output/nrf52840/lib/libopenthread-nrf528 40-sdk-usb.a
    mv $OT_DIR/output/nrf52840/lib/libopenthread-nrf52840-softdevice-sdk.a $OT_DIR/output/nrf52840/lib/libopenth read-nrf52840-sdk-softdevice-usb.a
    cp $OT_DIR/output/nrf52840/lib/* $SDK_DIR/external/openthread/lib/gcc/
    cp -r $OT_DIR/output/include/openthread/* $SDK_DIR/external/openthread/include/openthread/
    cp $OT_DIR/examples/platforms/nrf52840/platform-softdevice.h $SDK_DIR/external/openthread/include/openthread /platform/
    cp $OT_DIR/examples/platforms/nrf52840/platform-fem.h $SDK_DIR/external/openthread/include/openthread/platfo rm/
    cp $OT_DIR/examples/platforms/openthread-system.h $SDK_DIR/external/openthread/include/openthread/platform/

    Best Regards,

    Maciej Bożyk

  • Hi Maciej,

    Thanks for your help! 

    Unfortunately we are here again... We decided to start from zero. 

    We downloaded the nRF Thread & Zigbee SDK and clone the OT repo again.

    Then we followed your instructions using the script you provided but this time nothing works... We tried to build the CLI example ( under usb_bootloader folder) as we did before and again Segger Embedded Studio had some dependencies problem that we fixed adding the libraries : 1) libnordicsemi-nrf52840-radio-driver.a 2) libnordicsemi-nrf52840-radio-driver-softdevice.a.

    Done that, we compiled and put the .hex file in the nRF52840 Dongle using nRF connect. This time the device is not recognised by the PC and we cannot open the cli as we did before.

    What could have gone wrong? 

    Do you have any complete example code we can look at some repo? That would be fantastic.

    Thanks for you help and support, 

    Best regards

    Giorgio 

Related