#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RF24.h"
#include <DigitalIO.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
// RF pin 1 (BROWN) to Arduino GND
// RF pin 2 (RED) to Arduino 3.3v
#define SOFTSPI
#define RF_CE 8 // RF pin 3 (CE) (BLUE) to Arduino D8
#define RF_CSN 9 // RF pin 4 (CSN) (PURPLE) to Arduino D9
#define SOFT_SPI_SCK_PIN 10 // RF pin 5 (SCK) (WHITE) to Arduino D10
#define SOFT_SPI_MOSI_PIN 11 // RF pin 6 (MOSI) (GREY) to Arduino D11
#define SOFT_SPI_MISO_PIN 12 // RF pin 7 (MISO) (BLACK) to Arduino D12
// RF pin 8 is unused
RF24 radio (RF_CE,RF_CSN);
// Let these addresses be used for the pair
uint8_t address[][6] = { "1Node", "2Node", "3Node" };
// It is very helpful to think of an address as a path instead of as
// an identifying device destination
// to use different addresses on a pair of radios, we need a variable to
// uniquely identify which address this radio will use to transmit
bool radioNumber = 2; // 0 uses address[0] to transmit, 1 uses address[1] to transmit
// Used to control whether this node is sending or receiving
bool role = false; // true = TX role, false = RX role
// For this example, we'll be using a payload containing
// a single float number that will be incremented
// on every successful transmission
float payload = 0.0;
void setup() {
Serial.begin(9600);
while (!Serial) {
Serial.println(F("\nSystem not responding!!"));
while (1) {} // hold in infinite loop
}
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// initialize the transceiver on the SPI bus
if (!radio.begin()) {
Serial.println(F("\nRadio hardware is not responding!!"));
//while (1) {} // hold in infinite loop
}
// Set the PA Level low to try preventing power supply related problems
// because these examples are likely run with nodes in close proximity to
// each other.
radio.setPALevel(RF24_PA_MAX); // RF24_PA_MAX is default.
radio.setDataRate(RF24_250KBPS) ;
// save on transmission time by setting the radio to only transmit the
// number of bytes we need to transmit a float
radio.setPayloadSize(sizeof(payload)); // float datatype occupies 4 bytes
// set the TX address of the RX node into the TX pipe
radio.openWritingPipe(address[radioNumber]); // always uses pipe 0
// set the RX address of the TX node into a RX pipe
radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1
// additional setup specific to the node's role
if (role) {
radio.stopListening(); // put radio in TX mode
} else {
radio.startListening(); // put radio in RX mode
}
// For debugging info
// printf_begin(); // needed only once for printing details
// radio.printDetails(); // (smaller) function that prints raw register values
// radio.printPrettyDetails(); // (larger) function that prints human readable data
} // setup
void loop() {
if (role) {
// This device is a TX node
unsigned long start_timer = micros(); // start the timer
bool report = radio.write(&payload, sizeof(float)); // transmit & save the report
unsigned long end_timer = micros(); // end the timer
payload += 0.01; // increment float payload
delay(1000);
} else {
// This device is a RX node
uint8_t pipe;
if (radio.available(&pipe)) { // is there a payload? get the pipe number that recieved it
uint8_t bytes = radio.getPayloadSize(); // get the size of the payload
radio.read(&payload, bytes); // fetch payload from FIFO
Serial.print(F("Received "));
Serial.print(bytes); // print the size of the payload
Serial.print(F(" bytes on pipe "));
Serial.print(pipe); // print the pipe number
Serial.print(F(": "));
Serial.println(payload); // print the payload's value
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Payload: ");
display.print(payload);
display.display();
}
}
}