strlen, who about structlen?

Messing around with the VirtualWire library.
http://www.open.com.au/mikem/arduino/

Trying to find the size of a struct, so I can send it. Code looks Something like this.

#include <VirtualWire.h>

struct data 
{
  int a;
  int b;
  int c;
};

data *weather = { 0, 1, 2 };

void setup()
{
    vw_setup(2000); // Bits per sec
}

void loop()
{
  vw_send((uint8_t *)weather, strlen(weather));   //Ya, I know that does not work.  What does?
}

Is the size as simple as 16+16+16=48 bits? (6 bytes) Does the struct take up any room?

It's as simple as sizeof(data). Never try to do the size math yourself. Always trust the compiler's sizeof() operator. In simple cases like yours, you'll probably find that it is six bytes, as you guessed, but there are a lot of reasons that more complicated structures might not be quite the size you'd imagine.

In your case, you're making a pointer to a struct data. You can also say sizeof(*weather) to figure out the size of the thing that your pointer points to. (However, I'm wondering why you're making it a pointer at all, when you only make it point at a single static entry like this.)

If I remember my C correctly (read: I am probably wrong), then yes, adding up the variable sizes within the struct will give you the total size, once a variable is declared as the struct type. I don't think it takes up any extra room beyond that...

cr0sh,

Well, you sorta remember your C correctly. The compiler can add as much extra room as it wants to. It depends on the implementation, not on the definition of the language.

I'd be very surprised if the Arduino implementation ever adds any extra space, but when compiling for processors with 16 or 32-bit words, it's quite common to have extra bytes in a structure to make an integer, for instance, start on a word boundary.

Regards,

-Mike

I think I am off the deep end. I am trying to play weather station. I have 3 arduino placed around my house reporting temperature. 1 playing server, going to my computer for logging. But I dont have a clue how to pass the data! Here is what I have thus far, it does not work.

client side code

//Client
#include <VirtualWire.h>

struct DATA 
{
  int id;
  //most values are biased(B) by 100 (t*100)
  int t;  //temp, B
  int b;  //batt, B
  int h;  //Humidity, B 
  int a;  //activity
  int w;  //weight, B 
};

DATA weather = { 101, 0, 0, 0, 0, 0 };

void setup()
{
    vw_setup(2000); // Bits per sec
}

void loop()
{
  vw_send((uint8_t *)&weather, sizeof(weather)); 
  delay(1000);
}

server side code

//server
#include <VirtualWire.h>

struct DATA 
{
  int id;
  //most values are biased(B) by 100 (t*100)
  int t;  //temp, B
  int b;  //batt, B
  int h;  //Humidity, B 
  int a;  //activity
  int w;  //weight, B 
};

DATA weather = { 0, 0, 0, 0, 0, 0 };
void setup()
{
    Serial.begin(9600);
    Serial.println("setup");
    vw_setup(2000); // Bits per sec
    vw_rx_start();    // Start the receiver PLL running
}

void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    //DATA buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
      int i;
      // Message with a good checksum received, dump HEX
      Serial.println("Got: ");
      Serial.println((DATA)buf.id, BYTE);
      Serial.println((DATA)buf.t, BYTE);
      Serial.println((DATA)buf.b, BYTE);
      Serial.println((DATA)buf.h, BYTE);
      Serial.println((DATA)buf.a, BYTE);
      Serial.println((DATA)buf.w, BYTE);
      Serial.println("");
   }
}

In your client and your server, you need to add 'struct' in front of the DATA declaration, like this:struct DATA weather = { 101, 0, 0, 0, 0, 0 };
When you receive the structure in your server, you need to receive it back into a structure, like this:if (vw_get_message((uint8_t *)&weather,&buflen)) // Non-blockingYou don't need 'buf' at all.

When you print out the result, print the elements of the weather struct: weather.t, weather.b, weather.h, etc.

Regards,

-Mike

THANKS Mike! Its all running great.