Author Topic: using struct for RF data packet  (Read 2257 times)

d00m178

  • Jr. Member
  • **
  • Posts: 82
using struct for RF data packet
« on: October 15, 2019, 12:24:19 PM »
hello all.

Im using moteinos with RFM95 for sending data from different types of sensors - termo, laser, etc.

several "servers" devices send this data to one "client" (or "receiver") device.

currently in server code I use for data array of float:

Code: [Select]
float data_buf[23];

//[0]-[14]: ext thermosensors,

//[15]: int sensor,
//[16]: batV,
//[17]: RSSI,
//[18]: Vcc,
//[19]: light,
//[20]: snow_depth,
//[21]: LRF temp,
//[22]: LRF Vcc

on client side I use the same array and print received data like:

Code: [Select]
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];

uint8_t len = sizeof(buf);
uint8_t from;
   
if (manager.recvfromAckTimeout(buf, &len, 5000, &from)) {


Serial.print(F("T0:"));Serial.println((*(float *)&buf[0]));    // buf[0]
Serial.print(F("T1:"));Serial.println((*(float *)&buf[4]));    // buf[1]
..............................
etc


Some servers has different number of sensors.

for example - server1 has 5 termosensors, and server2 - has 10 termosensors.
I want implement in code some data packet which will be used on all servers and client side.

something like:

server1 - has 5 termosensors:
Code: [Select]
typedef struct {
  float         temp1;
  float         temp2;
  float         temp3;
  float         temp4;
  float         temp5;
} Payload;
Payload srv_data;

server2 - has 10 termosensors:
Code: [Select]
typedef struct {
  float         temp1;
  float         temp2;
  float         temp3;
  float         temp4;
  float         temp5;
.............................
  float         temp10;
} Payload;
Payload srv_data;

And on client side I need have a code that will receive data from server1 and server2
is it possible in c++?
what is the best practices for such task?
btw - can I use array in struct?
like:
Code: [Select]
typedef struct {
  float         temp_sensors[10];

..........



Any advice and examples would be very helpful.
Thank you.


Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: using struct for RF data packet
« Reply #1 on: October 15, 2019, 01:04:26 PM »
Just add a first field which denotes the struct type, or version of struct. Then you know how to parse it.

d00m178

  • Jr. Member
  • **
  • Posts: 82
Re: using struct for RF data packet
« Reply #2 on: October 15, 2019, 01:13:35 PM »
Just add a first field which denotes the struct type, or version of struct. Then you know how to parse it.

and on client side just use IF - THEN to handle "type" of received struct?
so if I have 10 types of sensors sets on different servers - I need 10 IF-THEN in client code.

also - if I have new type of server struct, for example, with 25 sensors - I need recompile client side to support new version..
no another way to handle this?
maybe I don't need struct at all.
probably I can convert all data to char/string and client just receive prepared text..
what is the best practice for send-receive payload from different sensors?

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: using struct for RF data packet
« Reply #3 on: October 15, 2019, 02:11:42 PM »
I would probably use a switch statement instead.
There are many ways to do this type of thing.
If you don't want to use structs, then use a simple ASCII format like "sensor:value".