Hello,
Im working on a project where I want to put multiple devices in the same room and turn them on/off from a single control panel that would be placed somewhere in that room. I want to use nrf24l01 for this and my plan is to use one of them as a master and others as slaves. I would connect master on arduino board where I would also connect some buttons or touch sensors and the goal is when I press a certain button it sends message to all slaves in that room so they can turn on/off function that is associated with that button (each function would be differentiated with different message). Also I would like to get feedback from slaves if they received the message and if they didnt master should send message to that slave until it receives it. Wanting to go one step at a time I used Robin2 tutorial code and tried to send message between one master and 2 slaves. On the master side I put in code to send message to both slaves every 2 seconds and connected 1 led to each of them so every time slave receives that message the led should go on/off/on/off. Ive tried couple of different "versions" of code but most I could do is to be able to turn on and off on one slave while other didnt respond. Here is my latest version of code for master:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 8
const byte numSlaves = 2;
const byte slaveAddress[numSlaves][5] = {
// each slave needs a different address
{'S','L','V','0','1'},
{'S','L','V','0','2'}
};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char msg[7] = "on/off";
int ackData[2] = {-1, -1}; // to hold the two values coming from the slave
bool newData = false;
void setup() {
Serial.begin(9600);
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(3,5); // delay, count
}
void loop() {
for (byte n = 0; n < numSlaves; n++){
radio.openWritingPipe(slaveAddress[n]);
bool rslt;
rslt = radio.write(&msg,sizeof(msg));
if (rslt) {
if (radio.isAckPayloadAvailable()) {
radio.read(&ackData, sizeof(ackData));
newData = true;
}
else {
Serial.println(" Acknowledge but no data ");
}
}
else {
Serial.println(" Tx failed");
}
}
delay(2000);
}
and for one of slaves, other slave jsut has different address and led pin:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 8
const byte thisSlaveAddress[5] = {'S','L','V','0','2'};
RF24 radio(CE_PIN, CSN_PIN);
char msg[7];
int ackData[2] = {0, 2};
char comp[7] = "on/off";
int led = 9;
int state = LOW;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.enableAckPayload();
radio.startListening();
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
}
void loop() {
if (radio.available()) {
radio.read(&msg,sizeof(msg));
}
if(strcmp(msg,comp) == 0)
{
state= !state;
digitalWrite(led,state);
}
delay(200);
}