In a previous post I described a very simple setup to send and receive data using the NRF24L01 breakouts that can be purchased (very!) inexpensively on eBay. I wanted to show another sketch, using basically the same physical setup, to send and receive text from one arduino to another. Forgive the mostly copy-and-paste nature of this post, if you are just looking for the code, scroll to the bottom.
You will need (hardware):
(2) nrf24L01 2.4ghz wireless transcievers
(2) Arduino Uno (or compatible)
Recommended: male-female jumpers for connecting nrf24L01 modules to Arduino
You will need (software):
RF24 libraries by maniacbug (https://github.com/maniacbug/RF24)
The image below shows the view of the nrf24L01 from the top. Note: the pins are on the bottom-side. The image shows the top-side.
For this sketch, both arduino boards will need to be setup like this:
Upload the send and receive sketches to corresponding Arduinos physically configured as above. The sending unit will take a string and break it down into individual characters, then send each character until the end of the string is reached. After that, it sends a ‘termination message’, in this case, the integer ‘2’. This tells the receiving end that the message is complete. The sending unit then powers down it’s radio for one second, then powers it back up and runs through the loop again.
You will see some comments in the code about this delay period causing some occasional lost data when sending. I’m hoping someone with more experience with the nrf24 radios can comment on this and provide some code improvements, but this method seems to work well for the most part. I have added an alternative receive-side sketch below that checks for a proper message length as a sort of ‘checksum’ function.
On the receiving end, the sketch begins listening for available messages. Once it begins receiving characters, it begins appending them to the receive string. It continues to append characters until it receives the termination character (again, the integer ‘2’ in this case, but you could set it to something else). It then prints the complete string to serial.
There is a second version of the receive sketch (receive_string_withChecksum) that also checks the final string length against an expected message length. If the actual received message length does not match the expected length, the string is rejected and the radio begins listening for a new message. Obviously, this will only work if you expect a message of a certain fixed size every time.