0

I'm using the RadioHead library with ASK Driver to transmit/receive data with generic RF modules. It works fine, but I'm lacking some knowledge in programming the code to use the data received. Most examples I could find show how to send a string, but I only need to send and receive an integer.

I'm sending the decimal value '1' through the transmitter:

 uint8_t *data;
 uint8_t value;
 void loop() {
 value = 1;
 data = &value;
 driver.send(data, sizeof(value));
 driver.waitPacketSent();
 delay(200);
 }

And I have in the receiver:

void loop() {
 uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
 uint8_t buflen = sizeof(buf);
 int value;
 if (driver.recv(buf, &buflen)) {
 driver.printBuffer("Received:", buf, buflen); //prints 1.
 value = (int) buf; //my attempt to cast
 Serial.println(value); // the output is 2233, so this is wrong.
 }
 if (value == 1){ //example
 Serial.println("good");
 }
}

So what I think it is, I'm receiving bytes from the transmitter, which the library function 'driver.printBuffer' reinterprets as decimals and prints the value '1'. But how do I interpret (or cast) this as an integer so I can use it for other things like in the example (If condition) at the end? Any help is very much appreciated. Thanks.

asked Dec 28, 2017 at 13:09

1 Answer 1

1

buf is a pointer to the received bytes. Try value = buf[0]; instead.

answered Dec 28, 2017 at 13:38
1
  • 1
    This of course only works for small positive values that fit in an 8-bit type. For larger values you will need to be more sophisticated. Commented Dec 28, 2017 at 16:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.