Master and Slave can now send their State

This commit is contained in:
simon 2024-10-04 12:51:27 +02:00
parent 19b099270a
commit bf78ed3d6e
2 changed files with 39 additions and 17 deletions

View File

@ -26,7 +26,7 @@
#define CONFIG_ESPNOW_PMK "pmk1234567890123" #define CONFIG_ESPNOW_PMK "pmk1234567890123"
#define CONFIG_ESPNOW_SEND_COUNT 100 #define CONFIG_ESPNOW_SEND_COUNT 100
#define CONFIG_ESPNOW_SEND_DELAY 1000 #define CONFIG_ESPNOW_SEND_DELAY 1000
#define CONFIG_ESPNOW_SEND_LEN 10 #define CONFIG_ESPNOW_SEND_LEN 250
#define CONFIG_ESPNOW_LMK "lmk1234567890123" #define CONFIG_ESPNOW_LMK "lmk1234567890123"
const char *tag = "Exam"; const char *tag = "Exam";
@ -70,6 +70,8 @@ static void example_espnow_deinit(example_espnow_send_param_t *send_param) {
void example_espnow_data_prepare(example_espnow_send_param_t *send_param) { void example_espnow_data_prepare(example_espnow_send_param_t *send_param) {
example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer; example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer;
ESP_LOGI(tag, "Example_Data_SIZE: %u, send_param_len: %d\n", sizeof(example_espnow_data_t), send_param->len);
assert(send_param->len >= sizeof(example_espnow_data_t)); assert(send_param->len >= sizeof(example_espnow_data_t));
buf->type = IS_BROADCAST_ADDR(send_param->dest_mac) buf->type = IS_BROADCAST_ADDR(send_param->dest_mac)
@ -79,9 +81,11 @@ void example_espnow_data_prepare(example_espnow_send_param_t *send_param) {
buf->seq_num = s_example_espnow_seq[buf->type]++; buf->seq_num = s_example_espnow_seq[buf->type]++;
buf->crc = 0; buf->crc = 0;
buf->magic = send_param->magic; buf->magic = send_param->magic;
buf->payload.isMaster = isMaster;
/* Fill all remaining bytes after the data with random values */ /* Fill all remaining bytes after the data with random values */
esp_fill_random(buf->payload, /* esp_fill_random(buf->payload,
send_param->len - sizeof(example_espnow_data_t)); send_param->len - sizeof(example_espnow_data_t)); */ // wieso sollte ich das mit random daten füllen
buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len); buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len);
} }
@ -157,6 +161,12 @@ int example_espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state,
buf->crc = 0; buf->crc = 0;
crc_cal = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, data_len); crc_cal = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, data_len);
if(buf->payload.isMaster) {
ESP_LOGE(tag, "Recived Data from Master");
} else {
ESP_LOGE(tag, "Recived Data from Slave");
}
if (crc_cal == crc) { if (crc_cal == crc) {
return buf->type; return buf->type;
} }
@ -379,6 +389,7 @@ static esp_err_t example_espnow_init(void) {
} }
void app_main(void) { void app_main(void) {
// Master Slave Detection, default pin is pull up so ground it and check state
gpio_reset_pin(Master_SlavePin); gpio_reset_pin(Master_SlavePin);
gpio_set_direction(Master_SlavePin, GPIO_MODE_INPUT); gpio_set_direction(Master_SlavePin, GPIO_MODE_INPUT);
int checkMaster = gpio_get_level(Master_SlavePin); int checkMaster = gpio_get_level(Master_SlavePin);

View File

@ -2,9 +2,11 @@
#define MAIN_H #define MAIN_H
#define Master_SlavePin 23 #define Master_SlavePin 23
#define MAX_PAYLOAD_SIZE 250
#define ESPNOW_QUEUE_SIZE 6 #define ESPNOW_QUEUE_SIZE 6
#define IS_BROADCAST_ADDR(addr) (memcmp(addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN) == 0) #define IS_BROADCAST_ADDR(addr) \
(memcmp(addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN) == 0)
static bool isMaster; static bool isMaster;
@ -39,14 +41,23 @@ enum {
EXAMPLE_ESPNOW_DATA_UNICAST, EXAMPLE_ESPNOW_DATA_UNICAST,
EXAMPLE_ESPNOW_DATA_MAX, EXAMPLE_ESPNOW_DATA_MAX,
}; };
typedef struct {
bool isMaster;
} __attribute__((packed)) payloadData;
static_assert(sizeof(payloadData) <= MAX_PAYLOAD_SIZE, "payloadData struct is too big to be sent in one part, keep it under 250 Bytes!");
/* User defined field of ESPNOW data in this example. */ /* User defined field of ESPNOW data in this example. */
typedef struct { typedef struct {
uint8_t type; // Broadcast or unicast ESPNOW data. uint8_t type; // Broadcast or unicast ESPNOW data.
uint8_t state; // Indicate that if has received broadcast ESPNOW data or not. uint8_t state; // Indicate that if has received broadcast ESPNOW data or not.
uint16_t seq_num; // Sequence number of ESPNOW data. uint16_t seq_num; // Sequence number of ESPNOW data.
uint16_t crc; // CRC16 value 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. uint32_t magic; // Magic number which is used to determine which device to
uint8_t payload[0]; //Real payload of ESPNOW data. // send unicast ESPNOW data.
payloadData payload; // Real payload of ESPNOW data.
} __attribute__((packed)) example_espnow_data_t; } __attribute__((packed)) example_espnow_data_t;
typedef struct { typedef struct {