2

I want to send integers via RF-module nRF24l01 from my Pi (library: https://github.com/jpbarraca/pynrf24) to my Arduino Pro Mini (library: http://maniacbug.github.io/RF24/index.html).

I am able to get a a basic connection and send a char 'a' and interpret it right on Arduinos side.

RPi code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import time, sys
from nrf24 import NRF24

pipes = [ [0xF0, 0xF0, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xD2] ] #40-bit hex adressen der pipes

#nRF24L01 Setup
radio = NRF24()
radio.begin(0,0,22, 23) #Set CE and IRQ pins
radio.setRetries(15,15) #zeit zwischen retries (x * 250mikro s) und anzahl an retries
radio.setPayloadSize(1) #32-byte payload
radio.setChannel(0x4c) #0-127 in hex
radio.setDataRate(NRF24.BR_250KBPS) 
radio.setPALevel(NRF24.PA_MAX) #power amplifier level
radio.openWritingPipe(pipes[0]) 
radio.openReadingPipe(1,pipes[1])
radio.printDetails() #Debug
print(time.strftime("%d.%m.%Y %H:%M:%S", time.localtime()) + " ------ PyNRF start ------")

try:
    # main code
    while True:

        data = ['a']

        result = radio.write(data)

        print(time.strftime("%H:%M:%S", time.localtime()) + " data send --- " + str(result))

        time.sleep(5)

except KeyboardInterrupt:
    sys.exit(0)

Arduino code:

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"

//nRF24 stuff
RF24 radio(8,9);
const uint64_t pipes[1] = { 0xF0F0F0F0E1LL };

void setup(void) {
  Serial.begin(57600); //Debug  

  //nRF24 stuff
  radio.begin();
  radio.setRetries(15,15);
  radio.setDataRate(RF24_250KBPS);
  radio.setPayloadSize(1);
  radio.openReadingPipe(1,pipes[0]);
  radio.startListening();
}

void loop() {
  if ( radio.available() ) {
    char data[1];
    bool done = false;
    while (!done) {
      done = radio.read( &data, sizeof(data) );
    }
    Serial.println(data[0]);
  }
}

But when i try to send an integer (lets say 5), i get as output 31493

RPi code: same as above, except data = [5]

Arduino code: same as above, except int data[1];

My first thought was changing payload size to 2 byte on both sides, since integers on arduino side are 16-bit and on RPi too (?), but that just gave me as output 31749

Where am I struggling here?

1
  • Couldn't you just sent a byte, instead of a int? The RF24 library only send and receives bytes. If you are going to sent 16-bit integers, you'd need to change setPayloadSize, to radio.setPayloadSize(2);. You'll also need to split the int into 2 bytes, and combine the 2 bytes into an int at the other end. Arduino has some functions to make this easier; word(h, l), highByte(x) and lowByte().
    – Gerben
    Commented Mar 8, 2014 at 19:33

3 Answers 3

1

There are a couple of things involved in this.

First, integers in Python are not 2 bytes and you should not relay on their representation being the same at both ends. You should define a format for send integers as bytes "over the air" and break/reconstruct on both sides. For example, you could send two bytes, the fist being (val>>8)&0xFF and the second val&0xFF. This convention will work for positive integer from 0 to 32767.

Second, you need to understand how a sequence of bytes is send/read at each side. In pynrf24 you should use a list:

data = [ (val>>8)&0xFF, val&0xFF ]
result = radio.write(data)

On the Arduino, you will use a array of bytes

int val;
char data[2];
...
done = radio.read( &data, sizeof(data) );
val = (data[0] << 8) + data[1]
0

Try sending this in the Python code

data = ['1']

Then you can use

Serial.println(data[0]);

in the Arduino to print it.

0

From the Sending Data example:

status = radio.write("HELLO")

it looks like you can send a primitive down the pipe rather than a list; that may be causing the problem. Try changing your code to this and see if it works:

data = 1
result = radio.write(data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.