commit fe01b9baf98c1cafd1cad29c00a38a5199dbd56c Author: simon Date: Fri Sep 13 10:40:32 2024 +0200 Non Working Inital Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f77d569 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build/ +sdkconfig +sdkconfig.old +.cache +.devcontainer/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9b1da3a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +# For more information about build system see +# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(espNowTest) \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b9194a7 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +all: + echo "no default rule" + +export: + source ~/esp/esp-idf/export.fish && fish + +buildIdf: + idf.py build + +flash: + idf.py flash -p /dev/ttyUSB0 + +monitor: + idf.py monitor diff --git a/README.md b/README.md new file mode 100644 index 0000000..455eb90 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# _Sample project_ + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This is the simplest buildable example. The example is used by command `idf.py create-project` +that copies the project to user specified path and set it's name. For more information follow the [docs page](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html#start-a-new-project) + + + +## How to use example +We encourage the users to use the example as a template for the new projects. +A recommended way is to follow the instructions on a [docs page](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html#start-a-new-project). + +## Example folder contents + +The project **sample_project** contains one source file in C language [main.c](main/main.c). The file is located in folder [main](main). + +ESP-IDF projects are built using CMake. The project build configuration is contained in `CMakeLists.txt` +files that provide set of directives and instructions describing the project's source files and targets +(executable, library, or both). + +Below is short explanation of remaining files in the project folder. + +``` +├── CMakeLists.txt +├── main +│   ├── CMakeLists.txt +│   └── main.c +└── README.md This is the file you are currently reading +``` +Additionally, the sample project contains Makefile and component.mk files, used for the legacy Make based build system. +They are not used or needed when building with CMake and idf.py. diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..8c1ea88 --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "main.c" + INCLUDE_DIRS "." + ) \ No newline at end of file diff --git a/main/main.c b/main/main.c new file mode 100644 index 0000000..f9ed180 --- /dev/null +++ b/main/main.c @@ -0,0 +1,128 @@ +#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)); + } +}