RFM69_LowPowerLab/Examples/Gateway/Gateway.ino

132 lines
3.7 KiB
Arduino
Raw Normal View History

2013-06-20 22:06:39 +01:00
#include <RFM69.h>
#include <SPI.h>
2013-07-30 21:50:37 +01:00
#include <SPIFlash.h>
2013-06-20 22:06:39 +01:00
#define NODEID 1
#define NETWORKID 100
2013-07-30 21:50:37 +01:00
#define FREQUENCY RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define KEY "thisIsEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
2013-06-20 22:06:39 +01:00
#define LED 9
#define SERIAL_BAUD 115200
2013-07-30 21:50:37 +01:00
#define ACK_TIME 30 // # of ms to wait for an ack
2013-06-20 22:06:39 +01:00
RFM69 radio;
2013-07-30 21:50:37 +01:00
SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip
2013-06-20 22:06:39 +01:00
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network
void setup() {
Serial.begin(SERIAL_BAUD);
2013-07-30 21:50:37 +01:00
delay(10);
2013-07-01 21:25:50 +01:00
radio.initialize(FREQUENCY,NODEID,NETWORKID);
2013-07-30 21:50:37 +01:00
//radio.setHighPower(); //uncomment only for RFM69HW!
2013-06-20 22:06:39 +01:00
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);
2013-07-30 21:50:37 +01:00
if (flash.initialize())
Serial.println("SPI Flash Init OK!");
else
Serial.println("SPI Flash Init FAIL! (is chip present?)");
2013-06-20 22:06:39 +01:00
}
byte ackCount=0;
void loop() {
//process any serial input
if (Serial.available() > 0)
{
char input = Serial.read();
2013-07-30 21:50:37 +01:00
if (input == 'r') //d=dump all register values
2013-06-20 22:06:39 +01:00
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");
}
2013-07-30 21:50:37 +01:00
if (input == 'd') //d=dump flash area
{
Serial.println("Flash content:");
int counter = 0;
while(counter<=256){
Serial.print(flash.readByte(counter++), HEX);
Serial.print('.');
}
while(flash.busy());
Serial.println();
}
if (input == 'e')
{
Serial.print("Erasing Flash chip ... ");
flash.chipErase();
while(flash.busy());
Serial.println("DONE");
}
if (input == 'i')
{
Serial.print("DeviceID: ");
word jedecid = flash.readDeviceId();
Serial.println(jedecid, HEX);
}
2013-06-20 22:06:39 +01:00
}
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
2013-07-30 21:50:37 +01:00
if (ackCount++%3==0)
2013-06-20 22:06:39 +01:00
{
2013-07-30 21:50:37 +01:00
Serial.print(" Pinging node ");
2013-06-20 22:06:39 +01:00
Serial.print(theNodeID);
delay(5);
radio.send(theNodeID, "ACK TEST", 8, true);
2013-07-30 21:50:37 +01:00
Serial.print(" - ACK...");
2013-06-20 22:06:39 +01:00
if (waitForAck(theNodeID)) Serial.print("ok!");
2013-07-30 21:50:37 +01:00
else Serial.print("nothing");
2013-06-20 22:06:39 +01:00
}
2013-07-30 21:50:37 +01:00
2013-06-20 22:06:39 +01:00
}
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;
}
2013-07-30 21:50:37 +01:00
void Blink(byte PIN, int DELAY_MS)
2013-06-20 22:06:39 +01:00
{
pinMode(PIN, OUTPUT);
digitalWrite(PIN,HIGH);
2013-07-30 21:50:37 +01:00
delay(DELAY_MS);
2013-06-20 22:06:39 +01:00
digitalWrite(PIN,LOW);
2013-07-30 21:50:37 +01:00
}