Converting a float to uint8_t for LoRa radio

Good mornig,
i read the forum Converting a float to uint8_t for LoRa radio 433MHz.
I used it to make a tx.ino and a rx.ino in order to send a temperature float value from a tx lora module to a rx lora module.
It works but i do not understand how it is possible sending through the software serial a pointer and then dereference it in the receiver.
I refer to these lines

TX.ino
(...)
mySerial.write((uint8_t *)(&t), sizeof(t));
(...)

RX.ino
(...)
uint8_t buf1[4];
uint8_t len1 = sizeof(buf1);
mySerial.readBytes(buf1, len1);
t = *((float *)buf1);
(...)

Thanks a lot!
best regards

here it is the tx.ino and rx.ino code:

TX CODE

// Qui spiega come convertire un float in uint8_t
// Converting a float to uint8_t for LoRa radio 433MHz

digita o incolla il codice qui

#include "Arduino.h"
#include "LoRa_E32.h"
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include "DHT.h"

#define DHTPIN 4
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

SoftwareSerial mySerial(3, 2); // e32 TX e32 RX
 


void setup() {

  Serial.begin(9600);
  dht.begin();
  mySerial.begin(9600);
  
  delay(500);
  
  Serial.println("Master");  
}



void loop() {
  float t;
  float u;

  delay(2000);
  t = dht.readTemperature();
  u = dht.readHumidity();

  if (isnan(t) || isnan(u)) {
    Serial.println("Errore di lettura del sensore!");
    return;
  }


/*  char str1[30];
  String str2=(String)t;
  int len =snprintf(str1,30,"Hello!");
  uint8_t bval = (uint8_t)t;
  mySerial.write((uint8_t *)str1, 2);
  mySerial.write(bval);
*/

  mySerial.write((uint8_t *)(&t), sizeof(t));
  mySerial.write((uint8_t *)(&u), sizeof(u));
  Serial.println(t);
  Serial.println(u);
 

}


**RX CODE**
// Qui spiega come convertire un float in uint8_t 
// https://forum.arduino.cc/t/converting-a-float-to-uint8_t-for-lora-radio-433mhz/599173

#include "Arduino.h"
#include "LoRa_E32.h"
#include <SoftwareSerial.h>

#define MAX_STR_LENGTH 20

typedef struct {
  char msg[MAX_STR_LENGTH] = {0};  // an array of char
  int valor = 0;
} Data_t;

Data_t Mydata;
 
SoftwareSerial mySerial(3, 2); // e32 TX e32 RX
float t, u;
 
void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);
 
  Serial.println("RX!");  
}
 
void loop() {
  float t;
  float u;
  if (mySerial.available()) {
    uint8_t buf1[4];
    uint8_t len1 = sizeof(buf1);
    mySerial.readBytes(buf1, len1);
    t = *((float *)buf1);

    uint8_t buf2[4];
    uint8_t len2 = sizeof(buf2);
    mySerial.readBytes(buf2, len2);
    u = *((float *)buf2);

    Serial.print("Temperatura: ");
    Serial.print(t);
    Serial.print(" C");
    Serial.print("\n");

    Serial.print("Umidità: ");
    Serial.print(u);
    Serial.print(" %");
    Serial.print("\n");
  }

/*
     static int i = 0;
   while (Serial.available() > 0) {
    char c = Serial.read();
    if (c != '\n') {
      MyData.msg[i] = c;
      i++;
    }
    else {
      // terminate the array with NULL terminator and send the struct
      MyData.msg[i] = '\0';
      MyData.valor = 10;
      Transceiver.SendStruct((uint8_t*) &MyData, (uint8_t) sizeof(MyData));
      Serial.print("Sending: "); 
      Serial.println(MyData.msg);
      Serial.println(MyData.valor);
      i = 0;
    }
  }
  */
  
}

What sort of a "lora module"? Post a link to the product page or data sheet.

A UART serial device may be expecting printable ASCII characters, in which case your approach will fail.

What has this to do with LoRa ?

Looks like your trying to send and receive data from some sort of UART or Serial device.

1 Like

You are not sending the pointer, you are sending the data that the pointer is referencing.

1 Like

You can't.
The pointer points to a memory address where the content you intend to send is, but that same content is not in the receiver, so the pointer is useless.

1 Like

Hi, It Is a LoRa E32 900T20D

Sorry could you please explain me this code:

mySerial.write((uint8_t *)(&t), sizeof(t));

How can work this type casting ?

&t Is t address
Then with uint8_t * i dereference casting in uint8_t ?

Thanks a lot

Hi, i write on a software serial object in order to comunicate with the receiver

&t is the address. (uint8_t *)(&t) is the address typecast to a pointer to uint8_t, not a de-reference. Two different things.

1 Like

So i write in the serial a pointer, so the receiver reads a uint8_t pointer?

No. You are passing the data pointer and the data length to the write() function. The write() function reads the bytes from the buffer you are pointing to and transmits them over the serial interface.

1 Like

Ok, i understand now that the write function sends the data on the serial, but why the receiver treats the data as pointer with these lines?:

mySerial.readBytes(buf1, len1);
t = *((float *)buf1)

A float is 4 bytes. The transmitter transmits 4 bytes so the receiver receives 4 bytes and puts them in a uint8_t buffer. In order to use the data as a float the receiver typecasts the uint8_t buffer address as a pointer to a float, dereferences it, and assigns it to a float variable.

1 Like

Ok thanks a lot now Is clear, good evening!

1 Like

I recommend to study the radio module user manual, as the code you propose is very unlikely to work unless the radios are properly configured.

The RX/TX lines are 3.3V only.

1 Like

Bear in mind, without memcpy or alignas , treating an array of bytes as a float is undefined behaviour.

1 Like

Thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.