SizeOf Byte different between Electron and a Mega

Hello,

I am having a weird issue. I am working on a project that use multiple NRF24L01 radios and during my trials I was able to get the system working well. So I moved on to the next step and that was to get the Electron into the mix and I loaded the code( had to adjust for the Particle library etc) and I got weird value. For example I would send a 1 from a arduino nano and get a 130209 on the Electron……… After banging my head against the wall for 3 days…… I notice that the calculated size of the byte I was sending was showing different between the arduino(two of them) and the Electron. So I built a simple program(no libraries ) to see if the problem would surface - it did. I search google but I can’t find anything - Any help is appreciated!!!

The Electron firmware is 0.6.2

From the Arduino Mega I get:
Size of MyData: 32

From the Particle:
Size of MyData: 60

here is the code - I put the same code in both(copy & paste)

struct MyData {
  int solar_voltage;
  int lipo_voltage;
  int cabin_1_humidity;
  int cabin_1_temp; 
  int cabin_2_humidity;
  int cabin_2_temp; 
  int cabin_3_humidity;
  int cabin_3_temp; 
  int cabin_4_humidity;
  int cabin_4_temp; 
  int cabin_5_humidity;
  int cabin_5_temp; 
  int date_rtc;
  int time_rtc; 
  unsigned long packetId;  
};

void setup() {
  // put your setup code here, to run once:
 Serial.begin(115200);
 delay(2000);
  int dataSize = sizeof(MyData);
  Serial.print("Size of MyData: ");
  Serial.println( dataSize);
}

void loop() {
  // put your main code here, to run repeatedly:

}

An int and an unsigned long are both 4 bytes on Particle devices (4 X 15 = 60). On a Mega, an int is only 2 bytes, and the unsigned long is still 4 bytes (2 x 14 + 4 = 32).

1 Like

oh my - I would never have figured that out! Thanks for your help. I will update my code tomorrow night and run the test again.

If you use uint16_t on both platforms you’ll get the same result.

3 Likes