// --------------------------------------------------------
// Set Board to Arduino Pro or Pro Mimi
// Set Processor to ATmega328P (3.3V, 8 MHz)
// Set COM port
// --------------------------------------------------------
#include <RFM69.h>
#include <LowPower.h>
// RFM69 frequency, uncomment the frequency of your module:
//#define FREQUENCY RF69_433MHZ
#define FREQUENCY RF69_915MHZ
// AES encryption (or not):
#define ENCRYPT true // Set to "true" to use encryption
//#define ENCRYPT false // Set to "true" to use encryption
#define ENCRYPTKEY "TOPSECRETPASSWRD" // Use the same 16-byte key on all nodes
// Use ACKnowledge when sending messages (or not):
#define USEACK true // Request ACKs or not
// Create a library object for our RFM69HCW module:
RFM69 radio;
//----------------------------------------------------------------
const byte interruptPin = 3;
// Addresses for this node. CHANGE THESE FOR EACH NODE!
int networkID = 144; // Must be the same for all nodes
int thisNodeID = 4; // This node ID - Set in setup
int toNodeID = 1; // Destination node ID
byte ledPin = 9;
char switchStateOPEN[] = "144-4-SO";
char switchStateCLOSED[] = "144-4-SC";
volatile bool interruptReceived = false;
byte sensorMode = LOW;
void setup ()
{
pinMode (interruptPin, INPUT_PULLUP);
attachInterrupt (digitalPinToInterrupt(interruptPin), handleInterrupt, CHANGE );
pinMode (ledPin, OUTPUT);
// Isolated relay output
pinMode (A0, OUTPUT);
digitalWrite (A0, LOW);
if (digitalRead(interruptPin) == LOW)
{
digitalWrite (ledPin, LOW);
}
else
{
digitalWrite (ledPin, HIGH);
}
// Switch Mode Pin
pinMode (8, INPUT_PULLUP);
if (digitalRead (8) == LOW)
{
// swap sensor action
sensorMode = HIGH;
}
// Initialize the RFM69HCW:
radio.initialize(FREQUENCY, thisNodeID, networkID);
//radio.initialize(FREQUENCY, myNodeID, NETWORKID);
radio.setHighPower(); // Always use this for RFM69HCW
// Turn on encryption if desired:
if (ENCRYPT) radio.encrypt(ENCRYPTKEY);
} //End setup()
void loop ()
{
if (interruptReceived == true)
{
sendSwitchState();
interruptReceived == false;
}
// Check again
if (interruptReceived == true)
{
sendSwitchState();
interruptReceived == false;
}
radio.sleep();
//LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
// do nothing
}
void handleInterrupt ()
{
interruptReceived = true;
}
void sendSwitchState()
{
if (digitalRead(interruptPin) == sensorMode)
//if (digitalRead(interruptPin) == LOW)
{
// Switch Closed
digitalWrite(ledPin,LOW);
if (radio.sendWithRetry(toNodeID, switchStateCLOSED, strlen(switchStateCLOSED)+1))
{ } // ACK received!
else
{ } // No ACK received
delay (1000);
}
else
{
// Switch Open
digitalWrite(ledPin,HIGH);
if (radio.sendWithRetry(toNodeID, switchStateOPEN, strlen(switchStateOPEN)+1))
{ } // ACK received!
else
{ } // No ACK received
digitalWrite (A0, HIGH);
delay (1000);
digitalWrite (A0, LOW);
}
} // End void sendSwitchState()