433 MHz Data Transmission Help

Hello all,

I have recently followed this tutorial HERE to transmit data between two arduinos via 433 MHz modules.

The big picture idea is to sync two variables if one of them changes. So I am able to send data from the Tx to the Rx with no issue, as long as the value of the variable is 1000 or greater. When I send, say 1 or 100, I get the “*” operator displayed on the serial monitor. I am assuming this is because I am expecting a buffer size of 4 on the Rx, but am only sending a value that is 1 or 3 (value of 1 or 100 respectively).

Any idea on how to clear this up? Essentially I want to receive a value of 1 when I send a value of 1, not 1**8. Below are my codes for the Tx and Rx arduinos.

Tx Arduino

int red_score = 0;
int blue_score = 0;
String blue_scoreSTR;
String red_scoreSTR;
String ScoreSTR;
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library 
#include <SPI.h> 
 
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
 
void setup()
{
    // Initialize ASK Object
    rf_driver.init();
    Serial.begin(9600);
    blue_score = 0;
    red_score = 2;
}
 
void loop()
{

//    blue_scoreSTR = String(blue_score);
//    red_scoreSTR = String(red_score);
    ScoreSTR = String(blue_score + red_score);
    static char *msg = ScoreSTR.c_str();
    rf_driver.send((uint8_t *)msg, strlen(msg));
    rf_driver.waitPacketSent();
    Serial.println("Data Sent");
    //red_score++;
    delay(1000);
}

Now the Rx arduino

String blue_scoreSTR;
 int score = 0;
 String ScoreSTR;
 int ii =0;
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library 
#include <SPI.h> 
 
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
 
void setup()
{
    // Initialize ASK Object
    rf_driver.init();
    // Setup Serial Monitor
    Serial.begin(9600);
    Serial.println("listening..");
}
 
void loop()
{
    // Set buffer to size of expected message
    uint8_t buf[4];
    uint8_t buflen = sizeof(buf);
    // Check if received packet is correct size
    if (rf_driver.recv(buf, &buflen))
    {
      
      // Message received with valid checksum
      Serial.print("Message Received: ");
      Serial.println((char*)buf);  
      ScoreSTR = String((char*)buf);
      score = atoi(ScoreSTR.c_str());
      Serial.println(ScoreSTR); 
      Serial.println(score);

      for (ii=0;ii<=5;ii++){//just to play with the received value
      score = score + ii;
      Serial.println(score);
      }
    }
}

On a fresh upload, I receive this in the serial monitor:

Message Received: 2**8

2**8

2

2

3

5

8

12

17

Now, if I make the Tx code send a value of 2000 (blue_score = 2000, red_score = 0), I get this:

Message Received: 2000

2000

2000

2000

2001

2003

2006

2010

2015

Any help is greatly appreciated.

    ScoreSTR = String(blue_score + red_score);

Huge waste of resources.

    static char *msg = ScoreSTR.c_str();

Why did you make this static? Maintaining a pointer after the pointed to object goes out of scope is a REALLY bad idea.

      Serial.println((char*)buf);

buf is NOT a string. Do NOT pass it to functions that expect strings.

      ScoreSTR = String((char*)buf);

buf is NOT a string. Do NOT pass it to functions that expect strings.

      score = atoi(ScoreSTR.c_str());

Well, now that was stupid. The resource-wasting String class has a toInt() method.

So are you going to help, or just nitpick code from a tutorial?

It's been awhile since I played with my 433 MHz modules.
As I recall, the tutorials I used did not use String.
Fix that and see if it fixes your problem.

.

Thanks for your reply. What would you use instead of string?

Start with the example programs that come with the RadioHead library, e.g. RadioHead: ask_transmitter.pde

That way you should not be misled into using String objects, which often cause the Arduino to crash. Use C-strings (zero terminated character arrays) instead. Or just send data, as in the following, modified from the linked example:

void loop()
{
    int msg[2];
    msg[0] = red_score;
    msg[1] = blue_score;
    driver.send((uint8_t *) msg, 4); //transmit 2 integers
    driver.waitPacketSent();
    delay(200);
}