I want to send a struct from an arduino mega2560 to an esp8266. so far my code sometimes will send the valid bytes 1 time and the esp8266 will stop receiving any more messages. other times the data is invalid.
how can i successfully send the struct from the arduino to the esp?
Arduino send code,
#include <SoftwareSerial.h>
SoftwareSerial swSerial(30, 31); // RX, TX
unsigned long structTimerMillis = 0;
struct testStruct {
uint32_t var0 = 123;
float var1 = 12.34;
uint32_t var2 = 123321;
// String message = "It Worked";
};
testStruct ts1;
int structSize[(sizeof(ts1))];
void setup() {
Serial.begin(115200);
swSerial.begin(9600);
}
void loop() {
if (millis() - structTimerMillis >= 1000) {
Write();
structTimerMillis = millis();
}
}
void Write() {
Serial.println("write");
swSerial.write((byte*)&ts1, sizeof(structSize));
}
esp receive struct,
//#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
SoftwareSerial swSerial(13, 15); // RX, TX
unsigned long now = 0;
struct testStruct {
uint32_t var0 = 0;
float var1 = 0.0;
uint32_t var2 = 0;
// String message = "N/A";
};
testStruct ts1;
int structSize[(sizeof(ts1))];
void setup() {
Serial.begin(115200);
swSerial.begin(9600);
Serial.println("booting");
}
void loop() {
// put your main code here, to run repeatedly:
ReadData();
if(millis()-now>=1000){
Serial.println("running");
now = millis();
}
}
void ReadData()
{
byte *ptr = (byte*)&ts1;
if (swSerial.available() >= sizeof(structSize))
{
for (byte lp = 0; lp < sizeof (structSize); lp++) {
ptr[lp] = swSerial.read();
}
Serial.println("var0:");
Serial.println(ts1.var0);
Serial.println("var1:");
Serial.println(ts1.var1);
Serial.println("var2:");
Serial.println(ts1.var2);
}
}
Is there a method i could do this with memcpy?

