123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- /*
- * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
- *
- * SPDX-License-Identifier: GPL-2.0-only
- */
- #include <stdint.h>
- #include <esp_wifi.h>
- #include <esp_err.h>
- #include <esp_log.h>
- #include <string.h>
- #include <freertos/FreeRTOS.h>
- #include <freertos/task.h>
- #include <freertos/event_groups.h>
- #include <lwip/ip_addr.h>
- #include "configuration.h"
- #define WIFI_CONNECTED_BIT BIT0
- #define WIFI_FAIL_BIT BIT1
- #define TAG "wifi"
- #define MAX_CONNECT_RETRIES 15
- static EventGroupHandle_t wifi_event_group;
- static int retries = 0;
- static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
- if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
- esp_wifi_connect();
- } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
- if (retries < MAX_CONNECT_RETRIES) {
- esp_wifi_connect();
- retries++;
- ESP_LOGI(TAG, "retry to connect to the AP");
- } else {
- xEventGroupSetBits(wifi_event_group, WIFI_FAIL_BIT);
- }
- ESP_LOGI(TAG,"connect to the AP fail");
- } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
- ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
- ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
- retries = 0;
- xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
- }
- }
- void wifi_init() {
- wifi_event_group = xEventGroupCreate();
- esp_netif_t* netif = esp_netif_create_default_wifi_sta();
- esp_netif_dhcpc_stop(netif);
- esp_netif_ip_info_t ip_info;
- ipaddr_aton(IP_ADDRESS, (struct ip_addr *) &ip_info.ip);
- ipaddr_aton(GATEWAY, (struct ip_addr *) &ip_info.gw);
- ipaddr_aton(NETMASK, (struct ip_addr *) &ip_info.netmask);
- esp_netif_set_ip_info(netif, &ip_info);
-
- esp_netif_dns_info_t dns_info;
- ipaddr_aton(DNS_SERVER, (struct ip_addr *) &dns_info.ip.u_addr.ip4.addr);
- dns_info.ip.type = IPADDR_TYPE_V4;
- esp_netif_set_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_info);
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_wifi_init(&cfg));
- esp_event_handler_instance_t instance_any_id;
- esp_event_handler_instance_t instance_got_ip;
- ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id));
- ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, &instance_got_ip));
- wifi_config_t wifi_config = {
- .sta = {
- .ssid = WIFI_ESSID,
- .password = WIFI_PASSWORD
- },
- };
- wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
- ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
- }
- uint8_t wifi_start() {
- ESP_ERROR_CHECK(esp_wifi_start());
- xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
- pdFALSE, pdFALSE, portMAX_DELAY);
- EventBits_t event_bits = xEventGroupGetBits(wifi_event_group);
- ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler));
- ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler));
- vEventGroupDelete(wifi_event_group);
- return event_bits & WIFI_CONNECTED_BIT;
- }
- void wifi_stop() {
- esp_wifi_disconnect();
- esp_wifi_stop();
- }
|