This commit is contained in:
leo 2022-03-19 19:01:58 +01:00
parent 00caef344b
commit 0b88d17045
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
5 changed files with 75 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
build
sdkconfig.old

51
components/APlib/APlib.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <string.h>
#include <esp_wifi.h>
#include "esp_event.h"
#include "esp_log.h"
#include "APlib.h"
static const char *TAG = "wifi AP init";
static void wifi_event_handler(void* arg, esp_event_base_t event_base,int32_t event_id, void* event_data){
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", MAC2STR(event->mac), event->aid);
}
else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", MAC2STR(event->mac), event->aid);
}
}
void wifi_init_softap(wifi_config_t wifi_config){
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_ap();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
}
wifi_config_t wifi_config_generator(char SSID[32], char pass[32], uint8_t channel){
wifi_config_t config = {
.ap = {
.ssid_len = strlen((char*)SSID),
.channel = channel,
.max_connection = 10,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
},
};
memcpy(config.ap.ssid, SSID, 32);
memcpy(config.ap.password, pass, 32);
if(strlen((char*)pass) == 0)
config.ap.authmode = WIFI_AUTH_OPEN;
return config;
}

View File

@ -0,0 +1,2 @@
idf_component_register(SRCS "APlib.c"
INCLUDE_DIRS "include")

View File

@ -0,0 +1,2 @@
void wifi_init_softap(wifi_config_t wifi_config);
wifi_config_t wifi_config_generator(char SSID[32], char pass[32], uint8_t channel);

View File

@ -1,6 +1,25 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <stdio.h>
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include <esp_wifi.h>
#include "APlib.h"
void app_main(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
wifi_config_t wifi_config = wifi_config_generator("CO2Sense","testtest",1);
wifi_init_softap(wifi_config);
}