Code problem with RF module

Hi everyone, that's my first question on this forum.
I'm using two Arduino UNO talking trough these RF module (https://lastminuteengineers.com/433mhz-rf-wireless-arduino-tutorial/) the wiring of mine is the same of this guy's.
I'm having a problem with the two different codes: the receiver doesn't read the value that i'm sending. I'm using the RadioHead library (http://www.airspayce.com/mikem/arduino/RadioHead/ and I've changed the first example (the one that send the "hello world" message) into a code that send an integer value but I cannot see the value on my receiver.
I have already read the question similar to mine ([solved] Problem with 433mhz radiohead lib sending integer - #2 by Eliminateur) but i cannot understand what I've done wrong.
That's the code for the sender:

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK rf_driver;
uint8_t dato = 8;
void setup() {
  // Initialize ASK Object
  rf_driver.init();
  Serial.begin(9600);
}

void loop() {

  rf_driver.send((uint8_t *)dato, sizeof(dato));
  rf_driver.waitPacketSent();
  delay(1000);
}

and that's for the receiver:

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;
void setup() {
  driver.init();
  Serial.begin(9600);
  if (driver.init()) {
    Serial.println("connected");
  } else {
    Serial.println("failed connection");
  }
  Serial.println("Start");
}
void loop() {
  uint16_t data;
  uint8_t datalen = sizeof(data);
  //uint16_t xyz=0;
  if (driver.recv(data, &datalen) && datalen == sizeof(data)) {
    long xyz = data;
    Serial.print("data value: ");
    Serial.print(data);
    Serial.println();
    Serial.print("xyz value: ");
    Serial.print(xyz);
    Serial.println();
    Serial.println(datalen);
  }
  delay(1000);
}

Thank you in advance or your response! <3

The first thing to do is to verify that your setup works "out of the box" with the RadioHead library, using one of the simple examples.

If that works, then you can modify the code.

If it doesn't, eliminate the possibility of faulty radios or faulty radio wiring by connecting the two Arduinos with wires (as described in the library documentation), and test again.

you are transmitting a unit8_t and attempting to receive a unit16_t therefore

  if (driver.recv(data, &datalen) && datalen == sizeof(data)) {

the sizes are different and above statement fails
make both unit16_t?

I've tried the first example ( the one who sends "hello world" from one to another) and it worked

The sta

The statement doesn't fail because it print something, that's my conclusion but maybe I'm wrong about it. I've also changed the size of the variables but still nothing.

you forgot to use the address of operator & to take the address of the parameter
transmitter

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK rf_driver;
uint16_t dato = 8;
void setup() {
  // Initialize ASK Object
  rf_driver.init();
  Serial.begin(115200);
}

void loop() {
  // use the & operator to take address of dato
  rf_driver.send((uint8_t *)&dato, sizeof(dato));
  rf_driver.waitPacketSent();
  delay(1000);
}

receiver

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;
void setup() {
  driver.init();
  Serial.begin(115200);
  if (driver.init()) {
    Serial.println("connected");
  } else {
    Serial.println("failed connection");
  }
  Serial.println("Start");
}
void loop() {
  uint16_t data;
  uint8_t datalen = sizeof(data);
  //uint16_t xyz=0;
   // use the & operator to take address of data and cast to uint8_t
  if (driver.recv((uint8_t *) &data, &datalen) && datalen == sizeof(data)) {
    long xyz = data;
    Serial.print("data value: ");
    Serial.print(data);
    Serial.println();
    Serial.print("xyz value: ");
    Serial.print(xyz);
    Serial.println();
    Serial.println(datalen);
  }
  delay(1000);
}

receiver output

data value: 8
xyz value: 8
2
data value: 8
xyz value: 8
2
data value: 8
xyz value: 8
2
data value: 8
xyz value: 8
2

Thank you so much!

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