diff --git a/main/main.c b/main/main.c index 385a85a..73ce932 100644 --- a/main/main.c +++ b/main/main.c @@ -13,6 +13,7 @@ #include "nvs_flash.h" #include "portmacro.h" #include +#include #include #include @@ -71,7 +72,6 @@ void espnow_data_prepare(espnow_send_param_t *send_param) { espnow_data_t *buf = (espnow_data_t *)send_param->buffer; ESP_LOGI(tag, "Example_Data_SIZE: %u, send_param_len: %d\n", sizeof(espnow_data_t), send_param->len); - assert(send_param->len >= sizeof(espnow_data_t)); buf->type = IS_BROADCAST_ADDR(send_param->dest_mac) @@ -81,10 +81,8 @@ void espnow_data_prepare(espnow_send_param_t *send_param) { buf->seq_num = s_espnow_seq[buf->type]++; buf->crc = 0; buf->magic = send_param->magic; - buf->payload.isMaster = isMaster; - /* Fill all remaining bytes after the data with random values */ - /* esp_fill_random(buf->payload, - send_param->len - sizeof(espnow_data_t)); */ // wieso sollte ich das mit random daten füllen + buf->unionPage = UNION_STATUS; + buf->realPayload.status.isMaster = isMaster; buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len); } @@ -161,7 +159,7 @@ int espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state, buf->crc = 0; crc_cal = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, data_len); - if(buf->payload.isMaster) { + if(buf->realPayload.status.isMaster) { ESP_LOGI(tag, "Recived Data from Master"); } else { ESP_LOGI(tag, "Recived Data from Slave"); diff --git a/main/main.h b/main/main.h index ec602b3..949fc5f 100644 --- a/main/main.h +++ b/main/main.h @@ -1,6 +1,7 @@ #ifndef MAIN_H #define MAIN_H +#include #define Master_SlavePin 23 #define MAX_PAYLOAD_SIZE 250 @@ -42,9 +43,23 @@ enum { EXAMPLE_ESPNOW_DATA_MAX, }; +enum { + UNION_STATUS, + UNION_SENSORDATA, +}; + typedef struct { bool isMaster; -} __attribute__((packed)) payloadData; +} __attribute__((packed)) payloadStatus; + +typedef struct { + uint8_t dataPoint; +} __attribute__((packed)) payloadSensorData; + +union realPayload { + payloadStatus status; + payloadSensorData sensorData; +}; /* User defined field of ESPNOW data in this example. */ typedef struct { @@ -53,12 +68,14 @@ typedef struct { uint16_t seq_num; // Sequence number of ESPNOW data. uint16_t crc; // CRC16 value of ESPNOW data. uint32_t magic; // Magic number which is used to determine which device to - // send unicast ESPNOW data. - payloadData payload; // Real payload of ESPNOW data. + // send unicast ESPNOW data. + uint8_t unionPage; + union realPayload realPayload; // Real payload of ESPNOW data. } __attribute__((packed)) espnow_data_t; -static_assert(sizeof(espnow_data_t) <= MAX_PAYLOAD_SIZE, "payloadData struct is too big to be sent in one part, keep it under 250 Bytes!"); - +static_assert(sizeof(espnow_data_t) <= MAX_PAYLOAD_SIZE, + "payloadData struct is too big to be sent in one part, keep it " + "under 250 Bytes!"); typedef struct { bool unicast; // Send unicast ESPNOW data.