Advertisement
LukacikPavel

pripojenie k AP

Apr 8th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <string.h>
  4.  
  5. #include <stdlib.h>
  6.  
  7. #include <sys/socket.h>
  8.  
  9. #include <netdb.h>
  10.  
  11. #include <math.h>
  12.  
  13. #include "freertos/FreeRTOS.h"
  14.  
  15. #include "freertos/task.h"
  16.  
  17. #include "freertos/queue.h"
  18.  
  19. #include "freertos/event_groups.h"
  20.  
  21.  
  22.  
  23. #include "driver/gpio.h"
  24.  
  25. #include "driver/i2c.h"
  26.  
  27. #include "driver/hw_timer.h"
  28.  
  29.  
  30.  
  31. #include "esp_log.h"
  32.  
  33. #include "esp_system.h"
  34.  
  35. #include "esp_wifi.h"
  36.  
  37. #include "esp_event_loop.h"
  38.  
  39. #include "esp_err.h"
  40.  
  41.  
  42.  
  43.  
  44.  
  45. #define CONF_ESP_WIFI_SSID      "<AP name>"
  46.  
  47. #define CONF_ESP_WIFI_PASS      "<AP password>"
  48.  
  49.  
  50.  
  51. //**********************************************************************
  52.  
  53.  
  54.  
  55. static EventGroupHandle_t wifi_event_group;
  56.  
  57. const int WIFI_CONNECTED_BIT = BIT0;
  58.  
  59.  
  60.  
  61. static const char *TAG = "WIFI";
  62.  
  63.  
  64.  
  65. //**********************************************************************
  66.  
  67.  
  68.  
  69. static void __attribute__((noreturn)) task_fatal_error()
  70.  
  71. {
  72.  
  73.     ESP_LOGE(TAG, "Exiting task due to fatal error...");
  74.  
  75.     (void)vTaskDelete(NULL);
  76.  
  77.  
  78.  
  79.     while (1) {
  80.  
  81.         ;
  82.  
  83.     }
  84.  
  85. }
  86.  
  87.  
  88.  
  89. static esp_err_t event_handler(void *ctx, system_event_t *event)
  90.  
  91. {
  92.  
  93.     switch(event->event_id) {
  94.  
  95.     case SYSTEM_EVENT_STA_START:
  96.  
  97.         esp_wifi_connect();
  98.  
  99.         break;
  100.  
  101.     case SYSTEM_EVENT_STA_GOT_IP:
  102.  
  103.         ESP_LOGI(TAG, "got ip:%s",
  104.  
  105.                  ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
  106.  
  107.         xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
  108.  
  109.         break;
  110.  
  111.     case SYSTEM_EVENT_STA_DISCONNECTED:
  112.  
  113.         esp_wifi_connect();
  114.  
  115.         xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
  116.  
  117.         break;
  118.  
  119.     default:
  120.  
  121.         break;
  122.  
  123.     }
  124.  
  125.     return ESP_OK;
  126.  
  127. }
  128.  
  129.  
  130.  
  131. void wifi_init_sta()
  132.  
  133. {
  134.  
  135.     wifi_event_group = xEventGroupCreate();
  136.  
  137.  
  138.  
  139.     tcpip_adapter_init();
  140.  
  141.     ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
  142.  
  143.  
  144.  
  145.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  146.  
  147.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  148.  
  149.     wifi_config_t wifi_config = {
  150.  
  151.         .sta = {
  152.  
  153.             .ssid     = CONF_ESP_WIFI_SSID,
  154.  
  155.             .password = CONF_ESP_WIFI_PASS
  156.  
  157.         },
  158.  
  159.     };
  160.  
  161.  
  162.  
  163.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  164.  
  165.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  166.  
  167.     ESP_ERROR_CHECK(esp_wifi_start() );
  168.  
  169.  
  170.  
  171.     ESP_LOGI(TAG, "wifi_init_sta finished.");
  172.  
  173.     ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
  174.  
  175.              CONF_ESP_WIFI_SSID, CONF_ESP_WIFI_PASS);
  176.  
  177. }
  178.  
  179.  
  180.  
  181. //**********************************************************************
  182.  
  183.  
  184.  
  185. void app_main(void)
  186.  
  187. {
  188.  
  189.     wifi_init_sta();
  190.  
  191.  
  192.  
  193.     while (1) {
  194.  
  195.         vTaskDelay(1000 / portTICK_RATE_MS);
  196.  
  197.     }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement