2015-01-09 00:45:19 +00:00
|
|
|
// **********************************************************************************************************
|
2020-04-17 00:23:50 +01:00
|
|
|
// Moteino gateway/base sketch that works with Moteinos equipped with RFM69 transceiver
|
2016-08-16 14:30:31 +01:00
|
|
|
// This is a basic gateway sketch that receives packets from end node Moteinos, formats them as ASCII strings
|
|
|
|
|
// with the end node [ID] and passes them to Pi/host computer via serial port
|
|
|
|
|
// (ex: "messageFromNode" from node 123 gets passed to serial as "[123] messageFromNode")
|
|
|
|
|
// It also listens to serial messages that should be sent to listening end nodes
|
|
|
|
|
// (ex: "123:messageToNode" sends "messageToNode" to node 123)
|
|
|
|
|
// Make sure to adjust the settings to match your transceiver settings (frequency, HW etc).
|
2016-11-18 02:13:22 +00:00
|
|
|
// **********************************************************************************
|
2020-04-17 00:23:50 +01:00
|
|
|
// Copyright Felix Rusu 2020, http://www.LowPowerLab.com/contact
|
2016-11-18 02:13:22 +00:00
|
|
|
// **********************************************************************************
|
2016-08-16 14:30:31 +01:00
|
|
|
#include <RFM69.h> //get it here: https://github.com/lowpowerlab/rfm69
|
|
|
|
|
#include <RFM69_ATC.h> //get it here: https://github.com/lowpowerlab/RFM69
|
2016-11-18 02:13:22 +00:00
|
|
|
#include <RFM69_OTA.h> //get it here: https://github.com/lowpowerlab/RFM69
|
2016-08-16 14:30:31 +01:00
|
|
|
#include <SPIFlash.h> //get it here: https://github.com/lowpowerlab/spiflash
|
2016-11-18 02:13:22 +00:00
|
|
|
//****************************************************************************************************************
|
|
|
|
|
//**** IMPORTANT RADIO SETTINGS - YOU MUST CHANGE/CONFIGURE TO MATCH YOUR HARDWARE TRANSCEIVER CONFIGURATION! ****
|
|
|
|
|
//****************************************************************************************************************
|
2016-08-16 14:30:31 +01:00
|
|
|
#define NODEID 1 //the ID of this node
|
|
|
|
|
#define NETWORKID 200 //the network ID of all nodes this node listens/talks to
|
2015-01-09 00:45:19 +00:00
|
|
|
#define FREQUENCY RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
|
2016-08-16 14:30:31 +01:00
|
|
|
#define ENCRYPTKEY "sampleEncryptKey" //identical 16 characters/bytes on all nodes, not more not less!
|
2020-04-17 17:33:46 +01:00
|
|
|
#define IS_RFM69HW_HCW //required for RFM69HW/HCW, comment out for RFM69W/CW!
|
2016-11-18 02:13:22 +00:00
|
|
|
#define ACK_TIME 30 // # of ms to wait for an ack packet
|
|
|
|
|
//*****************************************************************************************************************************
|
2016-08-16 14:30:31 +01:00
|
|
|
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
|
2020-04-17 00:23:50 +01:00
|
|
|
#define ATC_RSSI -90 //target RSSI for RFM69_ATC (recommended > -80)
|
2016-08-16 14:30:31 +01:00
|
|
|
//*****************************************************************************************************************************
|
|
|
|
|
// Serial baud rate must match your Pi/host computer serial port baud rate!
|
2020-04-17 00:23:50 +01:00
|
|
|
#define DEBUG_EN //comment out if you don't want any serial verbose output
|
2020-04-17 17:33:46 +01:00
|
|
|
#define SERIAL_BAUD 19200
|
2015-01-09 00:45:19 +00:00
|
|
|
//*****************************************************************************************************************************
|
2020-04-17 00:23:50 +01:00
|
|
|
#ifdef DEBUG_EN
|
|
|
|
|
#define DEBUG(input) {Serial.print(input);}
|
|
|
|
|
#define DEBUGln(input) {Serial.println(input);}
|
2015-01-09 00:45:19 +00:00
|
|
|
#else
|
|
|
|
|
#define DEBUG(input);
|
|
|
|
|
#define DEBUGln(input);
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-04-17 00:23:50 +01:00
|
|
|
#define LED_HIGH digitalWrite(LED_BUILTIN, HIGH)
|
|
|
|
|
#define LED_LOW digitalWrite(LED_BUILTIN, LOW)
|
|
|
|
|
|
2016-08-16 14:30:31 +01:00
|
|
|
#ifdef ENABLE_ATC
|
|
|
|
|
RFM69_ATC radio;
|
|
|
|
|
#else
|
|
|
|
|
RFM69 radio;
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-04-17 00:23:50 +01:00
|
|
|
SPIFlash flash(SS_FLASHMEM, 0xEF30); //EF30 for 4mbit Windbond FlashMEM chip
|
2015-01-09 00:45:19 +00:00
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
|
Serial.begin(SERIAL_BAUD);
|
|
|
|
|
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!
|
2015-01-09 00:45:19 +00:00
|
|
|
#endif
|
|
|
|
|
radio.encrypt(ENCRYPTKEY);
|
2016-08-16 14:30:31 +01:00
|
|
|
|
|
|
|
|
#ifdef ENABLE_ATC
|
|
|
|
|
radio.enableAutoPower(ATC_RSSI);
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-01-09 00:45:19 +00:00
|
|
|
char buff[50];
|
2020-04-17 17:33:46 +01:00
|
|
|
sprintf(buff, "\nDEBUG:Transmitting at %d Mhz...", radio.getFrequency()/1000000);
|
2015-01-09 00:45:19 +00:00
|
|
|
DEBUGln(buff);
|
2016-08-16 14:30:31 +01:00
|
|
|
|
2015-01-09 00:45:19 +00:00
|
|
|
if (flash.initialize())
|
|
|
|
|
{
|
2020-04-17 17:33:46 +01:00
|
|
|
DEBUGln("DEBUG:SPI Flash Init OK!");
|
2015-01-09 00:45:19 +00:00
|
|
|
}
|
|
|
|
|
else
|
2016-08-16 14:30:31 +01:00
|
|
|
{
|
2020-04-17 17:33:46 +01:00
|
|
|
DEBUGln("DEBUG:SPI FlashMEM not found (is chip onboard?)");
|
2016-08-16 14:30:31 +01:00
|
|
|
}
|
2015-01-09 00:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte inputLen=0;
|
|
|
|
|
char input[64];
|
|
|
|
|
byte buff[61];
|
|
|
|
|
String inputstr;
|
2016-08-16 14:30:31 +01:00
|
|
|
|
2015-01-09 00:45:19 +00:00
|
|
|
void loop() {
|
|
|
|
|
inputLen = readSerialLine(input);
|
|
|
|
|
inputstr = String(input);
|
|
|
|
|
inputstr.toUpperCase();
|
|
|
|
|
|
|
|
|
|
if (inputLen > 0)
|
2020-04-17 00:23:50 +01:00
|
|
|
{
|
2015-01-09 00:45:19 +00:00
|
|
|
byte targetId = inputstr.toInt(); //extract ID if any
|
|
|
|
|
byte colonIndex = inputstr.indexOf(":"); //find position of first colon
|
2016-08-16 14:30:31 +01:00
|
|
|
|
|
|
|
|
if (targetId > 0)
|
|
|
|
|
{
|
|
|
|
|
inputstr = inputstr.substring(colonIndex+1); //trim "ID:" if any
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 00:45:19 +00:00
|
|
|
if (targetId > 0 && targetId != NODEID && targetId != RF69_BROADCAST_ADDR && colonIndex>0 && colonIndex<4 && inputstr.length()>0)
|
|
|
|
|
{
|
|
|
|
|
inputstr.getBytes(buff, 61);
|
|
|
|
|
if (radio.sendWithRetry(targetId, buff, inputstr.length()))
|
|
|
|
|
{
|
|
|
|
|
DEBUGln("ACK:OK");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
DEBUGln("ACK:NOK");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (radio.receiveDone())
|
|
|
|
|
{
|
2020-04-17 00:23:50 +01:00
|
|
|
LED_HIGH;
|
2015-01-09 00:45:19 +00:00
|
|
|
int rssi = radio.RSSI;
|
2020-04-17 17:33:46 +01:00
|
|
|
Serial.print('[');Serial.print(radio.SENDERID);Serial.print("] ");
|
2015-01-09 00:45:19 +00:00
|
|
|
if (radio.DATALEN > 0)
|
|
|
|
|
{
|
|
|
|
|
for (byte i = 0; i < radio.DATALEN; i++)
|
2020-04-17 17:33:46 +01:00
|
|
|
Serial.print((char)radio.DATA[i]);
|
|
|
|
|
Serial.print(" [RSSI:");Serial.print(rssi);Serial.print(']');
|
2015-01-09 00:45:19 +00:00
|
|
|
}
|
2020-04-17 17:33:46 +01:00
|
|
|
Serial.println();
|
|
|
|
|
|
2015-01-09 00:45:19 +00:00
|
|
|
CheckForWirelessHEX(radio, flash, false); //non verbose DEBUG
|
|
|
|
|
|
|
|
|
|
if (radio.ACKRequested())
|
|
|
|
|
{
|
|
|
|
|
radio.sendACK();
|
2020-04-17 17:33:46 +01:00
|
|
|
DEBUGln("DEBUG:ACK-sent");
|
2015-01-09 00:45:19 +00:00
|
|
|
}
|
2020-04-17 17:33:46 +01:00
|
|
|
|
2020-04-17 00:23:50 +01:00
|
|
|
LED_LOW;
|
2015-01-09 00:45:19 +00:00
|
|
|
}
|
2020-04-17 00:23:50 +01:00
|
|
|
}
|