RF Tranmitter/Receiver Help

Hello, I'm wanting to turn on and off a relay using rf transmitters/receivers. I have three buttons hooked to the transmitter side, but for now I'm only using two (ON and OFF). The transmitter side is sending the message, although when you press the button you get 0OMS******8 with the "0" changing with the button press in the Serial Moniter. The main thing I'm trying to solve is turning on the LED_BUILTIN on the receiver side when the button on the transmitter side is pressed (When ON is pressed, the LED turns on when off is pressed, LED turns off). Thank You

RECEIVER CODE

#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile

RH_ASK driver;

void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);
    Serial.begin(9600);  // Debugging only
    if (!driver.init())
         Serial.println("init failed");
}

void loop()
{
      uint8_t buf[12];
    uint8_t buflen = sizeof(buf);
    if (driver.recv(buf, &buflen)) // Non-blocking
    {
      int i;
      // Message with a good checksum received, dump it.
      Serial.println((char*)buf); 
    }
    

}

TRANSMITTER CODE

#include <Servo.h>
#include <RH_ASK.h>
#include <SPI.h> 


int servoPin = 5;                                       //initializes the servo pin as digital pin 5


Servo servo1;

RH_ASK driver;

#define onButton 2                                      //defines digitalPin 3 as on button
#define offButton 3                                     //defines digitalPin 3 as off button
#define clButton 4                                      //defines digitalPin 4 as close button

void setup()
{ 
    pinMode(onButton, INPUT);                            //makes on button an INPUT
    pinMode(offButton,  INPUT);                          //makes off button an INPUT
    pinMode(clButton,  INPUT);                           //makes close button an INPUT
    pinMode(LED_BUILTIN, OUTPUT);
    servo1.attach(servoPin);
    servo1.write(0);
    digitalWrite(LED_BUILTIN, LOW);
    Serial.begin(9600);                                  //begins serial moniter at 9600 baud rate
    if (!driver.init())
         Serial.println("init failed");
}

void loop()
{
    const char *msg1 = "1";                     //RF on button message
    const char *msg2 = "2";                   //RF off button message
    const char *msg3 = "3";                  //RF close all button message
    const char *msg4 = "0";
    


    digitalRead(onButton);                               //reads on button state
    digitalRead(offButton);                              //reads off button state
    digitalRead(clButton);                               //reads close button state


   if(digitalRead(onButton) == HIGH){
    delay(25);
    driver.send((uint8_t *)msg1, strlen(msg1));      
    driver.waitPacketSent();
    Serial.println("1");
    digitalWrite(LED_BUILTIN, HIGH);
    servo1.write(180);
   }


    else{
    driver.send((uint8_t *)msg4, strlen(msg4));      
    driver.waitPacketSent();
    Serial.println("0");
    }


   if(digitalRead(offButton) == HIGH){
    delay(25);
    driver.send((uint8_t *)msg2, strlen(msg2));      
    driver.waitPacketSent();  
    Serial.println("2");
    digitalWrite(LED_BUILTIN, LOW);
    servo1.write(0);
    }


    else{
    driver.send((uint8_t *)msg4, strlen(msg4));      
    driver.waitPacketSent();
    Serial.println("0");
    }


   if(digitalRead(clButton) == HIGH){
    delay(25);
    driver.send((uint8_t *)msg3, strlen(msg3));
    driver.waitPacketSent();
    Serial.println("3");
    digitalWrite(LED_BUILTIN, LOW);
    servo1.write(0);
   
   }


   else{
    driver.send((uint8_t *)msg4, strlen(msg4));      
    driver.waitPacketSent();
    Serial.println("0");
    }
   
}

You print a buffer as a string although it isn't a string.

Try to replace

by

    driver.send((uint8_t *)msg1, strlen(msg1)+1);      

Do the same for the corresponding code for the other buttons.

This will also transfer the trailing null byte which tells the receiving side where the string ends so it won't print any contained garbage anymore.

How do I turn the LED_BUILTIN on/off on the receiver side when the buttons are pressed?

digitalWrite(LED_BUILTIN, 1);

I tried using if statements, but that didn't work. The only time it worked was when I typed the actual number in the receiver Serial monitor.

This was in the receiver code

if(Serial.available()){
        const int data = Serial.read();

       if(data == '1'){
        digitalWrite(led, HIGH);
       }

       else if(data == '2'){
        digitalWrite(led, LOW);
       }

       else if(data == '3'){
        digitalWrite(led, LOW);
       }    
   }

Post ALL the code, using code tags

What is connected to the pin identified in your program as "led"?

For the purpose of comparison with characters like '1', "data" should be declared char:

const int data = Serial.read();

Finally, check the setting on the serial monitor. Select line ending = none, otherwise you are sending line end characters in addition to whatever data are being sent.

See the Serial Input Basics tutorial posted on the forum.

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