2019-01-15 19:49:11 +00:00
|
|
|
// **********************************************************************************
|
|
|
|
|
// Struct Receive RFM69 Example
|
|
|
|
|
// **********************************************************************************
|
|
|
|
|
// Copyright Felix Rusu 2018, http://www.LowPowerLab.com/contact
|
|
|
|
|
// **********************************************************************************
|
|
|
|
|
// License
|
|
|
|
|
// **********************************************************************************
|
|
|
|
|
// This program is free software; you can redistribute it
|
|
|
|
|
// and/or modify it under the terms of the GNU General
|
|
|
|
|
// Public License as published by the Free Software
|
|
|
|
|
// Foundation; either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will
|
|
|
|
|
// be useful, but WITHOUT ANY WARRANTY; without even the
|
|
|
|
|
// implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
|
// PARTICULAR PURPOSE. See the GNU General Public
|
|
|
|
|
// License for more details.
|
|
|
|
|
//
|
|
|
|
|
// Licence can be viewed at
|
|
|
|
|
// http://www.gnu.org/licenses/gpl-3.0.txt
|
|
|
|
|
//
|
|
|
|
|
// Please maintain this license information along with authorship
|
|
|
|
|
// and copyright notices in any redistribution of this code
|
|
|
|
|
// **********************************************************************************
|
|
|
|
|
#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
|
|
|
|
|
#include <SPIFlash.h> //get it here: https://www.github.com/lowpowerlab/spiflash
|
|
|
|
|
#include <SPI.h> //included with Arduino IDE install (www.arduino.cc)
|
|
|
|
|
//*********************************************************************************************
|
|
|
|
|
//************ IMPORTANT SETTINGS - YOU MUST CHANGE/CONFIGURE TO FIT YOUR HARDWARE *************
|
|
|
|
|
//*********************************************************************************************
|
2013-08-18 02:57:34 +01:00
|
|
|
#define NODEID 1
|
|
|
|
|
#define NETWORKID 100
|
2019-01-15 19:49:11 +00:00
|
|
|
//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" //has to be same 16 characters/bytes on all nodes, not more not less!
|
2017-03-31 17:21:37 +01:00
|
|
|
#define IS_RFM69HW_HCW //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!
|
2019-01-15 19:49:11 +00:00
|
|
|
//*********************************************************************************************
|
|
|
|
|
//Auto Transmission Control - dials down transmit power to save battery
|
|
|
|
|
//Usually you do not need to always transmit at max output power
|
|
|
|
|
//By reducing TX power even a little you save a significant amount of battery power
|
|
|
|
|
//This setting enables this gateway to work with remote nodes that have ATC enabled to
|
|
|
|
|
//dial their power down to only the required level
|
|
|
|
|
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
|
|
|
|
|
//*********************************************************************************************
|
|
|
|
|
#define SERIAL_BAUD 115200
|
2013-08-18 02:57:34 +01:00
|
|
|
|
2019-01-15 20:06:41 +00:00
|
|
|
#if defined (MOTEINO_M0) && defined(SERIAL_PORT_USBVIRTUAL)
|
|
|
|
|
#define Serial SERIAL_PORT_USBVIRTUAL // Required for Serial on Zero based boards
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-01-15 19:49:11 +00:00
|
|
|
#ifdef ENABLE_ATC
|
|
|
|
|
RFM69_ATC radio;
|
|
|
|
|
#else
|
|
|
|
|
RFM69 radio;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
SPIFlash flash(SS_FLASHMEM, 0xEF30); //EF40 for 16mbit windbond chip
|
2013-08-18 02:57:34 +01:00
|
|
|
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network
|
|
|
|
|
|
2015-01-06 13:46:04 +00:00
|
|
|
typedef struct {
|
2013-08-18 02:57:34 +01:00
|
|
|
int nodeId; //store this nodeId
|
|
|
|
|
unsigned long uptime; //uptime in ms
|
|
|
|
|
float temp; //temperature maybe?
|
|
|
|
|
} Payload;
|
|
|
|
|
Payload theData;
|
|
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
|
Serial.begin(SERIAL_BAUD);
|
|
|
|
|
delay(10);
|
|
|
|
|
radio.initialize(FREQUENCY,NODEID,NETWORKID);
|
2017-03-31 17:21:37 +01:00
|
|
|
#ifdef IS_RFM69HW_HCW
|
|
|
|
|
radio.setHighPower(); //must include this only for RFM69HW/HCW!
|
|
|
|
|
#endif
|
2019-01-15 19:49:11 +00:00
|
|
|
radio.encrypt(ENCRYPTKEY);
|
2013-08-18 02:57:34 +01:00
|
|
|
radio.promiscuous(promiscuousMode);
|
|
|
|
|
char buff[50];
|
|
|
|
|
sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
|
|
|
|
|
Serial.println(buff);
|
|
|
|
|
if (flash.initialize())
|
|
|
|
|
Serial.println("SPI Flash Init OK!");
|
|
|
|
|
else
|
|
|
|
|
Serial.println("SPI Flash Init FAIL! (is chip present?)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte ackCount=0;
|
|
|
|
|
void loop() {
|
|
|
|
|
//process any serial input
|
|
|
|
|
if (Serial.available() > 0)
|
|
|
|
|
{
|
|
|
|
|
char input = Serial.read();
|
|
|
|
|
if (input == 'r') //d=dump all register values
|
|
|
|
|
radio.readAllRegs();
|
|
|
|
|
if (input == 'E') //E=enable encryption
|
2019-01-15 19:49:11 +00:00
|
|
|
radio.encrypt(ENCRYPTKEY);
|
2013-08-18 02:57:34 +01:00
|
|
|
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 (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();
|
|
|
|
|
}
|
2014-02-16 18:42:03 +00:00
|
|
|
if (input == 'D')
|
2013-08-18 02:57:34 +01:00
|
|
|
{
|
2014-02-16 18:42:03 +00:00
|
|
|
Serial.print("Deleting Flash chip content... ");
|
2013-08-18 02:57:34 +01:00
|
|
|
flash.chipErase();
|
|
|
|
|
while(flash.busy());
|
|
|
|
|
Serial.println("DONE");
|
|
|
|
|
}
|
|
|
|
|
if (input == 'i')
|
|
|
|
|
{
|
|
|
|
|
Serial.print("DeviceID: ");
|
|
|
|
|
word jedecid = flash.readDeviceId();
|
|
|
|
|
Serial.println(jedecid, HEX);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (radio.receiveDone())
|
|
|
|
|
{
|
|
|
|
|
Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
|
|
|
|
|
Serial.print(" [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]");
|
|
|
|
|
if (promiscuousMode)
|
2015-01-06 13:46:04 +00:00
|
|
|
{
|
2013-08-18 02:57:34 +01:00
|
|
|
Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
|
2013-08-18 03:15:07 +01:00
|
|
|
}
|
2015-01-06 13:46:04 +00:00
|
|
|
|
2013-08-18 02:57:34 +01:00
|
|
|
if (radio.DATALEN != sizeof(Payload))
|
|
|
|
|
Serial.print("Invalid payload received, not matching Payload struct!");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
theData = *(Payload*)radio.DATA; //assume radio.DATA actually contains our struct and not something else
|
|
|
|
|
Serial.print(" nodeId=");
|
|
|
|
|
Serial.print(theData.nodeId);
|
|
|
|
|
Serial.print(" uptime=");
|
|
|
|
|
Serial.print(theData.uptime);
|
|
|
|
|
Serial.print(" temp=");
|
|
|
|
|
Serial.print(theData.temp);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-13 03:38:58 +01:00
|
|
|
if (radio.ACKRequested())
|
2013-08-18 02:57:34 +01:00
|
|
|
{
|
|
|
|
|
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==0)
|
|
|
|
|
{
|
|
|
|
|
Serial.print(" Pinging node ");
|
|
|
|
|
Serial.print(theNodeID);
|
|
|
|
|
Serial.print(" - ACK...");
|
|
|
|
|
delay(3); //need this when sending right after reception .. ?
|
|
|
|
|
if (radio.sendWithRetry(theNodeID, "ACK TEST", 8, 0)) // 0 = only 1 attempt, no retries
|
|
|
|
|
Serial.print("ok!");
|
|
|
|
|
else Serial.print("nothing");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Serial.println();
|
2019-01-15 19:49:11 +00:00
|
|
|
Blink(LED_BUILTIN,3);
|
2013-08-18 02:57:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Blink(byte PIN, int DELAY_MS)
|
|
|
|
|
{
|
|
|
|
|
pinMode(PIN, OUTPUT);
|
|
|
|
|
digitalWrite(PIN,HIGH);
|
|
|
|
|
delay(DELAY_MS);
|
|
|
|
|
digitalWrite(PIN,LOW);
|
2019-01-15 19:49:11 +00:00
|
|
|
}
|