Skip to content

Instantly share code, notes, and snippets.

@steviee
Created July 11, 2017 14:31
Show Gist options
  • Save steviee/574908712b9aa3e216ef8fefd7cf9f90 to your computer and use it in GitHub Desktop.
Save steviee/574908712b9aa3e216ef8fefd7cf9f90 to your computer and use it in GitHub Desktop.
NRF24l01 sender....
#include <RF24.h>
#include <SPI.h>
// PIR variables
byte pirPin = 2;
int pirCalibrationTime = 30;
// Radio with CE & CSN connected to pins 7 & 8
RF24 radio(7, 8);
// Constants that identify this node and the node to send data to
const uint16_t this_node = 1;
const uint16_t parent_node = 0;
// Time between packets (in ms)
const unsigned long interval = 10000; // every TEN sec
// Structure of our message
struct message_1 {
bool motion;
};
message_1 message;
void setup(void)
{
// Set up the Serial Monitor
Serial.begin(9600);
// Initialize all radio related modules
SPI.begin();
radio.begin();
delay(5);
// Calibrate PIR
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);
Serial.print("Calibrating PIR ");
for(int i = 0; i < pirCalibrationTime; i++)
{
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("PIR ACTIVE");
delay(50);
}
void loop() {
// Read motion: HIGH means motion is detected
bool m = (digitalRead(pirPin) == HIGH);
// Construct the message we'll send
message = (message_1){ m };
// Writing the message to the network means sending it
if (radio.write(&message, sizeof(message))) {
Serial.print("Message sent\n");
} else {
Serial.print("Could not send message\n");
}
// Wait a bit before we start over again
delay(interval);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment