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?
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)
andlowByte()
.