|
|
@@ -1,73 +1,18 @@
|
|
|
-/* Scan Example
|
|
|
-
|
|
|
- This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
-
|
|
|
- Unless required by applicable law or agreed to in writing, this
|
|
|
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
- CONDITIONS OF ANY KIND, either express or implied.
|
|
|
-*/
|
|
|
-
|
|
|
-/*
|
|
|
- This example shows how to use the All Channel Scan or Fast Scan to connect
|
|
|
- to a Wi-Fi network.
|
|
|
-
|
|
|
- In the Fast Scan mode, the scan will stop as soon as the first network matching
|
|
|
- the SSID is found. In this mode, an application can set threshold for the
|
|
|
- authentication mode and the Signal strength. Networks that do not meet the
|
|
|
- threshold requirements will be ignored.
|
|
|
-
|
|
|
- In the All Channel Scan mode, the scan will end only after all the channels
|
|
|
- are scanned, and connection will start with the best network. The networks
|
|
|
- can be sorted based on Authentication Mode or Signal Strength. The priority
|
|
|
- for the Authentication mode is: WPA2 > WPA > WEP > Open
|
|
|
-*/
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
#include "freertos/event_groups.h"
|
|
|
#include "esp_wifi.h"
|
|
|
#include "esp_log.h"
|
|
|
#include "esp_event.h"
|
|
|
#include "nvs_flash.h"
|
|
|
+#include "esp_http_client.h"
|
|
|
|
|
|
-/* Set the SSID and Password via project configuration, or can set directly here */
|
|
|
+// 设置Wi-Fi的SSID和密码
|
|
|
#define DEFAULT_SSID "xxy"
|
|
|
#define DEFAULT_PWD "123456x-"
|
|
|
|
|
|
-#if CONFIG_EXAMPLE_WIFI_ALL_CHANNEL_SCAN
|
|
|
-#define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
|
|
|
-#elif CONFIG_EXAMPLE_WIFI_FAST_SCAN
|
|
|
-#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
|
|
|
-#else
|
|
|
-#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
|
|
|
-#endif /*CONFIG_EXAMPLE_SCAN_METHOD*/
|
|
|
-
|
|
|
-#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
|
|
|
-#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
|
|
-#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
|
|
|
-#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
|
|
|
-#else
|
|
|
-#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
|
|
-#endif /*CONFIG_EXAMPLE_SORT_METHOD*/
|
|
|
-
|
|
|
-#if CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD
|
|
|
-#define DEFAULT_RSSI CONFIG_EXAMPLE_FAST_SCAN_MINIMUM_SIGNAL
|
|
|
-#if CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_OPEN
|
|
|
-#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
|
|
-#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WEP
|
|
|
-#define DEFAULT_AUTHMODE WIFI_AUTH_WEP
|
|
|
-#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA
|
|
|
-#define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
|
|
|
-#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA2
|
|
|
-#define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
|
|
|
-#else
|
|
|
-#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
|
|
-#endif
|
|
|
-#else
|
|
|
-#define DEFAULT_RSSI -127
|
|
|
-#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
|
|
-#endif /*CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD*/
|
|
|
-
|
|
|
-static const char *TAG = "scan";
|
|
|
+static const char *TAG = "get_request";
|
|
|
|
|
|
+// Wi-Fi 事件处理函数
|
|
|
static void event_handler(void* arg, esp_event_base_t event_base,
|
|
|
int32_t event_id, void* event_data)
|
|
|
{
|
|
|
@@ -81,9 +26,8 @@ static void event_handler(void* arg, esp_event_base_t event_base,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-/* Initialize Wi-Fi as sta and set scan method */
|
|
|
-static void fast_scan(void)
|
|
|
+// 初始化Wi-Fi并连接
|
|
|
+static void wifi_init_sta(void)
|
|
|
{
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
@@ -94,42 +38,90 @@ static void fast_scan(void)
|
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));
|
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, NULL));
|
|
|
|
|
|
- // Initialize default station as network interface instance (esp-netif)
|
|
|
+ // 初始化默认的Wi-Fi STA模式
|
|
|
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
|
|
assert(sta_netif);
|
|
|
|
|
|
- // Initialize and start WiFi
|
|
|
+ // 配置Wi-Fi连接信息
|
|
|
wifi_config_t wifi_config = {
|
|
|
.sta = {
|
|
|
.ssid = DEFAULT_SSID,
|
|
|
.password = DEFAULT_PWD,
|
|
|
- .scan_method = DEFAULT_SCAN_METHOD,
|
|
|
- .sort_method = DEFAULT_SORT_METHOD,
|
|
|
- .threshold.rssi = DEFAULT_RSSI,
|
|
|
- .threshold.authmode = DEFAULT_AUTHMODE,
|
|
|
},
|
|
|
};
|
|
|
+
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
}
|
|
|
|
|
|
+// HTTP事件处理函数
|
|
|
+esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
|
|
+{
|
|
|
+ switch (evt->event_id) {
|
|
|
+ case HTTP_EVENT_ERROR:
|
|
|
+ ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
|
|
|
+ break;
|
|
|
+ case HTTP_EVENT_ON_CONNECTED:
|
|
|
+ ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
|
|
|
+ break;
|
|
|
+ case HTTP_EVENT_ON_DATA:
|
|
|
+ ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
|
|
|
+ if (evt->data_len) {
|
|
|
+ ESP_LOGI(TAG, "Received data: %.*s", evt->data_len, (char*)evt->data);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case HTTP_EVENT_ON_FINISH:
|
|
|
+ ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
|
|
|
+ break;
|
|
|
+ case HTTP_EVENT_DISCONNECTED:
|
|
|
+ ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return ESP_OK;
|
|
|
+}
|
|
|
+
|
|
|
+// 发起一个简单的GET请求
|
|
|
+static void http_get_request(void)
|
|
|
+{
|
|
|
+ esp_http_client_config_t config = {
|
|
|
+ .url = "http://60.204.139.57:6002/categories", // 替换为你要请求的URL
|
|
|
+ .event_handler = _http_event_handler,
|
|
|
+ };
|
|
|
+ esp_http_client_handle_t client = esp_http_client_init(&config);
|
|
|
+
|
|
|
+ // 执行GET请求
|
|
|
+ esp_err_t err = esp_http_client_perform(client);
|
|
|
+ if (err == ESP_OK) {
|
|
|
+ ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRId64,
|
|
|
+ esp_http_client_get_status_code(client),
|
|
|
+ esp_http_client_get_content_length(client));
|
|
|
+ } else {
|
|
|
+ ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
|
|
|
+ }
|
|
|
+
|
|
|
+ esp_http_client_cleanup(client);
|
|
|
+}
|
|
|
+
|
|
|
+// 主函数
|
|
|
void app_main(void)
|
|
|
{
|
|
|
- // Initialize NVS
|
|
|
+ // 初始化NVS
|
|
|
esp_err_t ret = nvs_flash_init();
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
ret = nvs_flash_init();
|
|
|
}
|
|
|
- ESP_ERROR_CHECK( ret );
|
|
|
+ ESP_ERROR_CHECK(ret);
|
|
|
|
|
|
- fast_scan();
|
|
|
+ // 初始化Wi-Fi并连接
|
|
|
+ wifi_init_sta();
|
|
|
+
|
|
|
+ // 等待Wi-Fi连接
|
|
|
+ vTaskDelay(10000 / portTICK_PERIOD_MS);
|
|
|
|
|
|
- int i = 0;
|
|
|
- while (1) {
|
|
|
- ESP_LOGI(TAG, "Scan done");
|
|
|
- ESP_LOGI(TAG, "AP number:%d", i++);
|
|
|
- vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
|
- }
|
|
|
+ // 执行HTTP GET请求
|
|
|
+ http_get_request();
|
|
|
}
|