2018-11-26 18:58:34 +00:00
|
|
|
// **********************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// !!!! ATTENTION: !!!!
|
|
|
|
|
//
|
|
|
|
|
// This is just a simple receiving sketch that will work with most examples
|
|
|
|
|
// in the RFM69 library.
|
|
|
|
|
//
|
|
|
|
|
// If you're looking for the Gateway sketch to use with your RaspberryPi,
|
|
|
|
|
// as part of the PiGateway software interface (lowpowerlab.com/gateway),
|
|
|
|
|
// this is the wrong sketch.
|
|
|
|
|
//
|
|
|
|
|
// Use this sketch instead: PiGateway:
|
|
|
|
|
// https://github.com/LowPowerLab/RFM69/blob/master/Examples/PiGateway/PiGateway.ino
|
|
|
|
|
// **********************************************************************************
|
|
|
|
|
|
2015-11-11 03:57:38 +00:00
|
|
|
// Sample RFM69 receiver/gateway sketch, with ACK and optional encryption, and Automatic Transmission Control
|
2013-11-06 03:02:01 +00:00
|
|
|
// Passes through any wireless received messages to the serial port & responds to ACKs
|
|
|
|
|
// It also looks for an onboard FLASH chip, if present
|
2016-11-18 02:13:22 +00:00
|
|
|
// **********************************************************************************
|
|
|
|
|
// Copyright Felix Rusu 2016, 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)
|
2013-06-20 22:06:39 +01:00
|
|
|
|
2015-11-11 03:57:38 +00:00
|
|
|
//*********************************************************************************************
|
|
|
|
|
//************ IMPORTANT SETTINGS - YOU MUST CHANGE/CONFIGURE TO FIT YOUR HARDWARE *************
|
|
|
|
|
//*********************************************************************************************
|
2013-11-06 03:02:01 +00:00
|
|
|
#define NODEID 1 //unique for each node on same network
|
|
|
|
|
#define NETWORKID 100 //the same on all nodes that talk to each other
|
|
|
|
|
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
|
2019-01-15 19:49:11 +00:00
|
|
|
//#define FREQUENCY RF69_433MHZ
|
2013-11-06 03:02:01 +00:00
|
|
|
//#define FREQUENCY RF69_868MHZ
|
2019-01-15 19:49:11 +00:00
|
|
|
#define FREQUENCY RF69_915MHZ
|
2013-11-06 03:02:01 +00:00
|
|
|
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
|
2017-03-31 17:21:37 +01:00
|
|
|
#define IS_RFM69HW_HCW //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!
|
2016-11-18 02:13:22 +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
|
2015-11-11 03:57:38 +00:00
|
|
|
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
|
|
|
|
|
//*********************************************************************************************
|
2013-11-06 03:02:01 +00:00
|
|
|
#define SERIAL_BAUD 115200
|
2013-06-20 22:06:39 +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
|
|
|
|
|
|
2015-11-11 03:57:38 +00:00
|
|
|
#ifdef ENABLE_ATC
|
|
|
|
|
RFM69_ATC radio;
|
|
|
|
|
#else
|
|
|
|
|
RFM69 radio;
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-01-15 19:49:11 +00:00
|
|
|
SPIFlash flash(SS_FLASHMEM, 0xEF30); //EF30 for 4mbit Windbond chip (W25X40CL)
|
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);
|
2017-03-31 17:21:37 +01:00
|
|
|
#ifdef IS_RFM69HW_HCW
|
|
|
|
|
radio.setHighPower(); //must include this only for RFM69HW/HCW!
|
2013-11-06 03:02:01 +00:00
|
|
|
#endif
|
|
|
|
|
radio.encrypt(ENCRYPTKEY);
|
2013-06-20 22:06:39 +01:00
|
|
|
radio.promiscuous(promiscuousMode);
|
2015-11-11 03:57:38 +00:00
|
|
|
//radio.setFrequency(919000000); //set frequency to some custom frequency
|
2013-06-20 22:06:39 +01:00
|
|
|
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())
|
2014-08-13 03:15:38 +01:00
|
|
|
{
|
2014-11-17 19:58:00 +00:00
|
|
|
Serial.print("SPI Flash Init OK. Unique MAC = [");
|
2014-08-13 03:15:38 +01:00
|
|
|
flash.readUniqueId();
|
|
|
|
|
for (byte i=0;i<8;i++)
|
|
|
|
|
{
|
|
|
|
|
Serial.print(flash.UNIQUEID[i], HEX);
|
2014-11-17 19:58:00 +00:00
|
|
|
if (i!=8) Serial.print(':');
|
2014-08-13 03:15:38 +01:00
|
|
|
}
|
2014-11-17 19:58:00 +00:00
|
|
|
Serial.println(']');
|
|
|
|
|
|
2014-08-13 03:15:38 +01:00
|
|
|
//alternative way to read it:
|
|
|
|
|
//byte* MAC = flash.readUniqueId();
|
|
|
|
|
//for (byte i=0;i<8;i++)
|
|
|
|
|
//{
|
|
|
|
|
// Serial.print(MAC[i], HEX);
|
|
|
|
|
// Serial.print(' ');
|
|
|
|
|
//}
|
|
|
|
|
}
|
2013-07-30 21:50:37 +01:00
|
|
|
else
|
2015-11-11 03:57:38 +00:00
|
|
|
Serial.println("SPI Flash MEM not found (is chip soldered?)...");
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_ATC
|
|
|
|
|
Serial.println("RFM69_ATC Enabled (Auto Transmission Control)");
|
|
|
|
|
#endif
|
2013-06-20 22:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte ackCount=0;
|
2014-11-17 19:58:00 +00:00
|
|
|
uint32_t packetCount = 0;
|
2013-06-20 22:06:39 +01:00
|
|
|
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
|
2013-11-06 03:02:01 +00:00
|
|
|
radio.encrypt(ENCRYPTKEY);
|
2013-06-20 22:06:39 +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");
|
|
|
|
|
}
|
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();
|
|
|
|
|
}
|
2014-02-09 19:23:24 +00:00
|
|
|
if (input == 'D')
|
2013-07-30 21:50:37 +01:00
|
|
|
{
|
2014-08-13 03:15:38 +01:00
|
|
|
Serial.print("Deleting Flash chip ... ");
|
2013-07-30 21:50:37 +01:00
|
|
|
flash.chipErase();
|
|
|
|
|
while(flash.busy());
|
|
|
|
|
Serial.println("DONE");
|
|
|
|
|
}
|
|
|
|
|
if (input == 'i')
|
|
|
|
|
{
|
|
|
|
|
Serial.print("DeviceID: ");
|
|
|
|
|
word jedecid = flash.readDeviceId();
|
|
|
|
|
Serial.println(jedecid, HEX);
|
2013-09-06 00:32:11 +01:00
|
|
|
}
|
2013-11-06 03:02:01 +00:00
|
|
|
if (input == 't')
|
2013-09-06 00:32:11 +01:00
|
|
|
{
|
|
|
|
|
byte temperature = radio.readTemperature(-1); // -1 = user cal factor, adjust for correct ambient
|
|
|
|
|
byte fTemp = 1.8 * temperature + 32; // 9/5=1.8
|
|
|
|
|
Serial.print( "Radio Temp is ");
|
|
|
|
|
Serial.print(temperature);
|
|
|
|
|
Serial.print("C, ");
|
|
|
|
|
Serial.print(fTemp); //converting to F loses some resolution, obvious when C is on edge between 2 values (ie 26C=78F, 27C=80F)
|
|
|
|
|
Serial.println('F');
|
2013-07-30 21:50:37 +01:00
|
|
|
}
|
2013-06-20 22:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (radio.receiveDone())
|
|
|
|
|
{
|
2014-11-17 19:58:00 +00:00
|
|
|
Serial.print("#[");
|
|
|
|
|
Serial.print(++packetCount);
|
|
|
|
|
Serial.print(']');
|
2013-06-20 22:06:39 +01:00
|
|
|
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]);
|
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
|
|
|
|
2014-08-13 03:15:38 +01:00
|
|
|
if (radio.ACKRequested())
|
2013-06-20 22:06:39 +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
|
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);
|
2013-07-30 21:50:37 +01:00
|
|
|
Serial.print(" - ACK...");
|
2013-08-08 02:44:23 +01:00
|
|
|
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!");
|
2013-07-30 21:50:37 +01:00
|
|
|
else Serial.print("nothing");
|
2013-06-20 22:06:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Serial.println();
|
2019-01-15 19:49:11 +00:00
|
|
|
Blink(LED_BUILTIN,3);
|
2013-06-20 22:06:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2015-11-11 03:57:38 +00:00
|
|
|
}
|