Code for sensor nodes connected by radio receiver with gateway
Dependencies: mbed WakeUp coapRadioClient DHT11
Diff: msg.cpp
- Revision:
- 0:4c0d384fd5cf
- Child:
- 3:4ffffdb5230f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/msg.cpp Thu Jan 24 16:40:14 2019 +0000 @@ -0,0 +1,25 @@ +#include "msg.h" + +#define UINT8_MAX 255; + +void Msg::construct_data_msg(uint8_t sensor_id, int data, uint8_t *buffer, int buffer_len) { + if (buffer_len < 5) + return; + + buffer[0] = sensor_id; + for (int i = 4; i > 0; i--) { + buffer[i] = data % UINT8_MAX; + data /= UINT8_MAX; + } +} + +void Msg::deconstruct_data_msg(uint8_t *sensor_id, int *data, uint8_t *buffer, int buffer_len) { + if (buffer_len < 5) + return; + + *sensor_id = buffer[0]; + *data = 0; + for (int i = 1; i < 5; i++) { + *data = *data * UINT8_MAX + buffer[i]; + } +}