NRF24L01+ on the Raspberry Pi

Having struggled with the esp8266, I've decided the answer is something I have a little more direct control over. My first, and perhaps only target is the balancing robot.

I've started off using pynrf24:

$ git clone https://github.com/jpbarraca/pynrf24.git
$ cd pynrf24

This is hopefully the shortest path for me: I already have a python program that acts as the bridge between the browser and the balancing robot, so I'll just adapt it to communicate using the nrf24, and run it on Raspberry Pi.

On the downside, my app is a twisted app, so I guess I'll have to write a twisted driver.

I've connect the SPI pins as you would expect. CS0 is GPIO8 and it's connected to the NRF24 CS. I've connected GPIO24 to NRF24 IRQ.

First thing I find is I need this library for SPI, Install it with pip:

$ sudo pip install spidev

A bit of compilation later, and I am ready to try some stuff:

$ python
Python 2.7.9 (default, Mar  8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from nrf24 import NRF24
>>> radio = NRF24()
>>> radio.begin(0, 0, 8, 24)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "nrf24.py", line 281, in begin
    self.setCRCLength(NRF24.CRC_16)
  File "nrf24.py", line 797, in setCRCLength
    config = self.read_register(NRF24.CONFIG) & ~(NRF24.EN_CRC | NRF24.CRC0)

Hmm. Not so good.... Not sure the author has actually tested his changes recently, but it's just a typo in the library. Once I've fixed that:

$ python
Python 2.7.9 (default, Mar  8 2015, 00:52:26) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from nrf24 import NRF24
>>> import time
>>> pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]]
>>> radio = NRF24()
>>> radio.begin(0, 0, 8, 24)
>>> radio.setRetries(15,15)
>>> radio.setPayloadSize(8)
>>> radio.setChannel(0x60)
>>> radio.setDataRate(NRF24.BR_250KBPS)
True
>>> radio.setPALevel(NRF24.PA_MAX)
>>> radio.openWritingPipe(pipes[1])
>>> radio.openReadingPipe(1, pipes[0])
>>> radio.startListening()
>>> radio.stopListening()
>>> radio.printDetails()
STATUS          = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1    = 0xc2c2c2c2c2 0xe7e7e7e7e7
RX_ADDR_P2-5    = 0xc3 0xc4 0xc5 0xc6
TX_ADDR         = 0xc2c2c2c2c2
RX_PW_P0-6      = 0x08 0x08 0x00 0x00 0x00 0x00
EN_AA           = 0x3f
EN_RXADDR       = 0x03
RF_CH           = 0x60
RF_SETUP        = 0x26
SETUP_AW        = 0x03
OBSERVE_TX      = 0x00
CONFIG          = 0x0e
FIFO_STATUS     = 0x11
DYNPD           = 0x00
FEATURE         = 0x00
Data Rate       = 250KBPS
Model           = nRF24l01+
CRC Length      = 16 bits
PA Power        = PA_MAX
>>> while True:
...     radio.write("PING")
...     time.sleep(1)
... 
False

The above is just the examples/send.py entered into a session, edited a bit because it's an example for Beagle Bone.

It seems to work, so I've installed the library:

$ sudo python setup.py install

I've forked pynrf24 just to make the changes mentioned above.

Popular posts from this blog

3D Printer ramblings

Balance Bot V2

Robot arm