标签:The erase mac sof disco gre tar wip psk
esp32 wifi简单配置,包括ap、sta模式
1 /* Simple WiFi Example 2 3 This example code is in the Public Domain (or CC0 licensed, at your option.) 4 5 Unless required by applicable law or agreed to in writing, this 6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 7 CONDITIONS OF ANY KIND, either express or implied. 8 */ 9 #include <string.h> 10 #include "freertos/FreeRTOS.h" 11 #include "freertos/task.h" 12 #include "freertos/event_groups.h" 13 #include "esp_system.h" 14 #include "esp_wifi.h" 15 #include "esp_event_loop.h" 16 #include "esp_log.h" 17 #include "nvs_flash.h" 18 19 #include "lwip/err.h" 20 #include "lwip/sys.h" 21 22 /* The examples use simple WiFi configuration that you can set via 23 ‘make menuconfig‘. 24 25 If you‘d rather not, just change the below entries to strings with 26 the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" 27 */ 28 #define EXAMPLE_ESP_WIFI_MODE_AP CONFIG_ESP_WIFI_MODE_AP //TRUE:AP FALSE:STA 29 #define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID 30 #define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD 31 #define EXAMPLE_MAX_STA_CONN CONFIG_MAX_STA_CONN 32 33 /* FreeRTOS event group to signal when we are connected*/ 34 static EventGroupHandle_t wifi_event_group; 35 36 /* The event group allows multiple bits for each event, 37 but we only care about one event - are we connected 38 to the AP with an IP? */ 39 const int WIFI_CONNECTED_BIT = BIT0; 40 41 static const char *TAG = "simple wifi"; 42 43 static esp_err_t event_handler(void *ctx, system_event_t *event) 44 { 45 switch(event->event_id) { 46 case SYSTEM_EVENT_STA_START: 47 esp_wifi_connect(); 48 break; 49 case SYSTEM_EVENT_STA_GOT_IP: 50 ESP_LOGI(TAG, "got ip:%s", 51 ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); 52 xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); 53 break; 54 case SYSTEM_EVENT_AP_STACONNECTED: 55 ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", 56 MAC2STR(event->event_info.sta_connected.mac), 57 event->event_info.sta_connected.aid); 58 break; 59 case SYSTEM_EVENT_AP_STADISCONNECTED: 60 ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d", 61 MAC2STR(event->event_info.sta_disconnected.mac), 62 event->event_info.sta_disconnected.aid); 63 break; 64 case SYSTEM_EVENT_STA_DISCONNECTED: 65 esp_wifi_connect(); 66 xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); 67 break; 68 default: 69 break; 70 } 71 return ESP_OK; 72 } 73 74 void wifi_init_softap() 75 { 76 wifi_event_group = xEventGroupCreate(); 77 78 tcpip_adapter_init(); 79 ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); 80 81 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); 82 ESP_ERROR_CHECK(esp_wifi_init(&cfg)); 83 wifi_config_t wifi_config = { 84 .ap = { 85 .ssid = EXAMPLE_ESP_WIFI_SSID, 86 .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID), 87 .password = EXAMPLE_ESP_WIFI_PASS, 88 .max_connection = EXAMPLE_MAX_STA_CONN, 89 .authmode = WIFI_AUTH_WPA_WPA2_PSK 90 }, 91 }; 92 if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) { 93 wifi_config.ap.authmode = WIFI_AUTH_OPEN; 94 } 95 96 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); 97 ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); 98 ESP_ERROR_CHECK(esp_wifi_start()); 99 100 ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s", 101 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); 102 } 103 104 void wifi_init_sta() 105 { 106 wifi_event_group = xEventGroupCreate(); 107 108 tcpip_adapter_init(); 109 ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); 110 111 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); 112 ESP_ERROR_CHECK(esp_wifi_init(&cfg)); 113 wifi_config_t wifi_config = { 114 .sta = { 115 .ssid = EXAMPLE_ESP_WIFI_SSID, 116 .password = EXAMPLE_ESP_WIFI_PASS 117 }, 118 }; 119 120 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); 121 ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); 122 ESP_ERROR_CHECK(esp_wifi_start() ); 123 124 ESP_LOGI(TAG, "wifi_init_sta finished."); 125 ESP_LOGI(TAG, "connect to ap SSID:%s password:%s", 126 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); 127 } 128 129 void app_main() 130 { 131 //Initialize NVS 132 esp_err_t ret = nvs_flash_init(); 133 if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { 134 ESP_ERROR_CHECK(nvs_flash_erase()); 135 ret = nvs_flash_init(); 136 } 137 ESP_ERROR_CHECK(ret); 138 139 #if EXAMPLE_ESP_WIFI_MODE_AP 140 ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); 141 wifi_init_softap(); 142 #else 143 ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); 144 wifi_init_sta(); 145 #endif /*EXAMPLE_ESP_WIFI_MODE_AP*/ 146 147 }
标签:The erase mac sof disco gre tar wip psk
原文地址:https://www.cnblogs.com/llw2017/p/9608809.html