RFM69_LowPowerLab/Examples/Node/Node.ino

196 lines
6.2 KiB
Arduino
Raw Normal View History

// Sample RFM69 sender/node sketch, with ACK and optional encryption, and Automatic Transmission Control
2013-11-06 03:02:01 +00:00
// Sends periodic messages of increasing length to gateway (id=1)
// It also looks for an onboard FLASH chip, if present
// RFM69 library and sample code by Felix Rusu - http://LowPowerLab.com/contact
// Copyright Felix Rusu (2015)
2014-11-03 16:24:39 +00:00
#include <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
#include <RFM69_ATC.h>//get it here: https://www.github.com/lowpowerlab/rfm69
2013-06-20 22:06:39 +01:00
#include <SPI.h>
2014-11-03 16:24:39 +00:00
#include <SPIFlash.h> //get it here: https://www.github.com/lowpowerlab/spiflash
2013-06-20 22:06:39 +01:00
//*********************************************************************************************
//************ IMPORTANT SETTINGS - YOU MUST CHANGE/CONFIGURE TO FIT YOUR HARDWARE *************
//*********************************************************************************************
#define NODEID 2 //must be unique for each node on same network (range up to 254, 255 is used for broadcast)
#define NETWORKID 100 //the same on all nodes that talk to each other (range up to 255)
2013-11-06 03:02:01 +00:00
#define GATEWAYID 1
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_868MHZ
//#define FREQUENCY RF69_915MHZ
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
//#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
//*********************************************************************************************
#ifdef __AVR_ATmega1284P__
#define LED 15 // Moteino MEGAs have LEDs on D15
#define FLASH_SS 23 // and FLASH SS on D23
#else
#define LED 9 // Moteinos have LEDs on D9
#define FLASH_SS 8 // and FLASH SS on D8
#endif
2013-11-06 03:02:01 +00:00
#define SERIAL_BAUD 115200
2013-06-20 22:06:39 +01:00
int TRANSMITPERIOD = 150; //transmit a packet to gateway so often (in ms)
2013-06-20 22:06:39 +01:00
char payload[] = "123 ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2013-11-06 03:02:01 +00:00
char buff[20];
2013-06-20 22:06:39 +01:00
byte sendSize=0;
boolean requestACK = false;
SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit Windbond chip (W25X40CL)
#ifdef ENABLE_ATC
RFM69_ATC radio;
#else
RFM69 radio;
#endif
2013-06-20 22:06:39 +01:00
void setup() {
Serial.begin(SERIAL_BAUD);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
2013-11-06 03:02:01 +00:00
#ifdef IS_RFM69HW
radio.setHighPower(); //uncomment only for RFM69HW!
#endif
radio.encrypt(ENCRYPTKEY);
//radio.setFrequency(919000000); //set frequency to some custom frequency
//Auto Transmission Control - dials down transmit power to save battery (-100 is the noise floor, -90 is still pretty good)
//For indoor nodes that are pretty static and at pretty stable temperatures (like a MotionMote) -90dBm is quite safe
//For more variable nodes that can expect to move or experience larger temp drifts a lower margin like -70 to -80 would probably be better
//Always test your ATC mote in the edge cases in your own environment to ensure ATC will perform as you expect
#ifdef ENABLE_ATC
radio.enableAutoPower(-70);
#endif
2013-06-20 22:06:39 +01:00
char buff[50];
sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
Serial.println(buff);
2013-11-06 03:02:01 +00:00
2013-07-30 21:50:37 +01:00
if (flash.initialize())
{
Serial.print("SPI Flash Init OK ... UniqueID (MAC): ");
flash.readUniqueId();
for (byte i=0;i<8;i++)
{
Serial.print(flash.UNIQUEID[i], HEX);
Serial.print(' ');
}
Serial.println();
}
2013-07-30 21:50:37 +01:00
else
Serial.println("SPI Flash MEM not found (is chip soldered?)...");
#ifdef ENABLE_ATC
Serial.println("RFM69_ATC Enabled (Auto Transmission Control)\n");
#endif
2013-06-20 22:06:39 +01:00
}
2014-11-03 16:24:39 +00:00
long lastPeriod = 0;
2013-06-20 22:06:39 +01:00
void loop() {
//process any serial input
if (Serial.available() > 0)
{
char input = Serial.read();
if (input >= 48 && input <= 57) //[0,9]
{
TRANSMITPERIOD = 100 * (input-48);
if (TRANSMITPERIOD == 0) TRANSMITPERIOD = 1000;
Serial.print("\nChanging delay to ");
Serial.print(TRANSMITPERIOD);
Serial.println("ms\n");
}
2013-07-30 21:50:37 +01:00
if (input == 'r') //d=dump register values
2013-06-20 22:06:39 +01:00
radio.readAllRegs();
2013-08-05 15:34:03 +01:00
//if (input == 'E') //E=enable encryption
// radio.encrypt(KEY);
//if (input == 'e') //e=disable encryption
// radio.encrypt(null);
2013-07-30 21:50:37 +01:00
if (input == 'd') //d=dump flash area
{
Serial.println("Flash content:");
uint16_t counter = 0;
2013-07-30 21:50:37 +01:00
Serial.print("0-256: ");
2013-07-30 21:50:37 +01:00
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
}
//check for any received packets
if (radio.receiveDone())
{
Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
for (byte i = 0; i < radio.DATALEN; i++)
Serial.print((char)radio.DATA[i]);
2013-11-07 04:15:51 +00:00
Serial.print(" [RX_RSSI:");Serial.print(radio.RSSI);Serial.print("]");
2013-06-20 22:06:39 +01:00
if (radio.ACKRequested())
2013-06-20 22:06:39 +01:00
{
radio.sendACK();
2013-07-30 21:50:37 +01:00
Serial.print(" - ACK sent");
2013-06-20 22:06:39 +01:00
}
Blink(LED,3);
2013-06-20 22:06:39 +01:00
Serial.println();
}
2013-06-20 22:06:39 +01:00
int currPeriod = millis()/TRANSMITPERIOD;
if (currPeriod != lastPeriod)
{
lastPeriod=currPeriod;
//send FLASH id
if(sendSize==0)
{
sprintf(buff, "FLASH_MEM_ID:0x%X", flash.readDeviceId());
byte buffLen=strlen(buff);
if (radio.sendWithRetry(GATEWAYID, buff, buffLen))
Serial.print(" ok!");
else Serial.print(" nothing...");
//sendSize = (sendSize + 1) % 31;
}
else
{
Serial.print("Sending[");
Serial.print(sendSize);
Serial.print("]: ");
for(byte i = 0; i < sendSize; i++)
Serial.print((char)payload[i]);
if (radio.sendWithRetry(GATEWAYID, payload, sendSize))
Serial.print(" ok!");
else Serial.print(" nothing...");
}
2013-06-20 22:06:39 +01:00
sendSize = (sendSize + 1) % 31;
Serial.println();
Blink(LED,3);
}
}
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);
}