Created
March 22, 2017 17:14
-
-
Save nonsintetic/3ffa1a47f98349fb76c4cd9cff3ab8f1 to your computer and use it in GitHub Desktop.
RFM69HW with RFM69 library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RFM69HCW Example Sketch | |
// Send serial input characters from one RFM69 node to another | |
// Based on RFM69 library sample code by Felix Rusu | |
// http://LowPowerLab.com/contact | |
// Modified for RFM69HCW by Mike Grusin, 4/16 | |
// This sketch will show you the basics of using an | |
// RFM69HCW radio module. SparkFun's part numbers are: | |
// 915MHz: https://www.sparkfun.com/products/12775 | |
// 434MHz: https://www.sparkfun.com/products/12823 | |
// See the hook-up guide for wiring instructions: | |
// https://learn.sparkfun.com/tutorials/rfm69hcw-hookup-guide | |
// Uses the RFM69 library by Felix Rusu, LowPowerLab.com | |
// Original library: https://www.github.com/lowpowerlab/rfm69 | |
// SparkFun repository: https://github.com/sparkfun/RFM69HCW_Breakout | |
// Include the RFM69 and SPI libraries: | |
#include <RFM69.h> | |
#include <SPI.h> | |
//node info | |
#define NETWORKID 22 // Must be the same for all nodes (0 to 255) | |
#define MYNODEID 1 // CHANGE THIS FOR EACH NODE - This node's ID (0 to 255) | |
#define TONODEID 255 // Destination node ID (0 to 254, 255 = broadcast) | |
//pins | |
#define RFM69_CS 5 | |
#define RFM69_IRQ 6 | |
#define RFM69_RST 9 | |
#define IS_RFM69HCW true //TRUE if it's a RFM69HCW or HW (the high power ones) | |
#define FREQUENCY RF69_433MHZ //CHANGE THIS TO YOUR FREQUENCY | |
//#define FREQUENCY RF69_868MHZ | |
//#define FREQUENCY RF69_915MHZ | |
#define ENCRYPT false // Set to "true" to use encryption | |
#define ENCRYPTKEY "TOPSECRETPASSWRD" // Use the same 16-byte key on all nodes | |
#define USEACK false // Request ACKs or not, for me it never responds with an ACK.. | |
//Library object | |
RFM69 radio = RFM69(RFM69_CS, RFM69_IRQ, IS_RFM69HCW, RFM69_IRQ); | |
void setup() { | |
// Open a serial port so we can send keystrokes to the module: | |
Serial.begin(57600); | |
delay(2000); | |
Serial.print("Node "); | |
Serial.print(MYNODEID,DEC); | |
Serial.println(" ready"); | |
// Initialize the RFM69HCW: | |
radio.initialize(FREQUENCY, MYNODEID, NETWORKID); | |
radio.setHighPower(true); // Always use this for RFM69HCW | |
radio.setPowerLevel(1); //0-32 - power level, for close range just use 1 to make sure there's no power supply problems | |
} | |
void loop() | |
{ | |
// Set up a "buffer" for characters that we'll send: | |
static char sendbuffer[62]; | |
static int sendlength = 0; | |
// SENDING | |
// In this section, we'll gather serial characters and | |
// send them to the other node if we (1) get a carriage return, | |
// or (2) the buffer is full (61 characters). | |
// If there is any serial input, add it to the buffer: | |
if (Serial.available() > 0) { | |
char input = Serial.read(); | |
if (input != '\r') { // not a carriage return | |
sendbuffer[sendlength] = input; | |
sendlength++; | |
} | |
// If the input is a carriage return, or the buffer is full: | |
if ((input == '\r') || (sendlength == 61)) { // CR or buffer full | |
Serial.print("sending to node "); | |
Serial.print(TONODEID, DEC); | |
Serial.print(": ["); | |
for (byte i = 0; i < sendlength; i++) Serial.print(sendbuffer[i]); | |
Serial.println("]"); | |
// There are two ways to send packets. If you want | |
// acknowledgements, use sendWithRetry(): | |
if (USEACK) { | |
if (radio.sendWithRetry(TONODEID, sendbuffer, sendlength)) | |
Serial.println("ACK received!"); | |
else | |
Serial.println("no ACK received :("); | |
} | |
// If you don't need acknowledgements, just use send(): | |
else // don't use ACK | |
{ | |
radio.send(TONODEID, sendbuffer, sendlength); | |
} | |
sendlength = 0; // reset the packet | |
} | |
} | |
// RECEIVING | |
// In this section, we'll check with the RFM69HCW to see | |
// if it has received any packets: | |
if (radio.receiveDone()) { // Got one! | |
// Print out the information: | |
Serial.print("received from node "); | |
Serial.print(radio.SENDERID, DEC); | |
Serial.print(": ["); | |
// The actual message is contained in the DATA array, | |
// and is DATALEN bytes in size: | |
for (byte i = 0; i < radio.DATALEN; i++) Serial.print((char)radio.DATA[i]); | |
// RSSI is the "Receive Signal Strength Indicator", | |
// smaller numbers mean higher power. Usually you want it under -40 for close range connections | |
Serial.print("], RSSI "); | |
Serial.println(radio.RSSI); | |
// Send an ACK if requested. | |
// (You don't need this code if you're not using ACKs.) | |
if (radio.ACKRequested()) { | |
radio.sendACK(); | |
Serial.println("ACK sent"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment