Hello all,
I want to send data from two sender esp32 boards via esp now and it will be received by one receiver board. Both sender send data from two different types of sensors (strain gauges and thermo couple) and the number of sensors varies as as well. For example: Sender 1: 3 strain gauges and 4 thermo couples Sender 2: 2 Strain gauges and 2 thermo couples.
The amount of sensors will vary from task to task. The data from each sensor type is being stored in an array and sent to the receiver. This works well but only if the amount of sensors match for Sender 1 and 2.
My problem is the following. The sender structure has to match the receiver structure.
#define NUMBER_OF_SENSORSSG 2
#define NUMBER_OF_SENSORSTC 0
#define NUMBER_OF_SENSORS 2
// Define data structure
typedef struct struct_message {
int id = 1; // must be unique for each sender board
int NUMBER_OF_SENSORSSG=2;
int32_t* strainNow;
float* ThermoNow;
} struct_message;
// Create structured data object
struct_message myData;
I declare the length of the arrays by defining macros at the beginning.
struct_message myData;
// Create a structure to hold the readings from each board
struct_message board1;
struct_message board2;
struct_message board3;
// Create an array with all the structures
struct_message boardsStruct[3] = {board1, board2, board3};
void loop(){
myData.strainNow = (int32_t*)malloc(myData.NUMBER_OF_SENSORSSG * sizeof(int32_t));
for (byte x = 0; x < NUMBER_OF_SENSORSSG; x++) {
myMux.setPort(x);
while (!nau[x]->available()) {
delay(1);
}
load[x] = nau[x]->read();
myData.strainNow[x]=load[x];
Serial.print("strain value "); Serial.print(x);Serial.print(": ");Serial.print(load[x]); Serial.print("\t");
}
Serial.println();
// Send message via ESP-NOW
esp_err_t resultTC = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (resultTC == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
I used the callback function OnDataRecv very similar to the tutorial from random nerds tutorial. The receiver structure is obviously the same as the sender.
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
int id;
int NUMBER_OF_SENSORSSG;
int32_t* strainNow;
float* ThermoNow;
}struct_message;
// Create a struct_message called myData
struct_message myData;
// Create a structure to hold the readings from each board
struct_message board1;
struct_message board2;
struct_message board3;
// Create an array with all the structures
struct_message boardsStruct[3] = {board1, board2, board3};
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
char macStr[18];
Serial.print("Packet received from: ");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.println(macStr);
myData.strainNow = (int32_t*)malloc(myData.NUMBER_OF_SENSORSSG * sizeof(int32_t));
memcpy(myData.strainNow, incomingData + sizeof(myData), myData.NUMBER_OF_SENSORSSG * sizeof(int32_t));// Update the corresponding board's data in boardsStruct
boardsStruct[myData.id - 1] = myData;
Can someone help me out on how to send the information of the length of each array with the package being sent like with a pointer *strainNow or anything like that.
I tried the command malloc Sender:
typedef struct struct_message {
int id; // must be unique for each sender board
int32_t strain;
int NUMBER_OF_SENSORSS = 2;
int NUMBER_OF_SENSORST = 0;
int32_t* strainNow; // Assuming a maximum size for demonstration
float* ThermoNow; // Adjust size as needed
} struct_message;
myData.strainNow = (int32_t*)malloc(myData.NUMBER_OF_SENSORSSG * sizeof(int32_t));
typedef struct struct_message {
int id;
int32_t strain;
int NUMBER_OF_SENSORSSG;
int NUMBER_OF_SENSORSTC;
int32_t* strainNow;
float* ThermoNow;
} struct_message;
struct_message board1;
struct_message board2;
struct_message board3;
struct_message boardsStruct[3] = {board1, board2, board3};
struct_message myData;
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
myData.strainNow = (int32_t*)malloc(myData.NUMBER_OF_SENSORSSG * sizeof(int32_t));
memcpy(myData.strainNow, incomingData + sizeof(myData), myData.NUMBER_OF_SENSORSS G* sizeof(int32_t));
I was able to send it but on the receiving side it got the array size right but not the actual data. Thanks for any suggestions.