Arduino saving struct definition onto SD

So I am trying to create a dynamic network of arduino nano's with different sensors communicating back to a base station. I will call these 'nodes'. Each node will have a microSD card for local storage that can be requested by the base station. I want to be able to have the base station request the 'sensor struct' and the node would pull the first line of the 'sensor.txt' file that I designate as the 'struct definition' and then request for 'sensor data'.

// Structure of our message
/*struct message_1 {
  float temperature;
  float humidity;
  byte light;
  bool motion;
  bool dooropen;
};
message_1 message;*/

All other lines would be the data saved in the designated struct format.

void write_struct_2_SD(file,data){
  
  file.write((uint8_t *)data, sizeof(data));
  
}

My question: How can I save the structure of a struct onto an SD and be able to pull it and have the base station use it as the struct to read the data?

Then, is there a way then to have the base station pull the individual variables [temperature,humidity,light,motion,dooropen] in order to put it into a database?

In python I used a combination of the dill and pickle modules in order to save a dictionary in binary and pull it back out. Is there a way to do that with struct in c/c++ ?

You wrote "My question: How can I save the structure of a struct onto an SD and be able to pull it and have the base station use it as the struct to read the data? ".

Can you please give one or two examples of what you are asking?

Paul

You can’t really do that. The structure has to be compiled. This isn’t an interpreted language, it’s a compiled language.

There are ways to accomplish what you want, but you cannot define variables at run-time from an SD. You can give existing variables values, but you can’t create new ones like that.

Paul_KD7HB:
You wrote "My question: How can I save the structure of a struct onto an SD and be able to pull it and have the base station use it as the struct to read the data? ".

Can you please give one or two examples of what you are asking?

Paul

Ok. So, my base station will be a raspberry pi 4 and I will be using python on that.

I want to mock up several 'arduino shields' with different sensors and switches on them. Say for instance, I create a new 'sensor node' that I just turned on and it's got an RF24L01 module with it's own address and it starts talking to the base station for the first time. In python I have nested definitions for each 'active node' that it sees. One of the definitions for each node is its sensors. When the base station requests for what kind of sensors the node has, I was thinking that the node could send the structure of its 'sensor struct' to the base station and the base station could fill in the nested definition and then create a database table for that node with all its sensor names that it got from the 'sensor struct'. That way the database even has the type of the variable. From that point on, the 'sensor data' messages the base station gets from that node address gets cataloged into its respective database table.

I guess I am being a bit naive. I thought since I was able to create this in python:

import pickle
import dill

def read_dict(dir,dict_name):
'''reads the previously saved dictionary '''
with open(dir+str(dict_name)+'.txt'.'rb') as f:
d=dill.loads(dill.dumps(dictionary))
dill.dump(d,f)

def write_dict(dir,dict_name,dictionary):
'''saves the created dictionary '''
with open(dir+str(dict_name)+'.txt','wb') as f:
d = dill.loads(dill.dumps(dictionary))
dill.dump(d,f)

I thought I could create the equivalent with struct. Is there no function in c/c++ that allows one to save an object and then replace a variable with that saved object? Or should I be looking at the problem differently?

Have you considered using JSON for this?

I have not.. I did not really know that's an option with arduino's. I really don't want to make this code heavy.

You don’t need to add JSON libraries, you can just create the object you need in a string buffer and save it to the SD and/or send it to to the Pi.

wildbill:
You don't need to add JSON libraries, you can just create the object you need in a string buffer and save it to the SD and/or send it to to the Pi.

How would I turn the object that I gave in my first post into a string buffer?..for example