This commit is contained in:
2022-11-25 16:38:32 +01:00
parent 9958803382
commit b39f967493
13 changed files with 3087 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
idf_component_register(
SRC_DIRS "."
INCLUDE_DIRS ".")

View File

@@ -0,0 +1,11 @@
menu "WIFI CONNECT"
config WIFI_SSID
string "SSID"
default "raspi-box-esp32"
config WIFI_PASSWORD
string "PASSWORD"
default "PolytechBoxRasp"
endmenu

View File

@@ -0,0 +1,2 @@
COMPONENT_SRCDIRS := .
COMPONENT_ADD_INCLUDEDIRS := .

View File

@@ -0,0 +1,82 @@
/****************************************************************************
* Copyright (C) 2021 by Fabrice Muller *
* *
* This file is useful for ESP32 Design course. *
* *
****************************************************************************/
/**
* @file main.c
* @author Fabrice Muller
* @date 12 Nov. 2021
* @brief File containing the lab1-1 of Part 7.
*
* @see https://github.com/fmuller-pns/esp32-vscode-project-template
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "wifi_connect.h"
static const char *TAG = "WIFI_LAB";
void ConnectedWifiTask (void *para);
/**
* @brief Starting point function
*
*/
void app_main() {
/* ERROR, WARNING, INFO level log */
esp_log_level_set(TAG, ESP_LOG_INFO);
/* Init WiFi */
wifiInit();
/* Create connected WiFi Task, STACK=3*1024, Priority=5 */
xTaskCreate(&ConnectedWifiTask, "WifiTask",3*1024, NULL, 5, NULL);
/* Delete task */
vTaskDelete(NULL);
}
void ConnectedWifiTask (void *para){
xSemaphoreHandle connectionWifiSem = getConnectionWifiSemaphore();
for(;;){
if (xSemaphoreTake(connectionWifiSem, pdMS_TO_TICKS(10000))){
ESP_LOGI(TAG,"Connected on %s", WIFI_SSID);
printf("Run Application\n");
xSemaphoreTake(connectionWifiSem,portMAX_DELAY);
ESP_LOGI(TAG,"Retried connexion on %s", WIFI_SSID);
}
else{
ESP_LOGE(TAG,"Failed to reconnect.Retry in");
for (int i=0; i<5;i++){
ESP_LOGE(TAG,"... %d",i);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
esp_restart();
}
}

View File

@@ -0,0 +1,118 @@
/****************************************************************************
* Copyright (C) 2021 by Fabrice Muller *
* *
* This file is useful for ESP32 Design course. *
* *
****************************************************************************/
/**
* @file wifi_connect.c
* @author Fabrice Muller
* @date 12 Nov. 2021
* @brief File containing the lab1-1 of Part 7.
*
* @see https://github.com/fmuller-pns/esp32-vscode-project-template
*/
#include "wifi_connect.h"
static const char *TAG = "WIFI_LAB";
/* Local communication */
static xSemaphoreHandle connectionWifiSemaphore;
/**
* @brief Get the Connected Wifi Semaphore
*
* @return xSemaphoreHandle connected semaphore
*/
xSemaphoreHandle getConnectionWifiSemaphore() {
return connectionWifiSemaphore;
}
static void wifi_event_handler_cb(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
switch (event_id) {
case SYSTEM_EVENT_STA_START:
/* Connect the ESP32 WiFi station to the Access Point (AP) Box */
esp_wifi_connect();
ESP_LOGI(TAG,"connecting...\n");
break;
case SYSTEM_EVENT_STA_CONNECTED:
ESP_LOGI(TAG, "Connected on %s\n", WIFI_SSID);
break;
case IP_EVENT_STA_GOT_IP:
ESP_LOGI(TAG,"got ip\n");
/* Connection with IP is established */
xSemaphoreGive(connectionWifiSemaphore);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG,"disconnected\n");
break;
default:
break;
}
}
/**
* @brief Init the WiFi
*
*/
void wifiInit() {
/* Initialize the default NVS (Non-Volatile Storage) partition */
ESP_ERROR_CHECK(nvs_flash_init());
/* Print MAC Address */
uint8_t macAddr[6];
esp_efuse_mac_get_default(macAddr);
printf("MAC Address : %02x-%02x-%02x-%02x-%02x-%02x\n", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
/* Create semaphore when the WiFi is connected */
connectionWifiSemaphore = xSemaphoreCreateBinary();
/* Initialize the underlying TCP/IP stack */
ESP_ERROR_CHECK(esp_netif_init());
/* Create default event loop */
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* User init default station (STATION mode) */
esp_netif_create_default_wifi_sta();
/* Init WiFi config with default parameters */
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
/* Register handler for events */
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT,ESP_EVENT_ANY_ID,wifi_event_handler_cb,NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT,IP_EVENT_STA_GOT_IP,wifi_event_handler_cb,NULL));
/* Store configuration in RAM (FLASH or RAM is possible) */
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
/* Init the SSID and PASSWORD */
wifi_config_t wifi_config =
{
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASSWORD,
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
}};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
/* Start Wi-Fi */
ESP_ERROR_CHECK(esp_wifi_start());
}

View File

@@ -0,0 +1,36 @@
/****************************************************************************
* Copyright (C) 2021 by Fabrice Muller *
* *
* This file is useful for ESP32 Design course. *
* *
****************************************************************************/
/**
* @file wifi_connect.h
* @author Fabrice Muller
* @date 12 Nov. 2021
* @brief File containing the lab1-1 of Part 7.
*
* @see https://github.com/fmuller-pns/esp32-vscode-project-template
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"
/* WIFI HOT SPOT (Access Point BOX) */
#define WIFI_SSID (CONFIG_WIFI_SSID)
#define WIFI_PASSWORD (CONFIG_WIFI_PASSWORD)
void wifiInit();
xSemaphoreHandle getConnectionWifiSemaphore();