Radiohead problems

Hello all,
I'm trying to program two boards to communicate via 433mhz chips. Using radiohead library I'm sending a
"A", "B","C" respectively using push buttons to the other end when the other receives the one it lights up led. The problem is that either the message isn't being sent or it's not being received. Either way, when I press my button nothing happens.

Transmitter Module:

// ask_transmitter.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module
// Tested on Arduino Mega, Duemilanova, Uno, Due, Teensy, ESP-12

#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif

RH_ASK driver;
// RH_ASK driver(2000, 4, 5, 0); // ESP8266 or ESP32: do not use pin 11 or 2
// RH_ASK driver(2000, 3, 4, 0); // ATTiny, RX on D3 (pin 2 on attiny85) TX on D4 (pin 3 on attiny85), 
// RH_ASK driver(2000, PD14, PD13, 0); STM32F4 Discovery: see tx and rx on Orange and Red LEDS
const int button_a = 2;
const int button_b = 3;
const int button_c = 4;


void setup()
{
#ifdef RH_HAVE_SERIAL
    Serial.begin(9600);	  // Debugging only
#endif
    if (!driver.init())
#ifdef RH_HAVE_SERIAL
         Serial.println("init failed");
#else
	;
#endif
 pinMode(button_a, INPUT_PULLUP);
 pinMode(button_b,INPUT_PULLUP);
 pinMode(button_c,INPUT_PULLUP);
}

void loop()
{
    
    const char *msg1 = "A";
const char *msg2 = "B";
const char *msg3 = "C";

        if (digitalRead(button_a)) {
            driver.send((uint8_t *)msg1, strlen(msg1));
            Serial.println("its here alse 1");
    driver.waitPacketSent();
    Serial.println("its here 1");
    delay(200);
    Serial.println("Button pressed! 1");
    delay(1000); // Add a delay to prevent continuous readings while the button is pressed
  }
if (digitalRead(button_b)) {
            driver.send((uint8_t *)msg2, strlen(msg2));
    driver.waitPacketSent();
    delay(200);
    Serial.println("Button pressed! 1");
    delay(1000); // Add a delay to prevent continuous readings while the button is pressed
  }
  if (digitalRead(button_c)) {
            driver.send((uint8_t *)msg3, strlen(msg3));
    driver.waitPacketSent();
    delay(200);
    Serial.println("Button pressed! 1");
    delay(1000); // Add a delay to prevent continuous readings while the button is pressed
  }
    }

Transmittor Circuit

Receiver Module:

// ask_receiver.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to receive messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) receiver with an Rx-B1 module
// Tested on Arduino Mega, Duemilanova, Uno, Due, Teensy, ESP-12

#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif

RH_ASK driver;
// RH_ASK driver(2000, 4, 5, 0); // ESP8266 or ESP32: do not use pin 11 or 2
// RH_ASK driver(2000, 3, 4, 0); // ATTiny, RX on D3 (pin 2 on attiny85) TX on D4 (pin 3 on attiny85),
// RH_ASK driver(2000, PD14, PD13, 0); STM32F4 Discovery: see tx and rx on Orange and Red LEDS
const int led_a = 7;
const int led_b = 6;
const int led_c = 5;
void setup()
{
  pinMode(led_a, OUTPUT);
  pinMode(led_b, OUTPUT);
  pinMode(led_c, OUTPUT);
#ifdef RH_HAVE_SERIAL
  Serial.begin(115200);	  // Debugging only
#endif
  if (!driver.init())
#ifdef RH_HAVE_SERIAL
    Serial.println("init failed");
#else
    ;
#endif
}

void loop()
{
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);

  if (driver.available()) // Non-blocking
  {
    
    int i;
    String a = "";
    // Message with a good checksum received, dump it.
    //	driver.printBuffer("Got:", buf, buflen);
    for (int i = 0; i < buflen; i++) 
    {
      //Serial.println("main :: ");
      //Serial.print((char)buf[i]);
      a = a + (char)buf[i];
    
    }
Serial.println(a);
    if (a == "A") {
       digitalWrite(led_a, HIGH);
      Serial.println("in If");
    } else if (a == "B") {
       digitalWrite(led_b, HIGH);
      Serial.println("in ElseIf");
    }
    else if (a == "C") {
       digitalWrite(led_c, HIGH);
      Serial.println("C");
    }
  }
}

Receiver Circuit :

At the receiver, how do things get into "buf"?

It is an excellent idea to start with the RadioHead library examples and verify communications, before writing your own code. That will make it easier for you to spot the fatal error noted above.

Avoid using Strings on AVR based Arduinos. They cause malfunctions and program crashes.

i understood what your trying to say im sorry that I didn't notice but I have changed it from recv() to available() after looking at the library

I am completely new to this but I'm trying to learn

I have changed it from string to char array but still the problem follows please help :pray:

Did the library example not work, or did you forget to try it?

When asking about revised code, post it, using code tags.

Library example worked fine initially but I want light up the led when hitting a push button at the transmitter end
Initially I made few changes only to send the respective data on the transmitter end
And on the receiver end I add conditions to light up the Leds But nothing works

Except the library example.

2 Likes

Yes i am getting the data but when i print it is showing like this

// ask_receiver.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to receive messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) receiver with an Rx-B1 module
// Tested on Arduino Mega, Duemilanova, Uno, Due, Teensy, ESP-12

#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif

RH_ASK driver;
// RH_ASK driver(2000, 4, 5, 0); // ESP8266 or ESP32: do not use pin 11 or 2
// RH_ASK driver(2000, 3, 4, 0); // ATTiny, RX on D3 (pin 2 on attiny85) TX on D4 (pin 3 on attiny85),
// RH_ASK driver(2000, PD14, PD13, 0); STM32F4 Discovery: see tx and rx on Orange and Red LEDS
const int led_a = 7;
const int led_b = 6;
const int led_c = 5;
void setup()
{
  pinMode(led_a, OUTPUT);
  pinMode(led_b, OUTPUT);
  pinMode(led_c, OUTPUT);
#ifdef RH_HAVE_SERIAL
  Serial.begin(9600);	  // Debugging only
#endif
  if (!driver.init())
#ifdef RH_HAVE_SERIAL
    Serial.println("init failed");
#else
    ;
#endif
}

void loop()
{
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);

  if (driver.recv(buf, &buflen)) // Non-blocking
  {
    

     char a[RH_ASK_MAX_MESSAGE_LEN];
    // Message with a good checksum received, dump it.
//    	driver.printBuffer("Got:", (char)buf, buflen);
    for (int i = 0; i < buflen; i++) 
    {
      //Serial.println("main :: ");
      //Serial.print((char)buf[i]);
      a[i] = (char)buf[i];
    
    }
Serial.println(a);
    if (a[0] == "A") {
       digitalWrite(led_a, HIGH);
      Serial.println("in If");
    } else if (a[0] == "B") {
       digitalWrite(led_b, HIGH);
      Serial.println("in ElseIf");
    }
    else if (a[0] == "C") {
       digitalWrite(led_c, HIGH);
      Serial.println("C");
    }
  }
}

You're printing a as though it were a C-style string.
But you didn't terminate it.

1 Like

A character cannot be compared to a C-string. Try instead

if (a[0] == 'A') {

Thank you my problem has been addressed the LEDs are working now thanks again appreciate your help

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