to transmit float variable insted of hello world

202 views
Skip to first unread message

amit patel

unread,
Apr 26, 2014, 7:21:57 AM4/26/14
to rf22-a...@googlegroups.com

hello all,

 i want to transmit float variable in rf22_reliable_datagram_client exmple inplace of hello world could anyone help me how it's possible?
rf22_reliable_datagram_client and  rf22_reliable_datagram_server example working ok but i want to transmit float variable not inform of mes.

i have 3 float variable num,b,and c
num=b/c
 
i want to transmit num so how it can be done?

//------------------------------this is exmple of rf22_reliable_datagram ---------------------------------------
#include <RF22ReliableDatagram.h>
#include <RF22.h>
#include <SPI.h>

#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Singleton instance of the radio
RF22ReliableDatagram rf22(CLIENT_ADDRESS);

float num=0,b=20,c=11 ;

void setup()
{
  Serial.begin(9600);
  if (!rf22.init())
    Serial.println("RF22 init failed");
  // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36

     num=b/c;

}

uint8_t data[] = "Hello World!"; //i want to transmit num in pace of hello world how it can be done?
// Dont put this on the stack:
uint8_t buf[RF22_MAX_MESSAGE_LEN];

void loop()
{
  while (1)
  {
    Serial.println("Sending to rf22_datagram_server");
   
    // Send a message to rf22_server
    if (!rf22.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
      Serial.println("sendtoWait failed");
    else
    {
      // Now wait for a reply from the server
 //     Serial.println(rf22.lastRssi(), HEX); // of the ACK
      uint8_t len = sizeof(buf);
      uint8_t from;  
      if (rf22.recvfromAckTimeout(buf, &len, 2000, &from))
      {
        Serial.print("got reply from : 0x");
        Serial.print(from, HEX);
        Serial.print(": ");
        Serial.println((char*)buf);
      }
      else
      {
        Serial.println("No reply, is rf22_datagram_server running?");
      }
    }
    delay(500);
  }
}

stevech

unread,
Apr 26, 2014, 7:19:35 PM4/26/14
to rf22-a...@googlegroups.com
First, assumptions: floating point numbers stored in a computer/microprocessor have no standard format. If you KNOW that both ends are always the same type, we can simplify, as example:
There are many ways to code this. Here's one, sending the float as binary data (not text):

float f;
f = 123.456;

rf22.sendtoWait((uint8_t *) &f, sizeof(f), SERVER_ADDRESS)); // sends 4 byte floating point. A double might be 8 bytes.
In the above, the memory address of f is "&f". the (uint8_t *) is a C language cast so that the memory address of f is compatible with the 1st parameter of senttoWait().

On the receiving end you could receive this binary float and convert it as follows:
float f;
rf22.recvfromAckTimeout((uint8_t *) &f, sizeof(f), &len, 2000, &from);
Serial.print(f);

A better and universal way to exchange ints and floats, since they differ among computers and MCUs, it to convert the data to text for transmission purposes. This is machine-type-independent:
float f = 123.456;
sprintf(data, "%f is the value!\n"); // put text in the data buffer.  sprintf() is a standard C library function.
Serial.print((char *)data); // display what we will send.
rf22.sendtoWait(data, 1+strlen(char *)data, SERVER_ADDRESS)); // strlen is a C lib function. 1+ is to send the C string's terminating '\0'

On the other end, receiver the text
float f;
rf22.recvfromAckTimeout(buf, &len, 2000, &from));
Serial.print((char *)buf); // show what we got.
sscanf((char *)buf, "%f", &f); // parse and convert from text back to binary (this works on any MCU type.
Serial.print(f);

There are lots of other library tools and techniques.
An intro to C would have more examples. None of this needs to get into the complexity of C++ (yet).
Reply all
Reply to author
Forward
0 new messages