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.

1

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

  • that makes sense, it worked perfectly thanks! – Chu Dec 28 '17 at 13:49
  • 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. – Chris Stratton Dec 28 '17 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.