/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.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.
*/
/**
* This example will receive dynamic payload from the Raspberry Pi and send
* back the payload to the Raspberry Pi
* I have added the LCD Module to display the payload and payload length
* on a 20x4 LCD Module
*
* Stanley Seow
* stanleyseow@gmail.com
*/
#include <LiquidCrystal.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
LiquidCrystal lcd(10, 9, 3, 4, 5, 6);
// Make way for the SPI pins
// 10 -> LCD 4
// 9 -> LCD 6
// 3 -> LCD 11
// 4 -> LCD 12
// 5 -> LCD 13
// 6 -> LCD 14
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 3 & 4
RF24 radio(8,7);
//
// Topology
//
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
//
// Payload
//
const int min_payload_size = 4;
const int max_payload_size = 32;
const int payload_size_increments_by = 2;
int next_payload_size = min_payload_size;
char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char
void setup(void)
{
// Setup LCD
lcd.begin(20,4); // col,row
lcd.setCursor(0,0);
lcd.print("nRF24L01 to RPi");
pinMode(2, OUTPUT);
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair_dyn/\n\r");
//
// Setup and configure rf radio
//
radio.begin();
// enable dynamic payloads
radio.enableDynamicPayloads();
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
radio.setDataRate(RF24_1MBPS);
//
// Open pipes to other nodes for communication
//
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
// Open 'our' pipe for writing
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
}
void loop(void)
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
uint8_t len;
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
len = radio.getDynamicPayloadSize();
done = radio.read( receive_payload, len );
// Put a zero at the end for easy printing
receive_payload[len] = 0;
// Spew it
printf("Got payload size=%i value=%s\n\r",len,receive_payload);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(len);
lcd.setCursor(0,1);
lcd.print(receive_payload);
digitalWrite(2,HIGH); // Turn on Beep
}
// First, stop listening so we can talk
radio.stopListening();
delay(50); // Put a small delay before sending response back
// Send the final one back.
radio.write( receive_payload, len );
printf("Sent response.\n\r");
// Now, resume listening so we catch the next packets.
radio.startListening();
delay(50); // Have a small delay
digitalWrite(2,LOW); // Turn off beep
}
}