100 lines
2.8 KiB
C++
100 lines
2.8 KiB
C++
#include <RFM69.h>
|
|
#include <SPI.h>
|
|
|
|
#define NODEID 1
|
|
#define NETWORKID 100
|
|
#define FREQUENCY RF69_915MHZ //Match this with the version of your Moteino! (others: RF12_433MHZ, RF12_915MHZ)
|
|
#define KEY "thisIsEncryptKey"
|
|
#define LED 9
|
|
#define SERIAL_BAUD 115200
|
|
#define ACK_TIME 10 // # of ms to wait for an ack
|
|
|
|
RFM69 radio;
|
|
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network
|
|
|
|
void setup() {
|
|
Serial.begin(SERIAL_BAUD);
|
|
radio.initialize(RF69_915MHZ,NODEID,NETWORKID);
|
|
radio.encrypt(KEY);
|
|
radio.promiscuous(promiscuousMode);
|
|
char buff[50];
|
|
sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
|
|
Serial.println(buff);
|
|
}
|
|
|
|
byte ackCount=0;
|
|
void loop() {
|
|
//process any serial input
|
|
if (Serial.available() > 0)
|
|
{
|
|
char input = Serial.read();
|
|
if (input == 'd') //d=dump all register values
|
|
radio.readAllRegs();
|
|
if (input == 'E') //E=enable encryption
|
|
radio.encrypt(KEY);
|
|
if (input == 'e') //e=disable encryption
|
|
radio.encrypt(null);
|
|
if (input == 'p')
|
|
{
|
|
promiscuousMode = !promiscuousMode;
|
|
radio.promiscuous(promiscuousMode);
|
|
Serial.print("Promiscuous mode ");Serial.println(promiscuousMode ? "on" : "off");
|
|
}
|
|
}
|
|
|
|
if (radio.receiveDone())
|
|
{
|
|
Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
|
|
if (promiscuousMode)
|
|
{
|
|
Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
|
|
}
|
|
for (byte i = 0; i < radio.DATALEN; i++)
|
|
Serial.print((char)radio.DATA[i]);
|
|
Serial.print(" [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]");
|
|
|
|
if (radio.ACK_REQUESTED)
|
|
{
|
|
byte theNodeID = radio.SENDERID;
|
|
radio.sendACK();
|
|
Serial.print(" - ACK sent.");
|
|
|
|
// When a node requests an ACK, respond to the ACK
|
|
// and also send a packet requesting an ACK (every 3rd one only)
|
|
// This way both TX/RX NODE functions are tested on 1 end at the GATEWAY
|
|
/*
|
|
if (ackCount++%3)
|
|
{
|
|
Serial.print(" Sending packet to node ");
|
|
Serial.print(theNodeID);
|
|
delay(5);
|
|
radio.send(theNodeID, "ACK TEST", 8, true);
|
|
Serial.print(" - waiting for ACK...");
|
|
if (waitForAck(theNodeID)) Serial.print("ok!");
|
|
else Serial.print("nothing...");
|
|
}
|
|
*/
|
|
}
|
|
Serial.println();
|
|
Blink(LED,3);
|
|
}
|
|
}
|
|
|
|
// wait a few milliseconds for proper ACK to me, return true if indeed received
|
|
static bool waitForAck(byte theNodeID) {
|
|
long now = millis();
|
|
while (millis() - now <= ACK_TIME) {
|
|
if (radio.ACKReceived(theNodeID))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Blink(byte PIN, int DELAY_MS)
|
|
{
|
|
pinMode(PIN, OUTPUT);
|
|
digitalWrite(PIN,HIGH);
|
|
delay(DELAY_MS);
|
|
digitalWrite(PIN,LOW);
|
|
}
|