help please: cannot get 434Mhz receiver coding work

I am trying to turn a LED on and off using 434Mhz receiver and transmitter. I am able to to get the basic communication work between the receiver and transmitter. However, I cannot the coding work by using the text message the receiver gets. Please see my coding below:

on the transmitter side:

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

const char* msg1=“Y”;
driver.send((uint8_t *)msg1, strlen(msg1));

On the receiver side:

uint8_t buf[1];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen))
// Message received with valid checksum
Serial.print("Message Received: ");
if ((char*)buf==“Y”)Serial.println(“light on”);

the serial monitor shows:
18:45:49.151 → Message Received: Y
18:45:49.424 → Message Received: Y
18:45:49.697 → Message Received: Y
18:45:49.972 → Message Received: Y
18:45:50.245 → Message Received: Y
18:45:50.519 → Message Received: Y
18:45:50.760 → Message Received: Y

why does not the coding on the receiver side if ((char*)buf==“Y”)Serial.println(“light on”); work???
Please help.

It doesn't work because you are creating a NEW version of the buffer each time. Get rid of the (char*) part.


This is not a good idea. If you receive more than one character, your program will probably crash. Make buf larger than any message you expect to receive, plus one for the zero terminator.

    uint8_t buf[1];

You cannot compare C-strings this way:

if ((char*)buf=="Y")

You can compare individual characters or C-strings

if (buf[0] == 'Y') do_something();
if (strcmp(buf,"Y") == 0) do_something_else():

But for the string comparison to work, you MUST make sure that that the character array buf is properly zero-terminated.

If you are expecting to receive just a single character, do that by setting buff[1] to zero, after reception (and declare it to be length 2 or greater).

This is a brilliant idea using if(buf[0] == 'Y') and I understand why this would work.
I figure out a different solution---> if ((char)buf=='Y').

Thank you .jremington

The two solutions are just different ways of representing the same operation.