user15847: I have 2 Arduinos communicating with each other. My problem is that one sends packets every 1.5-2 seconds but the other Arduino does sensor reading and data uploading with a Wifi module, which may take up to 5 seconds until the next time the nRF24L01+ is available to receive data.
This results in about 4 packet losses and if I am lucky sometimes 2. Is there any way that when the nRF24L01+ sends data, the other Arduino stops everything to get the data? Like a communication between these two Arduinos that will be open 24/7, so that the other Arduino can multitask (reading sensors and upload the data through wifi).
I am using the code from this tutorial. Here is a my problem in an image (I hope this helps you understand it). Thanks in advance. 

user4143: You need to re-factor your program somewhat (I would imagine).  The nRF needs to be read at the same time as doing other things. That means interleaving it with your other operations.
There is a concept called a finite state machine which is the best way of writing a complex program like this. The idea is that nothing ever blocks, it just changes states. And all the time other things can be happening, such as reading the nRF24L01+ module.
I have a little tutorial on the Finite State Machine here:

https://hackingmajenkoblog.wordpress.com/2016/02/01/the-finite-state-machine/

If you use the Finite State Machine concept to run the reading of your sensors and the writing of data to the WiFi the nRF can then be polled constantly outside of the FSM constantly - maybe with an FSM of its own to deal with reception etc.
Things to remember:

Avoid delay(), certainly for anything longer than a couple of ms.
Avoid while(), for(), etc except for very short loops that finish quickly.
Any operation that takes more than a few ms to complete should be broken down into a number of sub-operations and coded as separate states of the FSM.


user21205: I see this is an old thread, but thought I'd chime in.
You could accomplish the same "finite state machine" functionality with a real-time operating system built for microcontrollers. I can recommend FreeRTOS as a user-friendly entry to this. You can define several continually running while loops that the RTOS thread handler then interleaves for consistent execution. You could have 1 thread for your RF and 1 thread for your WiFi, with set execution rates, and RTOS handles the rest, e.g. it will try to fit RF reads inside of the Wifi loop whenever it can find downtime. This will likely make your Wifi loop somewhat longer as a sacrifice.
You can get started with these:
FreeRTOS Kernel Quick Start Guide
The FreeRTOS Reference Manual

15847: <p>I have 2 Arduinos communicating with each other. My problem is that one sends packets every 1.5-2 seconds but the other Arduino does sensor reading and data uploading with a Wifi module, which may take up to 5 seconds until the next time the nRF24L01+ is available to receive data.</p>

<p>This results in about 4 packet losses and if I am lucky sometimes 2. Is there any way that when the nRF24L01+ sends data, the other Arduino stops everything to get the data? Like a communication between these two Arduinos that will be open 24/7, so that the other Arduino can multitask (reading sensors and upload the data through wifi).</p>

<p>I am using the code from this <a href="https://www.youtube.com/watch?v=D40cgHyBLL4" rel="nofollow noreferrer">tutorial</a>. Here is a my problem in an image (I hope this helps you understand it). Thanks in advance. <a href="https://i.stack.imgur.com/oz1os.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oz1os.jpg" alt="enter image description here"></a></p>
 4143: <p>You need to re-factor your program somewhat (I would imagine).  The nRF needs to be read <em>at the same time</em> as doing other things. That means interleaving it with your other operations.</p>

<p>There is a concept called a <a href="https://en.wikipedia.org/wiki/Finite-state_machine" rel="nofollow">finite state machine</a> which is the best way of writing a complex program like this. The idea is that nothing ever <em>blocks</em>, it just changes states. And all the time other things can be happening, such as reading the nRF24L01+ module.</p>

<p>I have a little tutorial on the Finite State Machine here:</p>

<ul>
<li><a href="https://hackingmajenkoblog.wordpress.com/2016/02/01/the-finite-state-machine/" rel="nofollow">https://hackingmajenkoblog.wordpress.com/2016/02/01/the-finite-state-machine/</a></li>
</ul>

<p>If you use the Finite State Machine concept to run the reading of your sensors and the writing of data to the WiFi the nRF can then be polled constantly outside of the FSM constantly - maybe with an FSM of its own to deal with reception etc.</p>

<p>Things to remember:</p>

<ul>
<li>Avoid <code>delay()</code>, certainly for anything longer than a couple of ms.</li>
<li>Avoid <code>while()</code>, <code>for()</code>, etc except for very short loops that finish quickly.</li>
<li>Any operation that takes more than a few ms to complete should be broken down into a number of sub-operations and coded as separate states of the FSM.</li>
</ul>
 21205: <p>I see this is an old thread, but thought I'd chime in.</p>
<p>You could accomplish the same &quot;finite state machine&quot; functionality with a real-time operating system built for microcontrollers. I can recommend FreeRTOS as a user-friendly entry to this. You can define several continually running while loops that the RTOS thread handler then interleaves for consistent execution. You could have 1 thread for your RF and 1 thread for your WiFi, with set execution rates, and RTOS handles the rest, e.g. it will try to fit RF reads inside of the Wifi loop whenever it can find downtime. This will likely make your Wifi loop somewhat longer as a sacrifice.
You can get started with these:
<a href="https://www.freertos.org/FreeRTOS-quick-start-guide.html#page_top" rel="nofollow noreferrer">FreeRTOS Kernel Quick Start Guide</a>
<a href="https://www.freertos.org/fr-content-src/uploads/2018/07/FreeRTOS_Reference_Manual_V10.0.0.pdf" rel="nofollow noreferrer">The FreeRTOS Reference Manual</a></p>
