wifi.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "configuration.h"
  15. #define WIFI_CONNECTED_BIT BIT0
  16. #define WIFI_FAIL_BIT BIT1
  17. #define TAG "wifi"
  18. #define MAX_CONNECT_RETRIES 15
  19. static EventGroupHandle_t wifi_event_group;
  20. static int retries = 0;
  21. static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
  22. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  23. esp_wifi_connect();
  24. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  25. if (retries < MAX_CONNECT_RETRIES) {
  26. esp_wifi_connect();
  27. retries++;
  28. ESP_LOGI(TAG, "retry to connect to the AP");
  29. } else {
  30. xEventGroupSetBits(wifi_event_group, WIFI_FAIL_BIT);
  31. }
  32. ESP_LOGI(TAG,"connect to the AP fail");
  33. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  34. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  35. ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
  36. retries = 0;
  37. xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
  38. }
  39. }
  40. void wifi_init() {
  41. wifi_event_group = xEventGroupCreate();
  42. ESP_ERROR_CHECK(esp_netif_init());
  43. ESP_ERROR_CHECK(esp_event_loop_create_default());
  44. esp_netif_t* netif = esp_netif_create_default_wifi_sta();
  45. esp_netif_dhcpc_stop(netif);
  46. esp_netif_ip_info_t ip_info;
  47. ipaddr_aton(IP_ADDRESS, (struct ip_addr *) &ip_info.ip);
  48. ipaddr_aton(GATEWAY, (struct ip_addr *) &ip_info.gw);
  49. ipaddr_aton(NETMASK, (struct ip_addr *) &ip_info.netmask);
  50. esp_netif_set_ip_info(netif, &ip_info);
  51. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  52. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  53. esp_event_handler_instance_t instance_any_id;
  54. esp_event_handler_instance_t instance_got_ip;
  55. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id));
  56. ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, &instance_got_ip));
  57. wifi_config_t wifi_config = {
  58. .sta = {
  59. .ssid = WIFI_ESSID,
  60. .password = WIFI_PASSWORD
  61. },
  62. };
  63. wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
  64. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  65. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  66. }
  67. uint8_t wifi_start() {
  68. ESP_ERROR_CHECK(esp_wifi_start());
  69. xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  70. pdFALSE, pdFALSE, portMAX_DELAY);
  71. EventBits_t event_bits = xEventGroupGetBits(wifi_event_group);
  72. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler));
  73. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler));
  74. vEventGroupDelete(wifi_event_group);
  75. return event_bits & WIFI_CONNECTED_BIT;
  76. }
  77. void wifi_stop() {
  78. esp_wifi_disconnect();
  79. esp_wifi_stop();
  80. }