1 /* Scan 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 10 /* 11 This example shows how to use the All Channel Scan or Fast Scan to connect 12 to a Wi-Fi network. 13 14 In the Fast Scan mode, the scan will stop as soon as the first network matching 15 the SSID is found. In this mode, an application can set threshold for the 16 authentication mode and the Signal strength. Networks that do not meet the 17 threshold requirements will be ignored. 18 19 In the All Channel Scan mode, the scan will end only after all the channels 20 are scanned, and connection will start with the best network. The networks 21 can be sorted based on Authentication Mode or Signal Strength. The priority 22 for the Authentication mode is: WPA2 > WPA > WEP > Open 23 */ 24 #include "freertos/FreeRTOS.h" 25 #include "freertos/event_groups.h" 26 #include "esp_wifi.h" 27 #include "esp_log.h" 28 #include "esp_event_loop.h" 29 #include "nvs_flash.h" 30 #include "tcpip_adapter.h" 31 #include "lwip/api.h" 32 #include "esp_err.h" 33 #include <stdio.h> 34 #include <string.h> 35 36 /*Set the SSID and Password via "make menuconfig"*/ 37 #define DEFAULT_SSID "putal"//CONFIG_WIFI_SSID 38 #define DEFAULT_PWD "12344321"//CONFIG_WIFI_PASSWORD 39 40 #if CONFIG_WIFI_ALL_CHANNEL_SCAN 41 #define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN 42 #elif CONFIG_WIFI_FAST_SCAN 43 #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN 44 #else 45 #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN 46 #endif /*CONFIG_SCAN_METHOD*/ 47 48 #if CONFIG_WIFI_CONNECT_AP_BY_SIGNAL 49 #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL 50 #elif CONFIG_WIFI_CONNECT_AP_BY_SECURITY 51 #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY 52 #else 53 #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL 54 #endif /*CONFIG_SORT_METHOD*/ 55 56 #if CONFIG_FAST_SCAN_THRESHOLD 57 #define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL 58 #if CONFIG_EXAMPLE_OPEN 59 #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN 60 #elif CONFIG_EXAMPLE_WEP 61 #define DEFAULT_AUTHMODE WIFI_AUTH_WEP 62 #elif CONFIG_EXAMPLE_WPA 63 #define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK 64 #elif CONFIG_EXAMPLE_WPA2 65 #define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK 66 #else 67 #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN 68 #endif 69 #else 70 #define DEFAULT_RSSI -127 71 #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN 72 #endif /*CONFIG_FAST_SCAN_THRESHOLD*/ 73 74 #define TCP_Client_RX_BUFSIZE 199 75 void TCP_Client(void *pvParameter) 76 { 77 uint32_t date_len=0; 78 esp_err_t err,recv_err; 79 static u16_t server_port,local_port; 80 static ip_addr_t server_ipaddr,loca_ipaddr; 81 struct pbuf *q; 82 struct netconn *tcp_clientconn; 83 char tcp_client_sendbuf[200]="OK123123hahahaha"; 84 char tcp_client_recvbuf[200]; 85 // xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,false,true,protMAX_DELAY); 86 LWIP_UNUSED_ARG(pvParameter); 87 server_port=10000; 88 IP4_ADDR(&(server_ipaddr.u_addr.ip4),118,89,21,146); 89 while(1) 90 { 91 tcp_clientconn = netconn_new(NETCONN_TCP); 92 93 err = netconn_connect(tcp_clientconn,&server_ipaddr,server_port); 94 95 if( err!=ERR_OK ) 96 { 97 netconn_delete(tcp_clientconn); 98 } 99 else if(err == ERR_OK) 100 { 101 tcp_clientconn->recv_timeout=10; 102 netconn_getaddr(tcp_clientconn,&loca_ipaddr,&local_port,1); 103 printf("Connect server\r\n"); 104 while(1) 105 { 106 struct netbuf *recvbuf; 107 err = netconn_write(tcp_clientconn,tcp_client_sendbuf,strlen((char *)tcp_client_sendbuf),NETCONN_COPY); 108 if(err!= ERR_OK) 109 { 110 printf("Send error\r\n"); 111 } 112 if((recv_err=netconn_recv(tcp_clientconn,&recvbuf))==ERR_OK) 113 { 114 memset(tcp_client_recvbuf,0,TCP_Client_RX_BUFSIZE); 115 for(q=recvbuf->p;q!=NULL;q=q->next) 116 { 117 if(q->len>(TCP_Client_RX_BUFSIZE-date_len))memcpy(tcp_client_recvbuf+date_len,q->payload,(TCP_Client_RX_BUFSIZE-date_len)); 118 else memcpy(tcp_client_recvbuf+date_len,q->payload,q->len); 119 date_len+=q->len; 120 if(date_len > TCP_Client_RX_BUFSIZE)break; 121 122 } 123 date_len=0; 124 printf("%s\r\n", tcp_client_recvbuf); 125 netbuf_delete(recvbuf); 126 127 } 128 else if(recv_err==ERR_CLSD) 129 { 130 netconn_close(tcp_clientconn); 131 netconn_delete(tcp_clientconn); 132 printf("Close server\r\n"); 133 break; 134 } 135 vTaskDelay(1000/portTICK_PERIOD_MS); 136 } 137 } 138 } 139 } 140 141 static const char *TAG = "scan"; 142 143 static esp_err_t event_handler(void *ctx, system_event_t *event) 144 { 145 switch (event->event_id) { 146 case SYSTEM_EVENT_STA_START: 147 ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); 148 ESP_ERROR_CHECK(esp_wifi_connect()); 149 break; 150 case SYSTEM_EVENT_STA_GOT_IP: 151 ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); 152 ESP_LOGI(TAG, "Got IP: %s\n", 153 ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); 154 155 xTaskCreate(TCP_Client, "server", 2048, NULL, (tskIDLE_PRIORITY + 2), NULL); 156 157 break; 158 case SYSTEM_EVENT_STA_DISCONNECTED: 159 ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); 160 ESP_ERROR_CHECK(esp_wifi_connect()); 161 break; 162 default: 163 break; 164 } 165 return ESP_OK; 166 } 167 168 /* Initialize Wi-Fi as sta and set scan method */ 169 static void wifi_scan(void) 170 { 171 tcpip_adapter_init(); 172 ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); 173 174 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); 175 ESP_ERROR_CHECK(esp_wifi_init(&cfg)); 176 wifi_config_t wifi_config = { 177 .sta = { 178 .ssid = DEFAULT_SSID, 179 .password = DEFAULT_PWD, 180 .scan_method = DEFAULT_SCAN_METHOD, 181 .sort_method = DEFAULT_SORT_METHOD, 182 .threshold.rssi = DEFAULT_RSSI, 183 .threshold.authmode = DEFAULT_AUTHMODE, 184 }, 185 }; 186 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); 187 ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); 188 ESP_ERROR_CHECK(esp_wifi_start()); 189 } 190 191 void app_main() 192 { 193 // Initialize NVS 194 esp_err_t ret = nvs_flash_init(); 195 if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { 196 ESP_ERROR_CHECK(nvs_flash_erase()); 197 ret = nvs_flash_init(); 198 } 199 ESP_ERROR_CHECK( ret ); 200 201 wifi_scan(); 202 }