Skip to content

Instantly share code, notes, and snippets.

@stenius
Created June 24, 2016 19:59
Show Gist options
  • Save stenius/ec60789ff184b00954932a1e05db062c to your computer and use it in GitHub Desktop.
Save stenius/ec60789ff184b00954932a1e05db062c to your computer and use it in GitHub Desktop.
#include <RFM69.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled = Adafruit_SSD1306();
#define BUTTON_A 9
#define BUTTON_B 6
#define BUTTON_C 5
#define NETWORKID 100 //the same on all nodes that talk to each other
#define NODE1 1
#define NODE2 2
#define NODEID NODE1 // Swap these two for other Feather
#define RECEIVER NODE2 // Swap these two for other Feather
#define FREQUENCY RF69_915MHZ
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HCW true // set to 'true' if you are using an RFM69HCW module
//*********************************************************************************************
#define SERIAL_BAUD 115200
#define RFM69_CS 8
#define RFM69_IRQ 7
#define RFM69_IRQN 4 // Pin 7 is IRQ 4!
#define RFM69_RST 4
#define LED 13 // onboard blinky
int16_t packetnum = 0; // packet counter, we increment per xmission
RFM69 radio = RFM69(RFM69_CS, RFM69_IRQ, IS_RFM69HCW, RFM69_IRQN);
void setup() {
//while (!Serial); // wait until serial console is open, remove if not tethered to computer
delay(1000);
//Serial.begin(SERIAL_BAUD);
// Initialize OLED display
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
oled.display();
delay(250);
oled.clearDisplay();
oled.display();
// OLED text display tests
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,0);
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
pinMode(BUTTON_C, INPUT_PULLUP);
Serial.println("Feather RFM69HCW RX/TX OLED demo");
// Hard Reset the RFM module
pinMode(RFM69_RST, OUTPUT);
digitalWrite(RFM69_RST, HIGH);
delay(100);
digitalWrite(RFM69_RST, LOW);
delay(100);
// Initialize radio
radio.initialize(FREQUENCY,NODEID,NETWORKID);
if (IS_RFM69HCW) {
radio.setHighPower(); // Only for RFM69HCW & HW!
}
radio.setPowerLevel(31); // power output ranges from 0 (5dBm) to 31 (20dBm)
radio.encrypt(ENCRYPTKEY);
pinMode(LED, OUTPUT);
Serial.print("Node #"); Serial.print(NODEID); Serial.print(" radio at ");
Serial.print(FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
Serial.println(" MHz");
oled.print("Node #"); oled.print(NODEID); oled.print(" radio at ");
oled.print(FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
oled.println(" MHz");
oled.display();
}
void loop() {
//check if something was received (could be an interrupt from the radio)
if (radio.receiveDone())
{
//print message received to serial
Serial.print('[');Serial.print(radio.SENDERID);Serial.print("] ");
Serial.print((char*)radio.DATA);
Serial.print(" [RX_RSSI:");Serial.print(radio.RSSI);Serial.print("]");
oled.clearDisplay();
oled.setCursor(0,0);
oled.print('[');oled.print(radio.SENDERID);oled.print("] ");
oled.println((char*)radio.DATA);
oled.print("[RX_RSSI:"); oled.print(radio.RSSI); oled.print("]");
oled.display();
//check if sender wanted an ACK
if (radio.ACKRequested())
{
radio.sendACK();
Serial.println(" - ACK sent");
}
Blink(LED, 40, 3); //blink LED 3 times, 40ms between blinks
}
if (!digitalRead(BUTTON_A) || !digitalRead(BUTTON_B) || !digitalRead(BUTTON_C))
{
Serial.println("Button pressed!");
char radiopacket[20] = "Button #";
if (!digitalRead(BUTTON_A)) radiopacket[8] = 'A';
if (!digitalRead(BUTTON_B)) radiopacket[8] = 'B';
if (!digitalRead(BUTTON_C)) radiopacket[8] = 'C';
radiopacket[9] = 0;
Serial.print("Sending "); Serial.println(radiopacket);
int transmissionAttempts = 0;
bool transmitStatus = 0;
while (transmissionAttempts < 25 and !transmitStatus)
{
transmitStatus = radio.sendWithRetry(RECEIVER, radiopacket, strlen(radiopacket));
delay(250);
if (transmitStatus){
//Blink(LED, 50, 3); //blink LED 3 times, 50ms between blinks
oled.clearDisplay();
oled.setCursor(0,0);
oled.println("recieved");
oled.println(transmissionAttempts);
oled.print("[RX_RSSI:"); oled.print(radio.RSSI); oled.print("]");
oled.display();
}
else
{
transmissionAttempts += 1;
oled.clearDisplay();
oled.setCursor(0,0);
oled.println("retransmitting");
oled.println(transmissionAttempts);
oled.display();
}
}
}
radio.receiveDone(); //put radio in RX mode
Serial.flush(); //make sure all serial data is clocked out before sleeping the MCU
}
void Blink(byte PIN, byte DELAY_MS, byte loops)
{
for (byte i=0; i<loops; i++)
{
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
delay(DELAY_MS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment