0
点赞
收藏
分享

微信扫一扫

http下载


/* HTTP GET Example using plain POSIX sockets

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.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_http_client.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "hardware/uart/ms_uart.h"
/* The examples use simple WiFi configuration that you can set via
'make menuconfig'.

If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_WIFI_SSID "NETGEAR86"
#define EXAMPLE_WIFI_PASS "jack.12306"

/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;

/* The event group allows multiple bits for each event,
but we only care about one event - are we connected
to the AP with an IP? */
const int CONNECTED_BIT = BIT0;

/* Constants that aren't configurable in menuconfig */
#define WEB_SERVER "192.168.8.130"
#define WEB_PORT 8080
#define WEB_URL "http://192.168.8.130:8080/tomcat.png"
//http://192.168.2.189:8080/tomcat.png
static const char *TAG = "example";

static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n"
"Host: "WEB_SERVER"\r\n"
"User-Agent: esp-idf/1.0 esp32\r\n"
"\r\n";

int data_len = 0;

esp_err_t _http_event_handler(esp_http_client_event_t *evt) {
switch (evt->event_id) {
case HTTP_EVENT_ERROR:
ESP_LOGE(TAG, "HTTP_EVENT_ERROR");
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGE(TAG, "HTTP_EVENT_ON_CONNECTED");
break;
case HTTP_EVENT_HEADER_SENT:
ESP_LOGE(TAG, "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
ESP_LOGE(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key,
evt->header_value);
break;
case HTTP_EVENT_ON_DATA:
if (!esp_http_client_is_chunked_response(evt->client)) {
uart_write_bytes(UART_NUM_2, (const char *) evt->data,
evt->data_len);
data_len += evt->data_len;
printf("======%d====", data_len);
}
break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGE(TAG, "HTTP_EVENT_ON_FINISH");
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGE(TAG, "HTTP_EVENT_DISCONNECTED");
break;
}
return ESP_OK;
}

static void http_download_chunk() {
esp_http_client_config_t config =
{
//.url = "http://httpbin.org/stream-bytes/8912",
.url =
"http://192.168.1.4:7803/web-api/file/downloadFile?fileName=ESP8266Flasher.exe&index=0&size=524288",
.event_handler = _http_event_handler, };
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_perform(client);

if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP chunk encoding Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
while (1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

static esp_err_t event_handler(void *ctx, system_event_t *event) {
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect();
break;
default:
break;
}
return ESP_OK;
}

static void initialise_wifi(void) {
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
;
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_WIFI_SSID,
.password = EXAMPLE_WIFI_PASS, }, };
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...",
wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
}

void app_main() {
ESP_ERROR_CHECK(nvs_flash_init());
uart_init();
initialise_wifi();
}


举报

相关推荐

0 条评论