I have a controller (UNO) that pulls data via Modbus, and then publishes these as MQTT messages.
Instead of sending 100 values every 10 seconds, I want to send only the changed values.
I didn't want to use send 100 variables, and do a 'if changed send' condition, but thought of using a struct, and send only the changed values.
The idea is to store the previous value, a factor, whether the value was sent, and whether it is signed or not.
Is this the right setup?
struct modbus_data
{
// register_address = array index + 8000
bool b_publish_or_not;
bool b_signed_or_not;
uint8_t factor; // 0 = none, 1 = 0.1, 2 = 10
uint16_t previous_value;
};
struct modbus_data arr_modbus_data[NUM_MODBUS_ARR_ITEMS] =
{
// publish, signed_or_not, factor, previous_value
{1,1,1,0}, {1,1,1,0}, {1,1,2,0}, {1,1,1,0}, {1,1,1,0},
{1,1,2,0}, {1,1,1,0}, {1,1,2,0}, {1,0,0,0}, {1,1,1,0},
{1,1,1,0}, {1,1,1,0}, {1,0,0,0}, {1,1,2,0}, {1,1,2,0},
{1,1,2,0}, {1,1,2,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,1,2,0}, {1,0,0,0}, {1,1,2,0}, {1,1,2,0}, {1,1,1,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,1,2,0},
{1,1,2,0}, {1,1,2,0}, {1,1,2,0}, {1,0,1,0}, {1,1,2,0},
{1,1,2,0}, {1,1,2,0}, {1,1,2,0}, {1,1,2,0}, {1,1,1,0},
{1,1,1,0}, {1,0,1,0}, {1,0,1,0}, {1,0,1,0}, {1,0,1,0},
{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0},
{1,0,0,0}, {1,0,2,0}
};
And is this the right approach to update the values?
// get sign: value signed = 1, unsigned = 0
bool b_signed_or_not = arr_modbus_data[array_id + i].b_signed_or_not;
// get the stored value
uint16_t stored_value = arr_modbus_data[array_id + i].previous_value;
I haven't worked with these structs at this complexity before.
- Is this the wya to do it?
- Is there a better way to do this?
- Would a class be a better approach (getters/setters)?
Any hints appreciated.