Sleep in OpenThread application

163 Views Asked by At

I'm trying to build my own OpenThread application on two nrf52840 DK borads. When a specific button is pressed I want to start sending udp multicast in a specific time intervall. For now I have achieved that, when the button is pressed, the multicast is sent once. But is there any way that I can periodically send a udp multicast? I thought about using a while loop where the udp_send() methods is called and the some form of thread sleep is called. But I can't seem to find a sleep method that actually works.

Has anybody an idea how to put it into sleep?

2

There are 2 best solutions below

0
Morten Jensen On

The usual non-blocking method (i.e. without threads and sleep) is to

  1. send the packet
  2. take a timestamp
  3. continuously check if time_now >= (timestamp + 1 second) (for 1 second intervals, change accordingly)
  4. if so: send a packet, update the timestamp -> rinse and repeat
1
Frank Schrijver On

You should create a periodic task that sends a packet. Just exit the task at the end and the device should enter sleep.

Quoted from: Nordic Devzone

With the nRF Connect SDK and Zephyr there is a idle thread, and this idle thread will automatically enter system ON sleep mode (WFE). There is no need to do anything specifically for this to happen. As long as you don't have any other threads using the CPU, the CPU will be idle. Note that regardless of this you also need to ensure you don't use other resources on the chip or your board that consume power, like for instance UART logging, in order to achieve low power. I suggest you refer to the Power optimization chapter in the SDK documentation for an overview.