How do I send floating data from Lora node devices to Chirpstack?



To send float data from LoRa node devices to ChirpStack, you'll need to perform some data manipulation. When sending data from the end node, you typically send an array of bytes. However, if you want to send float data, you'll need to convert it and split it into separate bytes.

Let's take an example where we want to send the float value 10.12.

First, multiply the float by 100 to convert it to an integer:


float value = 10.12
value = value * 100; // 1012

Next, split the integer value into high and low bytes and store them in separate variables:


byte payload[2]; 
payload[0] = highByte(value); 
payload[1] = lowByte(value);

For the value 1012, the binary representation is 1111110100.

Considering a 16-bit representation, it would be 0000001111110100.

The high byte is 00000011, and the low byte is 11110100.


Send the payload data over the LoRa network. When decoding the data using a codec, you'll receive a bytes array. To retrieve the original data, perform the following steps:


byte data_array[] = {0b00000011, 0b11110100}; 
// Get the 16-bit representation of the data 
uint16_t original_data = (data_array[0] << 8) | data_array[1]; 
// Convert the 16-bit data back to the float representation 
float original_float = original_data / 100.0;

By left-shifting data_array[0] by 8 bits and performing a logical OR operation with data_array[1], you obtain the 16-bit data representation.

data_array[0] => 00000011 data_array[0]<<1 => 000000110 data_array[0]<<2 => 0000001100 data_array[0]<<3 => 00000011000 data_array[0]<<4 => 000000110000 data_array[0]<<5 => 0000001100000 data_array[0]<<6 => 00000011000000 data_array[0]<<7 => 000000110000000 data_array[0]<<8 => 0000001100000000

data_array[0]<<8 + data_array[1] => 000001100000000 + 11110100 => 0000001111110100

0000001111110100 to decimal gives 1012

Then, divide the 16-bit data by 100 to retrieve the original float value of 10.12.


Comments

Popular Posts