Author Topic: just nee the radio to send a simple status of an discrete Input  (Read 501 times)

rbjassoc

  • Newbie
  • *
  • Posts: 2
So I'm new to this radio stuff and all I want to do is send a status of a discrete input from one radio to another. I went through the example code on sending basically text messages (Limited to 62 char) and that all works fine. But now I want to see a discrete input on one of my mini pros and send its staus to the other mini pro.  Can anyone shed some light on just sending booleans, true-false, HIGH LOW or 1 and 0 ???

TomWS

  • Hero Member
  • *****
  • Posts: 1888
Re: just nee the radio to send a simple status of an discrete Input
« Reply #1 on: August 16, 2018, 10:16:19 AM »
So I'm new to this radio stuff and all I want to do is send a status of a discrete input from one radio to another. I went through the example code on sending basically text messages (Limited to 62 char) and that all works fine. But now I want to see a discrete input on one of my mini pros and send its staus to the other mini pro.  Can anyone shed some light on just sending booleans, true-false, HIGH LOW or 1 and 0 ???
The data you send between two nodes is just that, 'data'.  So, when you send a collection of 'stuff', as long as the two ends agree on what that 'stuff' is, then you're ok.

So:

sender:
Code: [Select]
  uint8_t buf[5];     // room for 5 bytes
  ...
  buf[0] = digitalRead(INPUT_SWITCH);    // boolean value
  radio.sendWithRetry(OTHERNODE, buf, 1);   // send 1 byte

recvr:
Code: [Select]
  bool inputState;     
  ...
  if (radio.receiveDone()) { // check for incoming data
    radio.sendACK();
    if (radio.SENDERID == FIRSTNODE && radio.DATALEN >=1) {
       inputState = radio.DATA[0];        // only getting one byte (true or false)
    }
  }


rbjassoc

  • Newbie
  • *
  • Posts: 2
Re: just nee the radio to send a simple status of an discrete Input
« Reply #2 on: August 16, 2018, 04:11:46 PM »
Thankyou !!!!  because of your example, I was able to get it working !!! thx

the code:

Code: [Select]
if(cycleStart == true )
   {
     buttonState = digitalRead(buttonPin);
     if(buttonState == true)
     {
       // need to send change of state to true
       
       buf[0] = digitalRead(buttonPin);    // boolean value
         radio.send(TONODEID, buf, 1);
       Serial.println("True sent");
     }
     else
     {
       // need to send change of state to false
       buf[0] = false;    // boolean value
         radio.send(TONODEID, buf, 1);
       Serial.println("false sent");
     }
       
     cycleStart = false;
     digitalWrite(LED,HIGH);
     delay(50);
     digitalWrite(LED,LOW);
     
     }

Again thx a million!
« Last Edit: August 20, 2018, 03:22:49 PM by Felix »

ChemE

  • Sr. Member
  • ****
  • Posts: 402
  • Country: us
Re: just nee the radio to send a simple status of an discrete Input
« Reply #3 on: August 16, 2018, 04:59:05 PM »
Your code uses an if/then/else for really no good reason in the example you shared.  This is IMHO easier to read and maintain as well as smaller.  You cache the button state twice; once in buttonState and then again in the first byte of buf.  Probably the compiler is smart enough to make these optimizations but tight coding is just good practice.

Code: [Select]
if(cycleState == true ) {
  buttonState = digitalRead(buttonPin);
  radio.send(TONODEID, buttonState, 1);
  buttonState ? Serial.println("True sent") : Serial.println("False sent");

  cycleStart = false;
  digitalWrite(LED, HIGH);
  _delay_ms(50);
  digitalWrite(LED, LOW);
}

This is purely style but I actually prefer this:
Code: [Select]
#define LED 13
#define ON_PULSE 50
#define STROBE_LED digitalWrite(LED, HIGH); _delay_ms(ON_PULSE); digitalWrite(LED, LOW)

and then in the code body I just have to use
Code: [Select]
STROBE_LED;

which I think is even easier to maintain long term.  So the compact code becomes:

Code: [Select]
if(cycleState == true ) {
  buttonState = digitalRead(buttonPin);
  radio.send(TONODEID, buttonState, 1);
  buttonState ? Serial.println("True sent") : Serial.println("False sent");

  cycleStart = false;
  STROBE_LED;
}
« Last Edit: August 16, 2018, 05:11:55 PM by ChemE »

TomWS

  • Hero Member
  • *****
  • Posts: 1888
Re: just nee the radio to send a simple status of an discrete Input
« Reply #4 on: August 16, 2018, 05:22:13 PM »
Your code uses an if/then/else for really no good reason in the example you shared.  This is IMHO easier to read and maintain as well as smaller.  You cache the button state twice; once in buttonState and then again in the first byte of buf.  Probably the compiler is smart enough to make these optimizations but tight coding is just good practice.

Code: [Select]
if(cycleState == true ) {
  buttonState = digitalRead(buttonPin);
  radio.send(TONODEID, buttonState, 1);
  buttonState ? Serial.println("True sent") : Serial.println("False sent");

  cycleStart = false;
  digitalWrite(LED, HIGH);
  _delay_ms(50);
  digitalWrite(LED, LOW);
}
@ChemE, you are correct that the state of the button doesn't need to be tested as the actual state is being transmitted to the other end.

Unfortunately, I'm sorry to say, this is close but no cigar.

radio.send() needs an address of the variable, so that line should read:
Code: [Select]
  radio.send(TONODEID, &buttonState, 1);

Also, this is where the exchange of data between two entities REQUIRES that both entities agree on what is being sent.  In this case, if the two processors (and compilers) are the same, then treating both ends as the same type is ok, but ONLY if you send the correct amount of data.  On some processors bool will be larger than a single byte and the previous code could truncate the variable.  A safer implementation would be:

Code: [Select]
  radio.send(TONODEID, &buttonState, sizeof(buttonState));
and the receiving code would use a corresponding set of code to retrieve it.

Using an intermediate value of known size, in this case, is not only safe, but sufficiently readable.
Code: [Select]
  uint8_t buttonState = digitalRead(buttonPin);
  radio.send(TONODEID, &buttonState, 1);



ChemE

  • Sr. Member
  • ****
  • Posts: 402
  • Country: us
Re: just nee the radio to send a simple status of an discrete Input
« Reply #5 on: August 16, 2018, 05:43:25 PM »
Thanks Tom, can you tell my first languages didn't use pointers? ;)