This article describes the ability to send and receive data using the nRF24L01 through the Java programming language from a PC.
The notion behind the idea came from a number of projects I was working upon where I had Arduino devices sending and receiving data through the nRF24L01 but needed a second Arduino to validate that my data was being transmitted correctly. In addition, I wanted to have my PC involved in the communications for a variety of purposes such as dashboards and as a proxy to the Internet. As a result of this, I asked myself how that might be achieved? The result is as shown in the following block diagram:
We have an Arduino Nano which has a USB port combined with an nRF24L01 on a circuit board. There will be more on this later but it is literally just two components with a cost of less than $5. We will call this the “Serial nRF24”. It attaches to the PC through a USB cable. To the PC, it then looks like a standard serial device with its own COM port. Data written to the COM port arrives at the Serial nRF24 while data written by the Serial nRF24 is available to be read from the COM port.
A breadboard layout for the Serial nRF24 may be:
Note that we have chosen CE to be pin 10 and CSN to be pin 9. The other pin selections are required. If you decide to use other pins, ensure you update the Arduino program to reflect your selection.
Running on the Serial nRF24 is a pre-written Arduino program (sketch) that is supplied with this article. There is nothing more to be said about this program other than you load it up one time.
Finally, we have a Java JAR file that you make available on the PC. This JAR file contains a class called NRF24Serial which exposes APIs that can be called by your own Java apps that run on the PC. Your Java apps have no idea how the NRF24Serial class is implemented but, between you and me, the class reads and writes to the COM port which thus reads and writes to the Serial nRF24 which in turn interacts with other nRF24s wirelessly.
The NRF24Serial java class has the following API:
Construct an instance of the class where the Serial nRF24 is found on the specified COM port.
Construct an instance of the class using the pre-populated SerialPort object as the interface to Serial nRF24.
Become a receiver and start listening for incoming packets of data.
Stop being a receiver and allow us to transmit outgoing packets of data.
void setTXAddr(String addr)
Specify the target address of the device to which we wish to transmit data.
void setRXAddr(String addr)
Specify our address against which other devices can transmit data to us.
void read(byte data)
Read data received over the network and place in the data array.
void write(byte data)
Write data over the network to the partner device.
void setRate(int rate)
Set our communication rate over the wireless network. Values are:
- 1 – 1 MBps
- 2 – 2 MBps
Dump the details of the nRF24L01 device.
Test that we have a connection to our own nRF24L01 device and that all is well.
The Serial nRF24 Protocol
As we have seen, the Java API connects to the Serial nRF24 using USB, but what does the actual serial protocol look like? What if we want to drive the Serial nRF24 using a library other than Java?
Here we delve into the protocol used by Serial nRF24.
The protocol is based on an “AT” set of commands. Commands are sent as ASCII text messages and responses are received. Unless otherwise noted, all commands return one of two messages:
- “OK” – The command executed correctly.
- “FAIL” – The command failed. Following the FAIL indicator is a text string providing more details of the failure.
No parameters. Tests connection to the Serial nRF24.
Set the over the air transmission rate. The following values are supported:
- 1 – Set the transmission rate to be 1 MBps
- 2 – Set the transmission rate to be 2 MBps
Set the direction of the Serial nRF24. There are two possibilities:
- TX – Make the Serial nRF24 a transmitter of data.
- RX – Make the Serial nRF24 a receiver of data.
AT+RXADDR=Up to 5 characters (5*8 = 40 bits)
Set the address that the Serial nRF24 will appear as to other transmitters. This is our identity on the network.
AT+TXADDR=Up to 5 characters (5*8 = 40 bits)
Set the address that the Serial nRF24 will send data to on the next transmission.
Set the channel used for communications.
Transmit data. The length of the data is specified as a parameter. The actual data to be transmitted is sent immediately after the command.
Read data. The length of the data to be read is specified as a parameter. The actual data read is returned immediately after the command.
Dump the current state of the Serial nRF24 to the serial stream. This is used exclusively for debugging.
Set the power amplification level. The lower the level, the less power consumed by the device. Values are:
Returns “OK” if we have detected a carrier and “FAIL” otherwise.
Set the baud rate that the Serial nRF24 uses to communicate over serial. The default baud is 9600.
This project makes extensive use of the following Open Source modules
The project can be downloaded from Github.