Question about float to uint8_t conversion

Hello,

I'm a newbie to arduino and arduino community and I have a little project using some sensor data with the lora technology. I'm using the lmic library for sending the data with an example code i found

void do_send(osjob_t* j){

float earth = D7S.getInstantaneusSI(); //function from D7S library that returns a float value like 0.15 String str_earth = String(earth);

uint8_t dataArray[str_earth.length()]; str_earth.toCharArray(dataArray, str_earth.length());

LMIC_setTxData2(1, dataArray, sizeof(dataArray)-1, 0);

}

I removed the extra lines of code that is not relevant to my problem.

Problem is that when the data I send from my arduino node are delivered to the server, the data that the server receives are wrong.

So for example if I sent something like 0.14 the server is receiving something like 0.1 and the last digit as NULL or something undefined. So I think there are some problems with my code. Actually I know that my code is really bad but I just started learning to code so please don't judge :P. For any further informations about the issue please let me know. Thank you.

    uint8_t dataArray[str_earth.length()];

You have not left room for the terminating NULL< so one character on the end of your String gets lopped off.

You could use dtstrf() avoiding the convoluted String code.

char buffer[10];

void setup() {
  Serial.begin(250000);
  float x = 1.23456;
  dtostrf(x, 0, 5, buffer);
  Serial.print(F("len "));
  Serial.print(strlen(buffer));
  Serial.print(F(", '"));
  Serial.print(buffer);
  Serial.println(F("'"));
}
void loop() {}
len 7, '1.23456'

Whandall: You could use dtstrf() avoiding the convoluted String code.

Beat me to it. 'Convoluted' was the exact word I was going to use, along with 'ugly'. Might need to cast the buffer type in the library call:

LMIC_setTxData2(1, (uint8_t *)buffer, strlen(buffer), 0);

You will either need to add the '\0' to the string at the far end or send it explicitly (strlen(buffer)+1).

Personally, I'd just send (and receive) the float as binary and avoid the ASCII conversion mess:

LMIC_setTxData2(1, (uint8_t *)(&earth), sizeof(float), 0);

gfvalvo:
Personally, I'd just send (and receive) the float as binary and avoid the ASCII conversion mess:

Thanks for the alternate idea.

A: Setup
uartUNO-5.png
Figure-1: Connection between UNO and NANO using SUART Ports

B: Sender Codes

#include<SoftwareSerial.h>
SoftwareSerial mySUART(10, 11); //SRX, STX

void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);

  float y = 1.23456; //binary32 formatted value is: 3F9E0601
  byte *ptr;
  ptr = (byte*) &y;
  for (int i = 0; i < 4; i++)
  {
    byte m = *ptr;
    Serial.print(m, HEX);
    mySUART.write(m);
    ptr++;
  }
  Serial.println();
}

void loop()
{

}

C: Receiver Codes

union
{
  float x;
  byte data[4];
} myData;

#include<SoftwareSerial.h>
SoftwareSerial mySUART(10, 11); //SRX, STX

void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
}

void loop()
{
  if (mySUART.available() == 4)
  {
    for (int i = 0; i < 4; i++)
    {
      myData.data[i] = mySUART.read();
      Serial.print(myData.data[i], HEX); //shows: 01069E3F (3F9E0601)
    }
    Serial.println();
    Serial.println(myData.x, 5); //shows: 1.23456
  }
}

uartUNO-5.png