Skip to content

Instantly share code, notes, and snippets.

@bryanthompson
Last active April 14, 2016 16:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bryanthompson/ef4ecf24ad36410f077b to your computer and use it in GitHub Desktop.
Save bryanthompson/ef4ecf24ad36410f077b to your computer and use it in GitHub Desktop.
This is an adaptation of several variations of 'getting started' sketches with the nrf24L01+ radios and arduino. These sketches assume that you are using an Arduino Pro Micro on both ends and that you have an LED attached on pin 2 of the transmit side. To run these, you'll need to install the rf24 and spi libraries from jscrane's repo: https://g…
/*
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.
*/
/**
* @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__
#include <SPI.h>
#include <RF24.h>
#include "printf.h"
#define RF_CS 9
#define RF_CSN 10
RF24 radio(RF_CS, RF_CSN);
const uint64_t pipes[2] = { 0xe7e7e7e7e7LL, 0xc2c2c2c2c2LL };
struct sensor_struct{
int sensor_id;
float temp;
float soil_temp;
float humid;
float pres;
};
void setup() {
Serial.begin(9600);
printf_begin();
radio.begin();
radio.openWritingPipe(pipes[1]); // note that our pipes are the same above, but that
radio.openReadingPipe(1, pipes[0]); // they are flipped between rx and tx sides.
radio.startListening();
radio.printDetails();
}
void loop() {
if (radio.available()) {
Serial.println("--------------------------------------------------------------------------------");
uint8_t rx_data[32]; // we'll receive a 32 byte packet
bool done = false;
while (!done) {
done = radio.read( &rx_data, sizeof(rx_data) );
printf("Got payload @ %lu...\r\n", millis());
}
// echo it back real fast
radio.stopListening();
radio.write( &rx_data, sizeof(rx_data) );
Serial.println("Sent response.");
radio.startListening();
// do stuff with the data we got.
Serial.print("First Value: ");
Serial.println(rx_data[0]);
}
}
// https://gist.github.com/bryanthompson/ef4ecf24ad36410f077b
#include <SPI.h>
#include <RF24.h>
#include "printf.h"
#define LED 2
#define RF_CS 9
#define RF_CSN 10
RF24 radio(RF_CS, RF_CSN);
const uint64_t pipes[2] = { 0xe7e7e7e7e7LL, 0xc2c2c2c2c2LL };
void setup() {
Serial.begin(9600);
printf_begin();
pinMode(LED, OUTPUT);
radio.begin();
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
radio.startListening();
radio.printDetails();
}
void loop() {
unsigned long time = millis();
uint8_t data[32]; // we'll transmit a 32 byte packet
data[0] = 99; // our first byte in the pcaket will just be the number 99.
// transmit the data
radio.stopListening();
radio.write( &data, sizeof(data) );
radio.startListening();
// listen for acknowledgement from the receiver
unsigned long started_waiting_at = millis();
bool timeout = false;
while (!radio.available() && ! timeout)
if (millis() - started_waiting_at > 250 )
timeout = true;
if (timeout){
Serial.println("Failed, response timed out.");
} else {
// the receiver is just going to spit the data back
radio.read( &data, sizeof(data) );
digitalWrite(LED, HIGH);
delay(100); // light up the LED for 100ms if it worked.
digitalWrite(LED, LOW);
Serial.print("Got response, round trip delay: ");
Serial.print(millis() - started_waiting_at);
}
delay(1000); // wait a second and do it again.
}
// https://gist.github.com/bryanthompson/ef4ecf24ad36410f077b
@dilantha111
Copy link

I think this is pretty cool. thank you very much for this work .. I spent lot of time to understand from the examples provided with the library files. and it was really a hard work. thanks again

@chrisdgenius
Copy link

No feedback from the receiver to the transmitter. please review and update me.
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment