first commit
This commit is contained in:
62
examples/rf24/rf24_client/rf24_client.ino
Normal file
62
examples/rf24/rf24_client/rf24_client.ino
Normal file
@@ -0,0 +1,62 @@
|
||||
// rf24_client.pde
|
||||
// -*- mode: C++ -*-
|
||||
// Example sketch showing how to create a simple messageing client
|
||||
// with the RH_RF24 class. RH_RF24 class does not provide for addressing or
|
||||
// reliability, so you should only use RH_RF24 if you do not need the higher
|
||||
// level messaging abilities.
|
||||
// It is designed to work with the other example rf24_server.
|
||||
// Tested on Anarduino Mini http://www.anarduino.com/mini/ with RFM24W and RFM26W
|
||||
|
||||
#include <SPI.h>
|
||||
#include <RH_RF24.h>
|
||||
|
||||
// Singleton instance of the radio driver
|
||||
RH_RF24 rf24;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
if (!rf24.init())
|
||||
Serial.println("init failed");
|
||||
// The default radio config is for 30MHz Xtal, 434MHz base freq 2GFSK 5kbps 10kHz deviation
|
||||
// power setting 0x10
|
||||
// If you want a different frequency mand or modulation scheme, you must generate a new
|
||||
// radio config file as per the RH_RF24 module documentation and recompile
|
||||
// You can change a few other things programatically:
|
||||
//rf24.setFrequency(435.0); // Only within the same frequency band
|
||||
//rf24.setTxPower(0x7f);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.println("Sending to rf24_server");
|
||||
// Send a message to rf24_server
|
||||
uint8_t data[] = "Hello World!";
|
||||
rf24.send(data, sizeof(data));
|
||||
|
||||
rf24.waitPacketSent();
|
||||
// Now wait for a reply
|
||||
uint8_t buf[RH_RF24_MAX_MESSAGE_LEN];
|
||||
uint8_t len = sizeof(buf);
|
||||
|
||||
if (rf24.waitAvailableTimeout(500))
|
||||
{
|
||||
// Should be a reply message for us now
|
||||
if (rf24.recv(buf, &len))
|
||||
{
|
||||
Serial.print("got reply: ");
|
||||
Serial.println((char*)buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("recv failed");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("No reply, is rf24_server running?");
|
||||
}
|
||||
delay(400);
|
||||
}
|
||||
|
80
examples/rf24/rf24_lowpower_client/rf24_lowpower_client.ino
Normal file
80
examples/rf24/rf24_lowpower_client/rf24_lowpower_client.ino
Normal file
@@ -0,0 +1,80 @@
|
||||
// rf24_lowpower_client.pde
|
||||
// -*- mode: C++ -*-
|
||||
// Example sketch showing how to create a simple message transmitter
|
||||
// which sleeps between transmissions (every 8 secs) to reduce power consumnption
|
||||
// It uses the watchdog timer and the CPU sleep mode and the radio sleep mode
|
||||
// to reduce quiescent power to 1.7mA
|
||||
// Tested on Anarduino Mini http://www.anarduino.com/mini/ with RFM24W and RFM26W
|
||||
|
||||
#include <SPI.h>
|
||||
#include <RH_RF24.h>
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/power.h>
|
||||
|
||||
// Singleton instance of the radio driver
|
||||
RH_RF24 rf24;
|
||||
|
||||
// Watchdog timer interrupt handler
|
||||
ISR(WDT_vect)
|
||||
{
|
||||
// Dont need to do anything, just override the default vector which causes a reset
|
||||
}
|
||||
|
||||
// Go into sleep mode until WDT interrupt
|
||||
void sleep()
|
||||
{
|
||||
// Select the sleep mode we want. This is the lowest power
|
||||
// that can wake with WDT interrupt
|
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
||||
sleep_mode(); // Sleep here and wake on WDT interrupt every 8 secs
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
if (!rf24.init())
|
||||
Serial.println("init failed");
|
||||
// The default radio config is for 30MHz Xtal, 434MHz base freq 2GFSK 5kbps 10kHz deviation
|
||||
// power setting 0x10
|
||||
// If you want a different frequency mand or modulation scheme, you must generate a new
|
||||
// radio config file as per the RH_RF24 module documentation and recompile
|
||||
// You can change a few other things programatically:
|
||||
//rf24.setFrequency(435.0); // Only within the same frequency band
|
||||
//rf24.setTxPower(0x7f);
|
||||
|
||||
// Set the watchdog timer to interrupt every 8 secs
|
||||
noInterrupts();
|
||||
// Set the watchdog reset bit in the MCU status register to 0.
|
||||
MCUSR &= ~(1<<WDRF);
|
||||
// Set WDCE and WDE bits in the watchdog control register.
|
||||
WDTCSR |= (1<<WDCE) | (1<<WDE);
|
||||
// Set watchdog clock prescaler bits to a value of 8 seconds.
|
||||
WDTCSR = (1<<WDP0) | (1<<WDP3);
|
||||
// Enable watchdog as interrupt only (no reset).
|
||||
WDTCSR |= (1<<WDIE);
|
||||
// Enable interrupts again.
|
||||
interrupts();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.println("Sending to rf24_server");
|
||||
|
||||
// Send a message to rf24_server
|
||||
uint8_t data[] = "Hello World!";
|
||||
rf24.send(data, sizeof(data));
|
||||
// Make sure its gone before we sleep
|
||||
rf24.waitPacketSent();
|
||||
|
||||
// Anarduino Mini + RFM26, no UART connection (power only)
|
||||
// 9mA quiescent without any sleep (more during Tx)
|
||||
// 1.7mA quiescent with radio and CPU sleeping
|
||||
|
||||
// radio is 1.58mA while sleeping (in STANDBY state but the antenna switch seems to take some power too)
|
||||
// 2mA when in Ready state
|
||||
rf24.sleep();
|
||||
|
||||
// Sleep inside here until next WDT in 8 secs
|
||||
sleep();
|
||||
}
|
@@ -0,0 +1,65 @@
|
||||
// rf24_reliable_datagram_client.pde
|
||||
// -*- mode: C++ -*-
|
||||
// Example sketch showing how to create a simple addressed, reliable messaging client
|
||||
// with the RHReliableDatagram class, using the RH_RF24 driver to control a RF24 radio.
|
||||
// It is designed to work with the other example rf24_reliable_datagram_server
|
||||
// Tested on Anarduino Mini http://www.anarduino.com/mini/ with RFM24W and RFM26W
|
||||
|
||||
#include <RHReliableDatagram.h>
|
||||
#include <RH_RF24.h>
|
||||
#include <SPI.h>
|
||||
|
||||
#define CLIENT_ADDRESS 1
|
||||
#define SERVER_ADDRESS 2
|
||||
|
||||
// Singleton instance of the radio driver
|
||||
RH_RF24 driver;
|
||||
|
||||
// Class to manage message delivery and receipt, using the driver declared above
|
||||
RHReliableDatagram manager(driver, CLIENT_ADDRESS);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
if (!manager.init())
|
||||
Serial.println("init failed");
|
||||
// The default radio config is for 30MHz Xtal, 434MHz base freq 2GFSK 5kbps 10kHz deviation
|
||||
// power setting 0x10
|
||||
// If you want a different frequency mand or modulation scheme, you must generate a new
|
||||
// radio config file as per the RH_RF24 module documentation and recompile
|
||||
// You can change a few other things programatically:
|
||||
//driver.setFrequency(435.0); // Only within the same frequency band
|
||||
//driver.setTxPower(0x7f);
|
||||
}
|
||||
|
||||
uint8_t data[] = "Hello World!";
|
||||
// Dont put this on the stack:
|
||||
uint8_t buf[RH_RF24_MAX_MESSAGE_LEN];
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.println("Sending to rf24_reliable_datagram_server");
|
||||
|
||||
// Send a message to manager_server
|
||||
if (manager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
|
||||
{
|
||||
// Now wait for a reply from the server
|
||||
uint8_t len = sizeof(buf);
|
||||
uint8_t from;
|
||||
if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
|
||||
{
|
||||
Serial.print("got reply from : 0x");
|
||||
Serial.print(from, HEX);
|
||||
Serial.print(": ");
|
||||
Serial.println((char*)buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("No reply, is rf24_reliable_datagram_server running?");
|
||||
}
|
||||
}
|
||||
else
|
||||
Serial.println("sendtoWait failed");
|
||||
delay(500);
|
||||
}
|
||||
|
@@ -0,0 +1,59 @@
|
||||
// rf24_reliable_datagram_server.pde
|
||||
// -*- mode: C++ -*-
|
||||
// Example sketch showing how to create a simple addressed, reliable messaging server
|
||||
// with the RHReliableDatagram class, using the RH_RF24 driver to control a RF24 radio.
|
||||
// It is designed to work with the other example rf24_reliable_datagram_client
|
||||
// Tested on Anarduino Mini http://www.anarduino.com/mini/ with RFM24W and RFM26W
|
||||
|
||||
#include <RHReliableDatagram.h>
|
||||
#include <RH_RF24.h>
|
||||
#include <SPI.h>
|
||||
|
||||
#define CLIENT_ADDRESS 1
|
||||
#define SERVER_ADDRESS 2
|
||||
|
||||
// Singleton instance of the radio driver
|
||||
RH_RF24 driver;
|
||||
|
||||
// Class to manage message delivery and receipt, using the driver declared above
|
||||
RHReliableDatagram manager(driver, SERVER_ADDRESS);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
if (!manager.init())
|
||||
Serial.println("init failed");
|
||||
// The default radio config is for 30MHz Xtal, 434MHz base freq 2GFSK 5kbps 10kHz deviation
|
||||
// power setting 0x10
|
||||
// If you want a different frequency mand or modulation scheme, you must generate a new
|
||||
// radio config file as per the RH_RF24 module documentation and recompile
|
||||
// You can change a few other things programatically:
|
||||
//driver.setFrequency(435.0); // Only within the same frequency band
|
||||
//driver.setTxPower(0x7f);
|
||||
}
|
||||
|
||||
uint8_t data[] = "And hello back to you";
|
||||
// Dont put this on the stack:
|
||||
uint8_t buf[RH_RF24_MAX_MESSAGE_LEN];
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (manager.available())
|
||||
{
|
||||
// Wait for a message addressed to us from the client
|
||||
uint8_t len = sizeof(buf);
|
||||
uint8_t from;
|
||||
if (manager.recvfromAck(buf, &len, &from))
|
||||
{
|
||||
Serial.print("got request from : 0x");
|
||||
Serial.print(from, HEX);
|
||||
Serial.print(": ");
|
||||
Serial.println((char*)buf);
|
||||
|
||||
// Send a reply back to the originator client
|
||||
if (!manager.sendtoWait(data, sizeof(data), from))
|
||||
Serial.println("sendtoWait failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
57
examples/rf24/rf24_server/rf24_server.ino
Normal file
57
examples/rf24/rf24_server/rf24_server.ino
Normal file
@@ -0,0 +1,57 @@
|
||||
// rf24_server.pde
|
||||
// -*- mode: C++ -*-
|
||||
// Example sketch showing how to create a simple messageing server
|
||||
// with the RH_RF24 class. RH_RF24 class does not provide for addressing or
|
||||
// reliability, so you should only use RH_RF24 if you do not need the higher
|
||||
// level messaging abilities.
|
||||
// It is designed to work with the other example rf24_client
|
||||
// Tested on Anarduino Mini http://www.anarduino.com/mini/ with RFM24W and RFM26W
|
||||
|
||||
#include <SPI.h>
|
||||
#include <RH_RF24.h>
|
||||
|
||||
// Singleton instance of the radio driver
|
||||
RH_RF24 rf24;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
if (!rf24.init())
|
||||
Serial.println("init failed");
|
||||
// The default radio config is for 30MHz Xtal, 434MHz base freq 2GFSK 5kbps 10kHz deviation
|
||||
// power setting 0x10
|
||||
// If you want a different frequency mand or modulation scheme, you must generate a new
|
||||
// radio config file as per the RH_RF24 module documentation and recompile
|
||||
// You can change a few other things programatically:
|
||||
//rf24.setFrequency(435.0); // Only within the same frequency band
|
||||
//rf24.setTxPower(0x7f);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (rf24.available())
|
||||
{
|
||||
// Should be a message for us now
|
||||
uint8_t buf[RH_RF24_MAX_MESSAGE_LEN];
|
||||
uint8_t len = sizeof(buf);
|
||||
if (rf24.recv(buf, &len))
|
||||
{
|
||||
// RF24::printBuffer("request: ", buf, len);
|
||||
Serial.print("got request: ");
|
||||
Serial.println((char*)buf);
|
||||
// Serial.print("RSSI: ");
|
||||
// Serial.println((uint8_t)rf24.lastRssi(), DEC);
|
||||
|
||||
// Send a reply
|
||||
uint8_t data[] = "And hello back to you";
|
||||
rf24.send(data, sizeof(data));
|
||||
rf24.waitPacketSent();
|
||||
Serial.println("Sent a reply");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("recv failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user