Hello,
i'm having an issue using the library to send a simple integer, using radiohead 1.101 library
i've googled and found similar threads(Arduino 433 mhz rf Sending numbers instead of strings - Programming Questions - Arduino Forum) but i can't get it to work at all even using the examples on the source.
sending board is a Leonard, receiver a pro mini.
I checked basic functionality using the send/receive examples as-is and it works ok
Checking the radiohead.h source i see an example to send a 16 bit int, but i can't get it to work, receiver gets nothing.
this is the sender, it's taken almost straight from the example(i'm not using the if for the send, i don't understand why the example uses it, the: if(!driver.send), but that's apart):
#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif
RH_ASK driver;
uint16_t data = 1023;
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
}
void loop()
{
driver.send((uint8_t *)&data, strlen(data));
driver.waitPacketSent();
delay(1000);
}
the receiver:
#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif
RH_ASK driver;
uint16_t data;
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
}
void loop()
{
uint8_t datalen = sizeof(data);
if (driver.recv((uint8_t*)&data, &datalen) && datalen == sizeof(data)) {
uint16_t xyz = data;
Serial.println(xyz);
}
}
yet nothing is received.
another thing i don't quite understand is why the AND "driver.recv((uint8_t*)&data, &datalen) && datalen == sizeof(data)", ¿doesn't the receive already compares the data length before returning the valid value?, as per RH documentation, datalen will contain the actual length of the received value, so ¿why that second check?, it should always be matching.
It makes no sense!, Unless: let's say i receive one byte only, data gets that byte, the datalen is "1" due to that(taken care from the recv function) , it then compares "1" to... 2 because sizeof(data) is operating on the variable that's a uint16_t, ¿is this right?
and why it does the "uint8_t datalen = sizeof(data);" is for?, data is an int, so datalen is 2 in this case, always
edit: also tried moving the "uint16_t data;" declaration inside the loop for the receive, no difference