Compare commits

..

2 Commits

Author SHA1 Message Date
72851b69c5 Utility Makefile for Flashing and Monitoring 2024-10-04 12:51:46 +02:00
bf78ed3d6e Master and Slave can now send their State 2024-10-04 12:51:27 +02:00
3 changed files with 50 additions and 20 deletions

View File

@ -7,8 +7,16 @@ export:
buildIdf:
idf.py build
flash:
flash0:
sudo chmod o+rw /dev/ttyUSB0
idf.py flash -p /dev/ttyUSB0
monitor:
idf.py monitor
flash1:
sudo chmod o+rw /dev/ttyUSB1
idf.py flash -p /dev/ttyUSB1
monitor0:
idf.py monitor -p /dev/ttyUSB0
monitor1:
idf.py monitor -p /dev/ttyUSB1

View File

@ -26,7 +26,7 @@
#define CONFIG_ESPNOW_PMK "pmk1234567890123"
#define CONFIG_ESPNOW_SEND_COUNT 100
#define CONFIG_ESPNOW_SEND_DELAY 1000
#define CONFIG_ESPNOW_SEND_LEN 10
#define CONFIG_ESPNOW_SEND_LEN 250
#define CONFIG_ESPNOW_LMK "lmk1234567890123"
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) {
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));
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->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(example_espnow_data_t));
/* esp_fill_random(buf->payload,
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);
}
@ -157,6 +161,12 @@ int example_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) {
ESP_LOGE(tag, "Recived Data from Master");
} else {
ESP_LOGE(tag, "Recived Data from Slave");
}
if (crc_cal == crc) {
return buf->type;
}
@ -379,6 +389,7 @@ static esp_err_t example_espnow_init(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_set_direction(Master_SlavePin, GPIO_MODE_INPUT);
int checkMaster = gpio_get_level(Master_SlavePin);

View File

@ -2,9 +2,11 @@
#define MAIN_H
#define Master_SlavePin 23
#define MAX_PAYLOAD_SIZE 250
#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;
@ -35,18 +37,27 @@ typedef struct {
} example_espnow_event_t;
enum {
EXAMPLE_ESPNOW_DATA_BROADCAST,
EXAMPLE_ESPNOW_DATA_UNICAST,
EXAMPLE_ESPNOW_DATA_MAX,
EXAMPLE_ESPNOW_DATA_BROADCAST,
EXAMPLE_ESPNOW_DATA_UNICAST,
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. */
typedef struct {
uint8_t type; //Broadcast or unicast ESPNOW data.
uint8_t state; //Indicate that if has received broadcast ESPNOW data or not.
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.
uint8_t payload[0]; //Real payload of ESPNOW data.
uint8_t type; // Broadcast or unicast ESPNOW data.
uint8_t state; // Indicate that if has received broadcast ESPNOW data or not.
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.
} __attribute__((packed)) example_espnow_data_t;
typedef struct {
@ -55,10 +66,10 @@ typedef struct {
uint8_t state; // Indicate that if has received broadcast ESPNOW data or not.
uint32_t magic; // Magic number which is used to determine which device to
// send unicast ESPNOW data.
uint16_t count; // Total count of unicast ESPNOW data to be sent.
uint16_t delay; // Delay between sending two ESPNOW data, unit: ms.
int len; // Length of ESPNOW data to be sent, unit: byte.
uint8_t *buffer; // Buffer pointing to ESPNOW data.
uint16_t count; // Total count of unicast ESPNOW data to be sent.
uint16_t delay; // Delay between sending two ESPNOW data, unit: ms.
int len; // Length of ESPNOW data to be sent, unit: byte.
uint8_t *buffer; // Buffer pointing to ESPNOW data.
uint8_t dest_mac[ESP_NOW_ETH_ALEN]; // MAC address of destination device.
} example_espnow_send_param_t;