Power_Broker:
Perhaps this?
memcpy(&st, data + 7, sizeof(data)- 7);
that did work. see at first i tried
memcpy(&st, data + 7, sizeof(data)- 7);
but i didnt get the results i entirely expected. I'm transferring a struct between two esp8266. I want to add some verification do i came up with this,
sending struct,
struct sampleStruct {
int var1 = 253;
//byte Array[20];
float var2 =5.5;
unsigned long var3 = 999;
unsigned long var4 =1;
unsigned long var5=9999;
unsigned long var6=7777;
unsigned long var7=6666;
unsigned long var8=5555;
// bool var9;
};
sampleStruct st;
static os_timer_t intervalTimer;
static void replyToServer(void* arg) {
AsyncClient* client = reinterpret_cast<AsyncClient*>(arg);
// send reply
if (client->space() > 32 && client->canSend()) {
client->add("VERIFY",7);
client->add((char *)&st, sizeof(sampleStruct));
client->send();
Serial.println("sent");
}
}
i added " client->add("VERIFY",7);" hoping it would add that to the beginning 7 bytes of data that i can check for on the receiving side and verify the first 7 bytes. however only the first variable in the struct on the receiving side was populated the the right value. I change it slightly to this,
memcpy(&st, data +7, sizeof(sampleStruct));
Now i receive the whole struct fine. I'm hoping since the structs are identifical data types that i should be okay use sizeof(sampleStruct) as the size reference?
receiving struct,
static void handleData(void* arg, AsyncClient* client, void *data, size_t len) {
Serial.printf("\n data received from client %s \n", client->remoteIP().toString().c_str());
// Serial.write((char *)data, len);
memcpy(&st, data +7, sizeof(sampleStruct));
Serial.write((const char *)data);
Serial.println("");
Serial.println(st.var1);
Serial.println(st.var2);
Serial.println(st.var3);
Serial.println(st.var4);
Serial.println(st.var5);
Serial.println(st.var6);
Serial.println(st.var7);
Serial.println(st.var8);
//Serial.println(st.var9);
}
the result in the serial console is,
data received from client 192.168.4.2
VERIFY
253
5.50
999
1
9999
7777
6666
5555