Skip to content

Instantly share code, notes, and snippets.

@sfambach
Created August 7, 2019 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfambach/05f6f2c0915820ad58739f88a79a3816 to your computer and use it in GitHub Desktop.
Save sfambach/05f6f2c0915820ad58739f88a79a3816 to your computer and use it in GitHub Desktop.
Arduino Nano Clone with nRF24 on board
// Nano Remote Receiver
// This programm receives a char array with maximum size 20
// www.fambach.net
//
// Libs:
// https://github.com/tmrh20/RF24/
//
// GPL2
/***********************************************************/
// radio
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#define PIN_CE 10
#define PIN_CSN 9
RF24 radio(PIN_CE, PIN_CSN);
byte addresses[][6] = {"007", "001"};
#define SIZE 20
char buf[SIZE];
/***********************************************************/
void setup() {
Serial.begin(9600);
delay(4000);
Serial.println("SPI settings");
SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_2XCLOCK_MASK);
Serial.println("init rf");
radio.begin(); // initialize RF24
radio.setRetries(0, 15); // set retries times
radio.setPALevel(RF24_PA_LOW); // set power
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
radio.startListening(); // start monitoring
}
void loop()
{
// receive data ;)
receiveData();
}
void receiveData() {
if ( radio.available()) { // if receive the data
// Read the data payload until we've received everything
bool done = false;
while (radio.available())
{
// Fetch the data payload
radio.read( buf, sizeof(buf));
// for(int i = 0; i < 20; i ++){
// Serial.print((char)buf[i]);
// }
Serial.println(buf);
}
}
else
{
// Serial.println("No radio available"); //only for test purpose
}
}
// Nano Remote Transmiter
// This programm sends Hello World! x (x is an index from 0 to 9)
// www.fambach.net
//
// Libs:
// https://github.com/tmrh20/RF24/
//
// GPL2
/***********************************************************/
// RF
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#define PIN_CE 10
#define PIN_CSN 9
RF24 radio(PIN_CE, PIN_CSN); // define the object to control NRF24L01
byte addresses[][6] = {"007", "001"};
int mode = 1;
#define SIZE 20
char buf[SIZE];
void setup() {
Serial.begin(9600);
/***********************************************************************/
// init radio
radio.begin(); // initialize RF24
radio.setRetries(0, 15); // set retries times
radio.setPALevel(RF24_PA_LOW); // set power
radio.openWritingPipe(addresses[1]); // open delivery channel
radio.openReadingPipe(1, addresses[0]);
radio.stopListening();
}
int i = 0;
void loop() {
String s;
s.reserve(20);
s = "Hello World! ";
s += i;
s.toCharArray(buf, SIZE);
Serial.println(s);
radio.write(buf,sizeof(buf));
i++;
if (i > 9) i = 0;
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment