I'm trying to make a network of interconnected sensors but I can't figure out the code for the NRF24l01.
What I was trying to do in this example is make the transmitter send a message to the receiver when the sensor is triggered. When the receiver gets the message, it will light an Led.
Transmitter
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int sensor = 7; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup(void){
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
pinMode(sensor, INPUT);
}
void loop(void){
val = digitalRead(sensor);
if (val == HIGH){
msg[0] = 111;
radio.write(msg, 1);}
}
Receiver
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int LED1 = 3;
void setup(void){
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
pinMode(LED1, OUTPUT);}
void loop(void){
if (radio.available()){
bool done = false;
while (!done){
done = radio.read(msg, 1);
Serial.println(msg[0]);
if (msg[0] == 111){delay(10);digitalWrite(LED1, HIGH);}
else {digitalWrite(LED1, LOW);}
delay(10);}}
else{Serial.println("No radio available");
}
}