Casting integers with RF Module

Good night, everyone. I’m currently doing a project that requires me to communicate my car with a controller via RF Module 433MHZ. In my code, i need to send and to receive an integer bigger than 1 byte(I think 2 bytes are enought for me), but i’m not having success with it. One idea someone gave me is to separate my integer in two shorter integers, using the functions LowByte and HighByte, send them separately, and then putting them together in my Receiver Code with the function Word. It is not working, tho, and i would appreciate some ideas about how to do it, or why my code is not working. I know that the RF Module only sends information 1-byte size, thats the reason why i separated them in that way. Thats the parts related with the RF Module of my code:

Transmitter:

#include <RH_ASK.h> //INCLUSÃO DE BIBLIOTECA
#include <SPI.h> //INCLUSÃO DE BIBLIOTECA
RH_ASK driver; //CRIA O DRIVER PARA COMUNICAÇÃO

int mensagem1;
uint8_t Mensagem[2];
void setup() {
Serial.begin(9600);

if(!driver.init())
{Serial.println(“init failed”);} // inicia comunicação rf do driver
}
void loop() {

Mensagem[0]=lowByte(mensagem1);
Mensagem[1]=highByte(mensagem1);

driver.send(Mensagem, sizeof(Mensagem));
driver.waitPacketSent();
delay(200);
}

Receiver:

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

RH_ASK driver; // instância RH ASK

uint8_t value1,value2;

int Value;

void setup() {

Serial.begin(9600);

if(!driver.init())

{
Serial.println(“init failed”);
} // inicia comunicação rf do driver

}

void loop() {
//recebendo a msg
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);

if (driver.recv(buf, &buflen)) // se o gerenciador receber mensagem
{
driver.printBuffer(“Received:”, buf, buflen);

value1 = buf[0];
value2 = buf[1];
Value = word(value1,value2);
}

I would really appreciate any ideias of how may i fix it, or one better strategy to solve my problem.

Take a look at the prototype for the RH_ASK send function 'send()':

virtual bool    send(const uint8_t* data, uint8_t len);

It simply takes a pointer to your data and how big it is. The data can be anything: a variable, an array, a struct, an array of structs -- it doesn't matter. You don't have to break anything apart. Just give it the pointer (cast as a uint8_t *) and your data structure's size (in bytes).

Likewise the receive function:

virtual bool    recv(uint8_t* buf, uint8_t* len);

Just give it a pointer to where you want to put the data (cast as a uint8_t *) and a pointer to a uint8_t that contains the buffer's size. Usually, the buffer will be an identical data structure as used in the transmitter. In any case, make sure it's large enough and you put its length (in bytes) into the uint8_t whose pointer you pass before calling 'recv()'. On return the uint8_t variable will contain the actual number of bytes transferred.

It's almost like magic, your data structure will transferred from TX to RX.

Please use code tags ("</>" button) when posting code.

To send an int variable, on the transmitter side, try the following:

  int mensagem1 = 375; //for example
   driver.send((uint8_t *) &mensagem1, 2);
   ...

on the receiver side:

  int received_value = 0;
   uint8_t buflen = 2;
   if (driver.recv((uint8_t *) &received_value, &buflen))  {
      Serial.print("Received: ");
      Serial.println(received_value);
   ...

This works for any sort of variable, as long as you count the bytes correctly.