#include "driver/gpio.h" #include "esp_crc.h" #include "esp_event.h" #include "esp_log.h" #include "esp_mac.h" #include "esp_netif.h" #include "esp_now.h" #include "esp_random.h" #include "esp_wifi.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "nvs_flash.h" #include #define ESPNOW_WIFI_MODE WIFI_MODE_AP #define ESPNOW_WIFI_IF ESP_IF_WIFI_AP #define CONFIG_ESPNOW_CHANNEL 1 const char *tag = "Exam"; void wifi_init(void) { esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { // NVS partition was truncated and needs to be erased ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); ESP_ERROR_CHECK(esp_wifi_set_mode(ESPNOW_WIFI_MODE)); ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK( esp_wifi_set_channel(CONFIG_ESPNOW_CHANNEL, WIFI_SECOND_CHAN_NONE)); ESP_ERROR_CHECK(esp_wifi_set_protocol( ESPNOW_WIFI_IF, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR)); } static esp_err_t example_espnow_init(void) { example_espnow_send_param_t *send_param; s_example_espnow_queue = xQueueCreate(ESPNOW_QUEUE_SIZE, sizeof(example_espnow_event_t)); if (s_example_espnow_queue == NULL) { ESP_LOGE(TAG, "Create mutex fail"); return ESP_FAIL; } /* Initialize ESPNOW and register sending and receiving callback function. */ ESP_ERROR_CHECK( esp_now_init() ); ESP_ERROR_CHECK( esp_now_register_send_cb(example_espnow_send_cb) ); ESP_ERROR_CHECK( esp_now_register_recv_cb(example_espnow_recv_cb) ); #if CONFIG_ESPNOW_ENABLE_POWER_SAVE ESP_ERROR_CHECK( esp_now_set_wake_window(CONFIG_ESPNOW_WAKE_WINDOW) ); ESP_ERROR_CHECK( esp_wifi_connectionless_module_set_wake_interval(CONFIG_ESPNOW_WAKE_INTERVAL) ); #endif /* Set primary master key. */ ESP_ERROR_CHECK( esp_now_set_pmk((uint8_t *)CONFIG_ESPNOW_PMK) ); /* Add broadcast peer information to peer list. */ esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t)); if (peer == NULL) { ESP_LOGE(TAG, "Malloc peer information fail"); vSemaphoreDelete(s_example_espnow_queue); esp_now_deinit(); return ESP_FAIL; } memset(peer, 0, sizeof(esp_now_peer_info_t)); peer->channel = CONFIG_ESPNOW_CHANNEL; peer->ifidx = ESPNOW_WIFI_IF; peer->encrypt = false; memcpy(peer->peer_addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN); ESP_ERROR_CHECK( esp_now_add_peer(peer) ); free(peer); /* Initialize sending parameters. */ send_param = malloc(sizeof(example_espnow_send_param_t)); if (send_param == NULL) { ESP_LOGE(TAG, "Malloc send parameter fail"); vSemaphoreDelete(s_example_espnow_queue); esp_now_deinit(); return ESP_FAIL; } memset(send_param, 0, sizeof(example_espnow_send_param_t)); send_param->unicast = false; send_param->broadcast = true; send_param->state = 0; send_param->magic = esp_random(); send_param->count = CONFIG_ESPNOW_SEND_COUNT; send_param->delay = CONFIG_ESPNOW_SEND_DELAY; send_param->len = CONFIG_ESPNOW_SEND_LEN; send_param->buffer = malloc(CONFIG_ESPNOW_SEND_LEN); if (send_param->buffer == NULL) { ESP_LOGE(TAG, "Malloc send buffer fail"); free(send_param); vSemaphoreDelete(s_example_espnow_queue); esp_now_deinit(); return ESP_FAIL; } memcpy(send_param->dest_mac, s_example_broadcast_mac, ESP_NOW_ETH_ALEN); example_espnow_data_prepare(send_param); xTaskCreate(example_espnow_task, "example_espnow_task", 2048, send_param, 4, NULL); return ESP_OK; } void app_main(void) { gpio_reset_pin(2); gpio_set_direction(2, GPIO_MODE_OUTPUT); wifi_init(); while (1) { ESP_LOGI(tag, "LED AN"); gpio_set_level(2, 1); vTaskDelay(pdMS_TO_TICKS(1000)); ESP_LOGI(tag, "LED AUS"); gpio_set_level(2, 0); vTaskDelay(pdMS_TO_TICKS(1000)); } }