Skip to content

Instantly share code, notes, and snippets.

@shaktidhar
Forked from bryanthompson/printf.h
Created April 14, 2016 16:33
Show Gist options
  • Save shaktidhar/4402515b54f22f204785efe86d23c1e3 to your computer and use it in GitHub Desktop.
Save shaktidhar/4402515b54f22f204785efe86d23c1e3 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment