This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Mesh sensor Model, data type

I'm setting up the message structure for usage in a sensor Model. The sensor readings may be represented by different data types (e.g. float, integer). How is this best handled in the Model? I've been thinking about these strategies:

  1. Use a data type big enough to contain all others, and cast them at the client based on sensor ID. This really doesn't feel like a good solution at all.
  2. Create separate messages (or even Models) for the different data types. This would probably work, but involves some code repetition.
  3. A C union. But I'm not sure how this would work in a Model. And would this incur an overhead when any of the smaller data types are used?
Parents
  • Hi Meshs,

    All data sent over the radio is converted to a byte array. It's doesn't mater if it's float, integer or any type of data. My suggestion is to use only one type of message but use different opcode for each type of data. For example currently in on off model we have SIMPLE_ON_OFF_OPCODE_SET, SIMPLE_ON_OFF_OPCODE_GET. You can define SIMPLE_ON_OFF_OPCODE_GET_ADC, SIMPLE_ON_OFF_OPCODE_GET_TEMP to get each type of data.

    Then on the receiver side (client) you base on the opcode to convert the data back to the original type.

  • Thank you for the answer, Hung. Would a Union then be an appropriate method for declaring the value member in the message structure? Something like:

    struct message_sensor_reading_t {
        uint32_t sensor_id;
        uint32_t timestamp;
        union Value {
            uint32_t i;
            uint8_t c;
            float f;
        };
    };
    

    And then, as you suggested, access the union correctly based on the opcode.

Reply Children
No Data
Related