diff --git a/RF24Network_config.h b/RF24Network_config.h index b56b882c..b49d3cc0 100644 --- a/RF24Network_config.h +++ b/RF24Network_config.h @@ -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 diff --git a/examples/Network_Ping/printf.h b/examples/Network_Ping/printf.h deleted file mode 100644 index b2efd56b..00000000 --- a/examples/Network_Ping/printf.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - Copyright (C) 2011 J. Coliz - - 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. - */ - -/** - * @file printf.h - * - * Setup necessary to direct stdout to the Arduino Serial library, which - * enables 'printf' - */ - -#ifndef __PRINTF_H__ -#define __PRINTF_H__ - -#ifdef ARDUINO - -int serial_putc( char c, FILE * ) -{ - Serial.write( c ); - - return c; -} - -void printf_begin(void) -{ - fdevopen( &serial_putc, 0 ); -} - -#else -#error This example is only for use on Arduino. -#endif // ARDUINO - -#endif // __PRINTF_H__ diff --git a/examples/Network_Ping_Sleep/printf.h b/examples/Network_Ping_Sleep/printf.h deleted file mode 100644 index b2efd56b..00000000 --- a/examples/Network_Ping_Sleep/printf.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - Copyright (C) 2011 J. Coliz - - 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. - */ - -/** - * @file printf.h - * - * Setup necessary to direct stdout to the Arduino Serial library, which - * enables 'printf' - */ - -#ifndef __PRINTF_H__ -#define __PRINTF_H__ - -#ifdef ARDUINO - -int serial_putc( char c, FILE * ) -{ - Serial.write( c ); - - return c; -} - -void printf_begin(void) -{ - fdevopen( &serial_putc, 0 ); -} - -#else -#error This example is only for use on Arduino. -#endif // ARDUINO - -#endif // __PRINTF_H__ diff --git a/examples/helloworld_rx/printf.h b/examples/helloworld_rx/printf.h deleted file mode 100644 index b2efd56b..00000000 --- a/examples/helloworld_rx/printf.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - Copyright (C) 2011 J. Coliz - - 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. - */ - -/** - * @file printf.h - * - * Setup necessary to direct stdout to the Arduino Serial library, which - * enables 'printf' - */ - -#ifndef __PRINTF_H__ -#define __PRINTF_H__ - -#ifdef ARDUINO - -int serial_putc( char c, FILE * ) -{ - Serial.write( c ); - - return c; -} - -void printf_begin(void) -{ - fdevopen( &serial_putc, 0 ); -} - -#else -#error This example is only for use on Arduino. -#endif // ARDUINO - -#endif // __PRINTF_H__ diff --git a/examples/helloworld_rx_advanced/helloworld_rx_advanced.ino b/examples/helloworld_rx_advanced/helloworld_rx_advanced.ino new file mode 100644 index 00000000..89bd8d2e --- /dev/null +++ b/examples/helloworld_rx_advanced/helloworld_rx_advanced.ino @@ -0,0 +1,74 @@ +/* + Copyright (C) 2020 TMRh20(tmrh20@gmail.com) + + 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 +#include +#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 - - 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. - */ - -/** - * @file printf.h - * - * Setup necessary to direct stdout to the Arduino Serial library, which - * enables 'printf' - */ - -#ifndef __PRINTF_H__ -#define __PRINTF_H__ - -#ifdef ARDUINO - -int serial_putc( char c, FILE * ) -{ - Serial.write( c ); - - return c; -} - -void printf_begin(void) -{ - fdevopen( &serial_putc, 0 ); -} - -#else -#error This example is only for use on Arduino. -#endif // ARDUINO - -#endif // __PRINTF_H__ diff --git a/examples/helloworld_tx_advanced/helloworld_tx_advanced.ino b/examples/helloworld_tx_advanced/helloworld_tx_advanced.ino new file mode 100644 index 00000000..72996474 --- /dev/null +++ b/examples/helloworld_tx_advanced/helloworld_tx_advanced.ino @@ -0,0 +1,90 @@ +/* + Copyright (C) 2020 TMRh20(tmrh20@gmail.com) + + 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 +#include +#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= 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."); + } +}