polling NRF24L01 for pin data

Hi all,

Sorry, I have searched all day for this and can't really find a definitive answer.

I am using reed witched to see if doors windows etc are open.

This is to feed home automation software.

I need 1 master and multiple slaves.

How do I go about the master polling each of the slaves to see the state of x number of pins?

I sm going to use battery-operated nanos as slaves

Many thanks

You should probably read this tutorial from Robin2.

Personally i would have the slaves just constantly send the data to the master. If you want to poll the slaves you would send a message to the slave with a known payload compare the data on the slave devices. If it matches what you sent you send a message back to the master with your "update command" followed by the data.

Here is a very sloppy example of a slave parsing incoming data and reacting to it. Maybe you can find something useful in there to point you in the right direction

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

const int pinCE = 9;
const int pinCSN = 10;
const int pH1Pin = A3;
const int pH2Pin = A4;
int sendState = 1;
int phreadstate = 1;
bool recvState = true;

float ph1Value = 5.89;
float ph1Update = 22.22;
float incomingArray[32];
float ph1Request = 12.34;
float ph2Value = 5.89;

unsigned long retryRefreshMarker = 0;
unsigned long respondDelay = 0;
unsigned long phRefreshMarker = 0;
unsigned long nrftimeout = 0;
unsigned long serialPrintDelay = 0;

int buf[64], temp;
int buf2[64], temp2;

unsigned long int pH1avgValue;
unsigned long int pH2avgValue;
RF24 wirelessSPI(pinCE, pinCSN);
const uint64_t pAddress = 0xB00B1E5000F0;
const byte numChars = 16; //MAX LENGTH OF SERIAL STRING
char receivedChars[numChars];
char tempChars[numChars];
char StringPt[numChars] = {0};
unsigned long integerPt = 0;
unsigned long integerPt2 = 0;
float IncomingFloatValue = 0.0;
bool newData = false;
void setup() {
  Serial.begin(57600);
  Serial.println("BOOTING");
  wirelessSPI.begin();
  wirelessSPI.setDataRate(RF24_1MBPS);
  wirelessSPI.setDataRate(RF24_250KBPS);
  wirelessSPI.setAutoAck(1);
  wirelessSPI.setCRCLength(8);
  wirelessSPI.enableAckPayload();
  wirelessSPI.setRetries(5, 5);
  wirelessSPI.openReadingPipe(1, pAddress);
  wirelessSPI.startListening();
}

void loop() {
  //if (recvState){
  readResponse();
  recvWithStartEndMarkers();
  if (newData) {
    strcpy(tempChars, receivedChars);
    parseData();
    showParsedData();
    newData = false;
  }


  if (millis() - respondDelay > 100) {
    sendData();
    respondDelay = millis();
  }




  if (millis() - phRefreshMarker > 850) {
    if (phreadstate == 1) {
      for (int i = 0; i < 60; i++)  {
        buf[i] = analogRead(pH1Pin);
       
      //  delay(55);
        if (i >= 59) {
          phreadstate = 2;  
         // delay(100);
        }
      }
    }
    if (phreadstate == 2) {
      for (int i = 0; i < 60; i++)  {
        buf2[i] = analogRead(pH2Pin); //ph 2 set to ph1pin for now
        //delay(55);
        if (i >= 59) {
          phreadstate = 1;
        //  delay(100);
        }
      }
    }

    pH1avgValue = 0;
    for (int i = 0; i < 60; i++)
      pH1avgValue += buf[i];
    float pHVol = (float)pH1avgValue * 5.0 / 1024 / 60;
    ph1Value = -5.7 * pHVol + 24.4;
    pH2avgValue  = 0;
    for (int i = 0; i < 60; i++)
      pH2avgValue  += buf2[i]; 
    float pHVol2 = (float)pH2avgValue  * 5.0 / 1024 / 60;
    ph2Value = -5.7 * pHVol2 + 24.4;
    phRefreshMarker = millis();
  }
}


void sendData() {
  // Serial.println("sending ata");
  wirelessSPI.stopListening();
  wirelessSPI.openWritingPipe(pAddress);
  float Array[3] = {ph1Update, ph1Value, ph2Value};
  if (!wirelessSPI.write( &Array, sizeof(Array))) {
    // Serial.println("send failed");
  }
  else {
    //  Serial.println("Send okayY");


  }
}
void readResponse() {
  //  wirelessSPI.stopListening();

  wirelessSPI.startListening();
  while (wirelessSPI.available()) {
    wirelessSPI.read( &incomingArray, sizeof(incomingArray) );
    //Serial.println("sending try");
    float incomingResponse = float(incomingArray[0]);
    if (incomingResponse == ph1Request) {  //if incoming cammand ==

      wirelessSPI.flush_rx();
      //  wirelessSPI.flush_tx();
      sendState = 1;
      Serial.println("message from tx");

    }
  }
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';

  while ((Serial.available() > 0) && (!newData)) {
    long rc = Serial.read();
    if (recvInProgress) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0';
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}
void RefreshSerial() {
  Serial.println(ph1Value);
  Serial.println(ph2Value);
  Serial.println("$");
}
void parseData() {
  char * strtokIndx;
  strtokIndx = strtok(tempChars, ",");
  strcpy(StringPt, strtokIndx);
  strtokIndx = strtok(NULL, ",");
  integerPt = atol(strtokIndx);
  strtokIndx = strtok(NULL, ",");
  integerPt2 = atof(strtokIndx);
  //IncomingFloatValue = atof(strtokIndx);
}

void showParsedData() {
  if (strcmp(StringPt, "Refresh") == 0) {
    RefreshSerial();
  }

}

Thats perfect thank you.

is there not a clash issue with all sending at once?

In this Simple nRF24L01+ Tutorial there is an example for a master and 2 slaves that can easily be extended to a larger number of slaves. IMHO it is a great deal simpler than the code in Reply #1.

...R

that's exactly what i need, thank you, i was using your code from the first page, should have looked at the second for multiple slaves.

is there a limit to the number of slaves can be set up?

Many thanks

JonMiles:
is there a limit to the number of slaves can be set up?

Not really. How many do you need?

If you have a very large number then the time taken to poll each of them in succession might mean that they are not polled often enough.

...R

it was only going to be about 12, so perfect

thank you so much for your guide