wifi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only
  5. */
  6. #include <stdint.h>
  7. #include <esp_wifi.h>
  8. #include <esp_err.h>
  9. #include <esp_log.h>
  10. #include <string.h>
  11. #include <freertos/FreeRTOS.h>
  12. #include <freertos/task.h>
  13. #include <freertos/event_groups.h>
  14. #include <lwip/ip_addr.h>
  15. #include "configuration.h"
  16. #define WIFI_CONNECTED_BIT BIT0
  17. #define WIFI_FAIL_BIT BIT1
  18. #define TAG "wifi"
  19. #define MAX_CONNECT_RETRIES 15
  20. static EventGroupHandle_t wifi_event_group;
  21. static int retries = 0;
  22. static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
  23. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  24. esp_wifi_connect();
  25. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  26. if (retries < MAX_CONNECT_RETRIES) {
  27. esp_wifi_connect();
  28. retries++;
  29. ESP_LOGI(TAG, "retry to connect to the AP");
  30. } else {
  31. xEventGroupSetBits(wifi_event_group, WIFI_FAIL_BIT);
  32. }
  33. ESP_LOGI(TAG,"connect to the AP fail");
  34. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  35. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  36. ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
  37. retries = 0;
  38. xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
  39. }
  40. }
  41. void wifi_init() {
  42. wifi_event_group = xEventGroupCreate();
  43. esp_netif_t* netif = esp_netif_create_default_wifi_sta();
  44. esp_netif_dhcpc_stop(netif);
  45. esp_netif_ip_info_t ip_info;
  46. ipaddr_aton(IP_ADDRESS, (struct ip_addr *) &ip_info.ip);
  47. ipaddr_aton(GATEWAY, (struct ip_addr *) &ip_info.gw);
  48. ipaddr_aton(NETMASK, (struct ip_addr *) &ip_info.netmask);
  49. esp_netif_set_ip_info(netif, &ip_info);
  50. esp_netif_dns_info_t dns_info;
  51. ipaddr_aton(DNS_SERVER, (struct ip_addr *) &dns_info.ip.u_addr.ip4.addr);
  52. dns_info.ip.type = IPADDR_TYPE_V4;
  53. esp_netif_set_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_info);
  54. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  55. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  56. esp_event_handler_instance_t instance_any_id;
  57. esp_event_handler_instance_t instance_got_ip;
  58. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id));
  59. ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, &instance_got_ip));
  60. wifi_config_t wifi_config = {
  61. .sta = {
  62. .ssid = WIFI_ESSID,
  63. .password = WIFI_PASSWORD
  64. },
  65. };
  66. wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
  67. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  68. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  69. }
  70. uint8_t wifi_start() {
  71. ESP_ERROR_CHECK(esp_wifi_start());
  72. xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  73. pdFALSE, pdFALSE, portMAX_DELAY);
  74. EventBits_t event_bits = xEventGroupGetBits(wifi_event_group);
  75. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler));
  76. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler));
  77. vEventGroupDelete(wifi_event_group);
  78. return event_bits & WIFI_CONNECTED_BIT;
  79. }
  80. void wifi_stop() {
  81. esp_wifi_disconnect();
  82. esp_wifi_stop();
  83. }