Skip to content

Commit

Permalink
Add 2 new examples
Browse files Browse the repository at this point in the history
New examples Demonstrate:
 Fragmentation and Reassembly:
 - nrf24l01+ radios can tx/rx 32 bytes of data per transmission
 - RF24Network will fragment and re-assemble payloads of any size
 - Use of differing sized payloads using peek() function

Also removed  unneeded printf.h files (Located in RF24 library now)
  • Loading branch information
TMRh20 committed Jul 16, 2020
1 parent 7628187 commit c1cb484
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 156 deletions.
23 changes: 15 additions & 8 deletions RF24Network_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@
/** System defines */
/** Maximum size of fragmented network frames and fragmentation cache.
*
* @note:
* The size of fragmented data is initialy indicated by the number of packets to be sent (Max 24 bytes data per packet)
* ie: A payload of 24 bytes requires 1 packet send. MAX_PAYLOAD_SIZE of 24 is fine.
* A payload of 25 requires 2 packets, so the MAX_PAYLOAD_SIZE must be 48 or higher.
* @note: Must be a multiple of 24!!!
* @note: If used with RF24Ethernet, this value is used to set the buffer sizes.
*/
#define MAX_PAYLOAD_SIZE 144

/** The size of the main buffer. This is the user-cache, where incoming data is stored.
* Data is stored using Frames: Header (8-bytes) + Frame_Size (2-bytes) + Data (?-bytes)
*
* @note The MAX_PAYLOAD_SIZE is (MAIN_BUFFER_SIZE - 10), and the result must be divisible by 24.
*
* @note The MAX_PAYLOAD_SIZE is (MAIN_BUFFER_SIZE - 10), and the result must be divisible by 24!!!
*/
#define MAIN_BUFFER_SIZE 144 + 10
#define MAIN_BUFFER_SIZE MAX_PAYLOAD_SIZE + 10

/** Maximum size of fragmented network frames and fragmentation cache. This MUST BE divisible by 24.
* @note: Must be a multiple of 24.
* @note: If used with RF24Ethernet, this value is used to set the buffer sizes.
*/
#define MAX_PAYLOAD_SIZE MAIN_BUFFER_SIZE-10

/** Disable user payloads. Saves memory when used with RF24Ethernet or software that uses external data.*/
//#define DISABLE_USER_PAYLOADS
Expand Down
37 changes: 0 additions & 37 deletions examples/Network_Ping/printf.h

This file was deleted.

37 changes: 0 additions & 37 deletions examples/Network_Ping_Sleep/printf.h

This file was deleted.

37 changes: 0 additions & 37 deletions examples/helloworld_rx/printf.h

This file was deleted.

74 changes: 74 additions & 0 deletions examples/helloworld_rx_advanced/helloworld_rx_advanced.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright (C) 2020 TMRh20([email protected])
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/

/**
* More advanced example of using RF24Network:
* Fragmentation and Reassembly:
* - nrf24l01+ radios can tx/rx 32 bytes of data per transmission
* - RF24Network will fragment and re-assemble payloads of any size
* Demonstrates use of differing sized payloads using peek() function
*
* RECEIVER NODE
* Every X milliseconds, send a payload to the receiver node.
*/

#include <RF24.h>
#include <RF24Network.h>
#include "printf.h"

RF24 radio(7,8); // nRF24L01(+) radio attached using Getting Started board

RF24Network network(radio); // Network uses that radio
const uint16_t this_node = 00; // Address of our node in Octal format ( 04,031, etc)
const uint16_t other_node = 01; // Address of the other node in Octal format

/**** Create a large array for data to be received ****
* MAX_PAYLOAD_SIZE is defined in RF24Network_config.h
* Payload sizes of ~1-2 KBytes or more are practical when radio conditions are good
*/
uint8_t dataBuffer[MAX_PAYLOAD_SIZE]; //MAX_PAYLOAD_SIZE is defined in RF24Network_config.h


void setup(void)
{
Serial.begin(115200);
Serial.println("RF24Network/examples/helloworld_rx_advanced/");
printf_begin(); //Used to enable printf on AVR devices

radio.begin();
network.begin(/*channel*/ 90, /*node address*/ this_node);
radio.printDetails();
}

// Variable for calculating how long between RX
uint32_t timeBetweenPackets = 0;

void loop(void){

network.update(); // Check the network regularly

while ( network.available() ) { // Is there anything ready for us?

RF24NetworkHeader header; // If so, grab it and print it out
uint16_t payloadSize = network.peek(header); // Use peek() to get the size of the payload
network.read(header,&dataBuffer,payloadSize); // Get the data
Serial.print("Received packet, size "); // Print info about received data
Serial.print(payloadSize);
Serial.print("(");
Serial.print(millis()-timeBetweenPackets);
Serial.println("ms since last)");
timeBetweenPackets = millis();
// Uncomment below to print the entire payload
/*for(uint32_t i=0;i<payloadSize;i++){
Serial.print(dataBuffer[i]);
Serial.print(":");
if(i%50 == 49){Serial.println();} //Add a line break every 50 characters
} Serial.println();*/

}
}
37 changes: 0 additions & 37 deletions examples/helloworld_tx/printf.h

This file was deleted.

90 changes: 90 additions & 0 deletions examples/helloworld_tx_advanced/helloworld_tx_advanced.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright (C) 2020 TMRh20([email protected])
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/

/**
* More advanced example of using RF24Network:
* Fragmentation and Reassembly:
* - nrf24l01+ radios can tx/rx 32 bytes of data per transmission
* - RF24Network will fragment and re-assemble payloads of any size
* Demonstrates use of differing sized payloads using peek() function
*
* TRANSMITTER NODE
* Every X milliseconds, send a payload to the receiver node.
*/

#include <RF24.h>
#include <RF24Network.h>
#include "printf.h"

RF24 radio(7,8); // nRF24L01(+) radio attached using Getting Started board

RF24Network network(radio); // Network uses that radio

const uint16_t this_node = 01; // Address of our node in Octal format
const uint16_t other_node = 00; // Address of the other node in Octal format

const unsigned long interval = 500; //ms // How often to send 'hello world to the other unit

unsigned long last_sent; // When did we last send?

/**** Create a large array for data to be sent ****
* MAX_PAYLOAD_SIZE is defined in RF24Network_config.h
* Payload sizes of ~1-2 KBytes or more are practical when radio conditions are good
*/
uint8_t dataBuffer[MAX_PAYLOAD_SIZE];

void setup(void)
{
Serial.begin(115200);
Serial.println("RF24Network/examples/helloworld_tx_advanced/");
printf_begin(); //Used to enable printf on AVR devices

radio.begin();
network.begin(/*channel*/ 90, /*node address*/ this_node);
radio.printDetails();

// Load our data buffer with numbered data
for(uint16_t i=0;i<MAX_PAYLOAD_SIZE;i++){
dataBuffer[i] = i % 256; //Ensure the max value is 255
}
}

uint16_t sizeofSend = 0; //Variable to indicate how much data to send
bool stopSending = 0; //Used to stop/start sending of data

void loop() {

//User input anything via Serial to stop/start data transmission
if(Serial.available()){
Serial.read();
stopSending = !stopSending;
}

network.update(); // Check the network regularly

unsigned long now = millis(); // If it's time to send a message, send it!
if ( now - last_sent >= interval && !stopSending ){
last_sent = now;
Serial.print("Sending size ");
Serial.print(sizeofSend );

// Fragmentation/reassembly is transparent. Just send payloads as usual.
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header,&dataBuffer,sizeofSend++);

// If the size of data to be sent is larger than max payload size, reset at 0
if(sizeofSend > MAX_PAYLOAD_SIZE){
sizeofSend = 0;
}

if (ok)
Serial.println(" ok.");
else
Serial.println(" failed.");
}
}

0 comments on commit c1cb484

Please sign in to comment.