Using NRF24l01 to check unknown message received and respond correctly

Let's say I have only 2 NRF radios.
I know NRF24 radios are not full duplex. So I want to keep them both listening and only stop listening to send a message.

If one sends one of the two messages

  1. abc
  2. def

and the one receiving will need to respond as such

  1. if abc is received do x
  2. if def is received do y

but won't know what message is being sent then how would I set this up?
I was thinking something like this:

radio 1

if (someValue == 1)
      {
     
        const char abc[] = "abc";
        radio.stopListening();
        delay(100);
        radio.write(&abc, sizeof(abc));
        delay(100);
        radio.startListening();
        delay(100);

if (someValue == 2)
      {     
        const char def[] = "def";
        radio.stopListening();
        delay(100);
        radio.write(&abc, sizeof(abc));
        delay(100);
        radio.startListening();
        delay(100);      
}

radio 2


if (radio.available())
  {
    char abc[32] = {0};                       
    radio.read(&abc, sizeof(abc));
 
    if (strcmp(abc, "abc"))
    {
     digitalWrite(pin 1, HIGH);
      delay(100);
    }
    char AlarmOff[32] = {0};                
    radio.read(&def, sizeof(def)); 
    if (strcmp(def, "def"))     
    {
      digitalWrite(pin 2, HIGH); 
     delay(100);

    }

Is that supposed to be char def[32] = {0}; ?

Why not something like:

 if (radio.available())
   {
      char payload[32];
      radio.read(&payload, sizeof(payload));

      if (strcmp(payload, "abc") == 0)
      {
         digitalWrite(pin_1, HIGH);  \\ pin 1 invalid identifier, no spaces allowed
         delay(100);
      }

      else if (strcmp(payload, "def") == 0)
      {
         digitalWrite(pin_2, HIGH);  \\ pin 2 invalid identifier, no spaces allowed
         delay(100);
      }
   }

Yes, it was to be def.

why check to see if the strcmp is equal to zero? I dont understand why.

also, why use
if...
else if...

vs
if..
if..

See a reference for the strcmp() function.

Return Value

Returns an integral value indicating the relationship between the strings:

return value indicates
<0 the first character that does not match has a lower value in ptr1 than in ptr2
0 the contents of both strings are equal
>0 the first character that does not match has a greater value in ptr1 than in ptr2

You could use
if ...
if ...
but
if ...
else if ...
will not check the else if if the if is true, saving a bit of time. See the else reference.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.