I have successfully achieved communication between two arduino uno's with the NRF24l01 modules.
I can send a value of either 1 or 0, 1 should turn a relay on and 0 turn it off on pin 8.
when I read the serial data being received I get the result that I expect, but it doesn't activate or deactivate the relay! not really sure what I am doing wrong.
Please can you take a look at the code for the receiving arduino and give me some pointers.
Many thanks
John
/*
This Program will recieve data from the NRF24l01, depending on the result the relay on pin 8 will activate or deactivate. 1 on 0 off.
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int state; // holds relay state
int relay = 8; // declare pin 8 as relay
void setup()
{
Serial.begin(9600);
delay(1000);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}
//--(end setup )---
void loop()
{
if ( radio.available() )
{
// Read the data payload until we've received everything
bool done = false;
while (!done)
{
// Fetch the data
done = radio.read( &state, sizeof(unsigned long) );
Serial.println(state); // print the received data 0 or 1
digitalWrite(relay,state);// set relay as state: 1 on, 0 off
}
}
}
*
This Program will recieve data from the NRF24l01, depending on the result the relay on pin 8 will activate or deactivate. 1 on 0 off.
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int state; // holds relay state
int relay = 8; // declare pin 8 as relay
void setup()
{
Serial.begin(9600);
delay(1000);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
pinMode(relay, OUTPUT);
}
//--(end setup )---
void loop()
{
if ( radio.available() )
{
// Read the data payload until we've received everything
bool done = false;
while (!done)
{
// Fetch the data
done = radio.read( &state, sizeof(unsigned long) );
Serial.println(state); // print the received data 0 or 1
}
if (state = 1)
{
digitalWrite(relay,HIGH);// set relay as HIGH: on
}
}
}