wifi.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_ERROR_CHECK(esp_netif_init());
  44. ESP_ERROR_CHECK(esp_event_loop_create_default());
  45. esp_netif_t* netif = esp_netif_create_default_wifi_sta();
  46. esp_netif_dhcpc_stop(netif);
  47. esp_netif_ip_info_t ip_info;
  48. ipaddr_aton(IP_ADDRESS, (struct ip_addr *) &ip_info.ip);
  49. ipaddr_aton(GATEWAY, (struct ip_addr *) &ip_info.gw);
  50. ipaddr_aton(NETMASK, (struct ip_addr *) &ip_info.netmask);
  51. esp_netif_set_ip_info(netif, &ip_info);
  52. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  53. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  54. esp_event_handler_instance_t instance_any_id;
  55. esp_event_handler_instance_t instance_got_ip;
  56. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id));
  57. ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, &instance_got_ip));
  58. wifi_config_t wifi_config = {
  59. .sta = {
  60. .ssid = WIFI_ESSID,
  61. .password = WIFI_PASSWORD
  62. },
  63. };
  64. wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
  65. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  66. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  67. }
  68. uint8_t wifi_start() {
  69. ESP_ERROR_CHECK(esp_wifi_start());
  70. xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  71. pdFALSE, pdFALSE, portMAX_DELAY);
  72. EventBits_t event_bits = xEventGroupGetBits(wifi_event_group);
  73. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler));
  74. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler));
  75. vEventGroupDelete(wifi_event_group);
  76. return event_bits & WIFI_CONNECTED_BIT;
  77. }
  78. void wifi_stop() {
  79. esp_wifi_disconnect();
  80. esp_wifi_stop();
  81. }