Here we go, transmitter is an ATmega328P, receiver is a DUE.
Transmiter;
/*******************************************************************************************************
Programs for Arduino - Copyright of the author Stuart Robinson - 04/10/21
This program is supplied as is, it is up to the user of the program to decide if the program is
suitable for the intended purpose and free from errors.
*******************************************************************************************************/
/*******************************************************************************************************
Program Operation - This is a demonstration of using the nRF24L01 to transmit a memory array, data[].
The array contains a series of variables that represent the output from a GPS tracker. A set of
functions on this transmitter are used to write each variable in turn into the array, with specific
functions for each variable type. On the receiver the variables must be read out of the received array
in the same order. The LED flashes when a packet is transmitted.
There are read\write array functions for these variable types; uint8_t, int8_t, char, uint16_t, int16_t,
float, uint32_t, int32_t, character arrays and byte arrays.
Serial monitor baud rate should be set at 115200.
*******************************************************************************************************/
#include <SPI.h>
#include "RF24.h" //get library here; https://github.com/nRF24/RF24
#define CE_PIN 6 //nRF24L01 CE pin
#define CSN_PIN 10 //nRF24L01 CSN pin
#define LED1 8 //LED pin, high for LED on
RF24 radio(CE_PIN, CSN_PIN);
const uint64_t pipes[2] = { 0x1111111111LL, 0x2222222222LL}; //pipe addresses
uint8_t data[32]; //max packet size is 32 bytes
uint16_t TXPacketsFail;
const uint32_t packetdelaymS = 1000; //delay between packets
//variables to send
uint32_t TXPacketNum;
float latitude = 51.23456;
float longitude = -3.12345;
uint16_t altitude = 199;
uint8_t satellites = 8;
uint16_t voltage = 3999;
int8_t temperature = -9;
uint8_t TXPacketL;
void loop()
{
beginarrayRW(data, 0); //start the array write at location 0
arrayWriteUint32(TXPacketNum);
arrayWriteFloat(latitude);
arrayWriteFloat(longitude);
arrayWriteUint16(altitude);
arrayWriteUint8(satellites);
arrayWriteUint16(voltage);
arrayWriteInt8(temperature);
TXPacketL = endarrayRW(); //this function returns the length of the payload to send, 18 bytes in this case
if (!radio.writeFast(&data, TXPacketL))
{
TXPacketsFail++;
Serial.print(F("Packets failed "));
Serial.println(TXPacketsFail);
}
digitalWrite(LED1, HIGH);
radio.txStandBy();
Serial.print("Packet ");
Serial.print(TXPacketNum);
Serial.print(" length ");
Serial.print(TXPacketL);
Serial.print(" > ");
printarrayHEX(data, TXPacketL); //print out hex bytes of array
Serial.println();
digitalWrite(LED1, LOW);
TXPacketNum++;
delay(packetdelaymS);
}
void setup()
{
pinMode(LED1, 8);
Serial.begin(115200);
Serial.println();
Serial.println(F(__FILE__));
Serial.println();
radio.begin();
radio.setChannel(1);
//radio.setPALevel(RF24_PA_MAX); //choose power level
//radio.setPALevel(RF24_PA_HIGH);
radio.setPALevel(RF24_PA_LOW);
//radio.setDataRate(RF24_2MBPS); //choose data rate
radio.setDataRate(RF24_250KBPS);
radio.setAutoAck(0); //no ack is used
radio.setCRCLength(RF24_CRC_16); //using 16 bit CRC
radio.openWritingPipe(pipes[1]);
radio.enableDynamicPayloads();
radio.stopListening();
//radio.printDetails(); //print RF24 config
radio.powerUp();
Serial.println("Transmitter ready");
Serial.println();
}
/*
arrayRW library routines
Copyright 2021 - Stuart Robinson
Licensed under a MIT license
Original published 06/05/21
*/
uint32_t _arraylcn;
uint8_t *_arrayaddress;
void printArrayLocation(uint8_t num);
void beginarrayRW(uint8_t *buff, uint32_t lcn)
{
_arrayaddress = buff;
_arraylcn = lcn; //sets the current location in the array
}
uint8_t endarrayRW()
{
return _arraylcn; //return the length of the array written
}
void printarrayHEX(uint8_t *buff, uint32_t len)
{
uint8_t index, buffdata;
for (index = 0; index < len; index++)
{
buffdata = buff[index];
if (buffdata < 16)
{
Serial.print(F("0"));
}
Serial.print(buffdata, HEX);
Serial.print(F(" "));
}
}
void printArrayLocation(uint8_t num)
{
Serial.print(num);
Serial.print(F(",_arraylcn,"));
Serial.println(_arraylcn);
}
//RW uint8_t ***************************************
void arrayWriteUint8(uint8_t buffdata)
{
_arrayaddress[_arraylcn++] = buffdata;
}
uint8_t arrayReadUint8()
{
return _arrayaddress[_arraylcn++];
}
//**************************************************
//RW int8_t ****************************************
void arrayWriteInt8(int8_t buffdata)
{
_arrayaddress[_arraylcn++] = buffdata;
}
int8_t arrayReadInt8()
{
return _arrayaddress[_arraylcn++];
}
//**************************************************
//RW char ******************************************
void arrayWriteChar(char buffdata)
{
_arrayaddress[_arraylcn++] = buffdata;
}
char arrayReadChar()
{
return _arrayaddress[_arraylcn++];
}
//**************************************************
//RW uint16_t *************************************
void arrayWriteUint16(uint16_t buffdata)
{
_arrayaddress[_arraylcn++] = lowByte(buffdata);
_arrayaddress[_arraylcn++] = highByte(buffdata);
}
uint16_t arrayReadUint16()
{
uint16_t buffdata;
buffdata = _arrayaddress[_arraylcn++];
buffdata = buffdata + (_arrayaddress[_arraylcn++] << 8);
return buffdata;
}
//**************************************************
//RW int16_t ***************************************
void arrayWriteInt16(int16_t buffdata)
{
_arrayaddress[_arraylcn++] = lowByte(buffdata);
_arrayaddress[_arraylcn++] = highByte(buffdata);
}
int16_t arrayReadInt16()
{
uint16_t buffdata;
buffdata = _arrayaddress[_arraylcn++];
buffdata = buffdata + (_arrayaddress[_arraylcn++] << 8);
return buffdata;
}
//**************************************************
//RW float *****************************************
void arrayWriteFloat(float buffdata)
{
memcpy ((_arrayaddress + _arraylcn), &buffdata, 4);
_arraylcn = _arraylcn + 4;
}
float arrayReadFloat()
{
float tempf;
memcpy (&tempf, (_arrayaddress + _arraylcn), 4);
_arraylcn = _arraylcn + 4;
return tempf;
}
//**************************************************
//RW uint32_t **************************************
void arrayWriteUint32(uint32_t buffdata)
{
memcpy ((_arrayaddress + _arraylcn), &buffdata, 4);
_arraylcn = _arraylcn + 4;
}
uint32_t arrayReadUint32()
{
uint32_t tempdata;
memcpy (&tempdata, (_arrayaddress + _arraylcn), 4);
_arraylcn = _arraylcn + 4;
return tempdata;
}
//**************************************************
//RW int32_t ***************************************
void arrayWriteInt32(int32_t buffdata)
{
memcpy ((_arrayaddress + _arraylcn), &buffdata, 4);
_arraylcn = _arraylcn + 4;
}
int32_t arrayReadInt32()
{
int32_t tempdata;
memcpy (&tempdata, (_arrayaddress + _arraylcn), 4);
_arraylcn = _arraylcn + 4;
return tempdata;
}
//**************************************************
//RW character arrays ******************************
void arrayWriteCharArray(char *buff, uint8_t len)
{
uint8_t index = 0;
for (index = 0; index < len; index++)
{
_arrayaddress[_arraylcn] = buff[index];
_arraylcn++;
}
}
void arrayReadCharArray(char *buff, uint8_t len)
{
uint8_t index = 0;
for (index = 0; index < len; index++)
{
buff[index] = _arrayaddress[_arraylcn];
_arraylcn++;
}
}
//*************************************************
//RW byte (uint8_t) arrays ************************
void arrayWriteByteArray(uint8_t *buff, uint8_t len)
{
uint8_t index = 0;
for (index = 0; index < len; index++)
{
_arrayaddress[_arraylcn] = buff[index];
_arraylcn++;
}
}
void arrayReadByteArray(uint8_t *buff, uint8_t len)
{
uint8_t index = 0;
for (index = 0; index < len; index++)
{
buff[index] = _arrayaddress[_arraylcn];
_arraylcn++;
}
}
//**************************************************
And receiver;
/*******************************************************************************************************
Programs for Arduino - Copyright of the author Stuart Robinson - 04/10/21
This program is supplied as is, it is up to the user of the program to decide if the program is
suitable for the intended purpose and free from errors.
*******************************************************************************************************/
/*******************************************************************************************************
Program Operation - This is a demonstration of using the nRF24L01 to receive a memory array, data[].
The array contains a series of variables that represent the output from a GPS tracker. A set of
functions on the transmitter are used to write each variable in turn into the array, with specific
functions for each variable type. On the receiver the variables must be read out of the received array
in the same order. The LED flashes when a packet is received.
There are read\write array functions for these variable types; uint8_t, int8_t, char, uint16_t, int16_t,
float, uint32_t, int32_t, character arrays and byte arrays.
Serial monitor baud rate should be set at 115200.
*******************************************************************************************************/
#include <SPI.h>
#include "RF24.h" //get library here; https://github.com/nRF24/RF24
#define CE_PIN 6 //nRF24L01 CE pin
#define CSN_PIN 10 //nRF24L01 CSN pin
#define LED1 8 //LED pin, high for LED on
RF24 radio(CE_PIN, CSN_PIN);
const uint64_t pipes[2] = { 0x111111111111LL, 0x222222222222LL}; //pipe addresses
uint8_t data[32]; //max packet size is 32 bytes
uint8_t RXPacketL;
//variables to receive
uint32_t RXPacketNum;
float latitude;
float longitude;
uint16_t altitude;
uint8_t satellites;
uint16_t voltage;
int8_t temperature;
void loop()
{
while (radio.available())
{
digitalWrite(LED1, HIGH);
RXPacketL = radio.getDynamicPayloadSize();
radio.read(&data, RXPacketL);
beginarrayRW(data, 0); //start the read at location 0 in array
RXPacketNum = arrayReadUint32();
latitude = arrayReadFloat();
longitude = arrayReadFloat();
altitude = arrayReadUint16();
satellites = arrayReadUint8();
voltage = arrayReadUint16();
temperature = arrayReadInt8();
Serial.print("Packet ");
Serial.print(RXPacketNum);
Serial.print(" length ");
Serial.print(RXPacketL);
Serial.print(" > ");
printarrayHEX(data, RXPacketL); //print out hex bytes of array
Serial.println();
Serial.print(latitude, 5);
Serial.print(F(","));
Serial.print(longitude, 5);
Serial.print(F(","));
Serial.print(altitude);
Serial.print(F("m,"));
Serial.print(satellites);
Serial.print(F("sats,"));
Serial.print(voltage);
Serial.print(F("mV,"));
Serial.print(temperature);
Serial.print(F("c "));
Serial.println();
Serial.println();
digitalWrite(LED1, LOW);
}
}
void setup()
{
pinMode(LED1, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println(F(__FILE__));
Serial.println();
radio.begin();
radio.setChannel(1);
//radio.setPALevel(RF24_PA_MAX); //choose power level
//radio.setPALevel(RF24_PA_HIGH);
radio.setPALevel(RF24_PA_LOW);
//radio.setDataRate(RF24_2MBPS); //choose data rate
radio.setDataRate(RF24_250KBPS);
radio.setAutoAck(0); //no ack is used
radio.setCRCLength(RF24_CRC_16); //using 16 bit CRC
radio.openReadingPipe(1, pipes[1]);
radio.enableDynamicPayloads();
radio.startListening();
//radio.printDetails(); //print RF24 config
radio.powerUp();
Serial.println("Receiver ready");
Serial.println();
}
/*
arrayRW library routines
Copyright 2021 - Stuart Robinson
Licensed under a MIT license
Original published 06/05/21
*/
uint32_t _arraylcn;
uint8_t *_arrayaddress;
void printArrayLocation(uint8_t num);
void beginarrayRW(uint8_t *buff, uint32_t lcn)
{
_arrayaddress = buff;
_arraylcn = lcn; //sets the current location in the array
}
uint8_t endarrayRW()
{
return _arraylcn; //return the length of the array written
}
void printarrayHEX(uint8_t *buff, uint32_t len)
{
uint8_t index, buffdata;
for (index = 0; index < len; index++)
{
buffdata = buff[index];
if (buffdata < 16)
{
Serial.print(F("0"));
}
Serial.print(buffdata, HEX);
Serial.print(F(" "));
}
}
void printArrayLocation(uint8_t num)
{
Serial.print(num);
Serial.print(F(",_arraylcn,"));
Serial.println(_arraylcn);
}
//RW uint8_t ***************************************
void arrayWriteUint8(uint8_t buffdata)
{
_arrayaddress[_arraylcn++] = buffdata;
}
uint8_t arrayReadUint8()
{
return _arrayaddress[_arraylcn++];
}
//**************************************************
//RW int8_t ****************************************
void arrayWriteInt8(int8_t buffdata)
{
_arrayaddress[_arraylcn++] = buffdata;
}
int8_t arrayReadInt8()
{
return _arrayaddress[_arraylcn++];
}
//**************************************************
//RW char ******************************************
void arrayWriteChar(char buffdata)
{
_arrayaddress[_arraylcn++] = buffdata;
}
char arrayReadChar()
{
return _arrayaddress[_arraylcn++];
}
//**************************************************
//RW uint16_t *************************************
void arrayWriteUint16(uint16_t buffdata)
{
_arrayaddress[_arraylcn++] = lowByte(buffdata);
_arrayaddress[_arraylcn++] = highByte(buffdata);
}
uint16_t arrayReadUint16()
{
uint16_t buffdata;
buffdata = _arrayaddress[_arraylcn++];
buffdata = buffdata + (_arrayaddress[_arraylcn++] << 8);
return buffdata;
}
//**************************************************
//RW int16_t ***************************************
void arrayWriteInt16(int16_t buffdata)
{
_arrayaddress[_arraylcn++] = lowByte(buffdata);
_arrayaddress[_arraylcn++] = highByte(buffdata);
}
int16_t arrayReadInt16()
{
uint16_t buffdata;
buffdata = _arrayaddress[_arraylcn++];
buffdata = buffdata + (_arrayaddress[_arraylcn++] << 8);
return buffdata;
}
//**************************************************
//RW float *****************************************
void arrayWriteFloat(float buffdata)
{
memcpy ((_arrayaddress + _arraylcn), &buffdata, 4);
_arraylcn = _arraylcn + 4;
}
float arrayReadFloat()
{
float tempf;
memcpy (&tempf, (_arrayaddress + _arraylcn), 4);
_arraylcn = _arraylcn + 4;
return tempf;
}
//**************************************************
//RW uint32_t **************************************
void arrayWriteUint32(uint32_t buffdata)
{
memcpy ((_arrayaddress + _arraylcn), &buffdata, 4);
_arraylcn = _arraylcn + 4;
}
uint32_t arrayReadUint32()
{
uint32_t tempdata;
memcpy (&tempdata, (_arrayaddress + _arraylcn), 4);
_arraylcn = _arraylcn + 4;
return tempdata;
}
//**************************************************
//RW int32_t ***************************************
void arrayWriteInt32(int32_t buffdata)
{
memcpy ((_arrayaddress + _arraylcn), &buffdata, 4);
_arraylcn = _arraylcn + 4;
}
int32_t arrayReadInt32()
{
int32_t tempdata;
memcpy (&tempdata, (_arrayaddress + _arraylcn), 4);
_arraylcn = _arraylcn + 4;
return tempdata;
}
//*************************************************
//RW character arrays *****************************
void arrayWriteCharArray(char *buff, uint8_t len)
{
uint8_t index = 0;
for (index = 0; index < len; index++)
{
_arrayaddress[_arraylcn] = buff[index];
_arraylcn++;
}
}
void arrayReadCharArray(char *buff, uint8_t len)
{
uint8_t index = 0;
for (index = 0; index < len; index++)
{
buff[index] = _arrayaddress[_arraylcn];
_arraylcn++;
}
}
//*************************************************
//RW byte (uint8_t) arrays ************************
void arrayWriteByteArray(uint8_t *buff, uint8_t len)
{
uint8_t index = 0;
for (index = 0; index < len; index++)
{
_arrayaddress[_arraylcn] = buff[index];
_arraylcn++;
}
}
void arrayReadByteArray(uint8_t *buff, uint8_t len)
{
uint8_t index = 0;
for (index = 0; index < len; index++)
{
buff[index] = _arrayaddress[_arraylcn];
_arraylcn++;
}
}
//*************************************************