-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
1,689 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import requests | ||
|
||
|
||
def fetch_data(url): | ||
try: | ||
response = requests.get(url, stream=True) | ||
response.raise_for_status() | ||
|
||
# define buffer size | ||
buffer_size = 5000 | ||
|
||
# reading data in chunks | ||
for chunk in response.iter_content(chunk_size=buffer_size): | ||
if chunk: | ||
print(chunk.decode("utf-8")) | ||
|
||
except requests.exceptions.RequestException as e: | ||
print(f"HTTP GET request failed: {e}") | ||
|
||
|
||
if __name__ == "__main__": | ||
url = "http://192.168.1.100" | ||
fetch_data(url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# For more information about build system see | ||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html | ||
# The following five lines of boilerplate have to be in your project's | ||
# CMakeLists in this exact order for cmake to work correctly | ||
cmake_minimum_required(VERSION 3.5) | ||
|
||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
project(client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
idf_component_register(SRCS "client.c" | ||
INCLUDE_DIRS ".") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
#include "esp_event.h" | ||
#include "esp_log.h" | ||
#include "esp_wifi.h" | ||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/event_groups.h" | ||
#include "nvs_flash.h" | ||
#include <esp_http_client.h> | ||
|
||
static const char *TAG = "tcp_client"; | ||
|
||
#define WIFI_SSID "tb" | ||
#define WIFI_PASS "HACK@LAB" | ||
#define MAX_RETRY 5 | ||
|
||
#define HOST_IP_ADDR "192.168.1.100" | ||
|
||
static EventGroupHandle_t s_wifi_event_group; | ||
static int retry_num = 0; | ||
#define PORT 3333 | ||
|
||
#define WIFI_CONNECTED_BIT BIT0 | ||
#define WIFI_FAIL_BIT BIT1 | ||
|
||
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) | ||
{ | ||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) | ||
{ | ||
esp_wifi_connect(); | ||
} | ||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) | ||
{ | ||
if (retry_num < MAX_RETRY) | ||
{ | ||
esp_wifi_connect(); | ||
retry_num++; | ||
ESP_LOGI(TAG, "retry to connect to the AP"); | ||
} | ||
else | ||
{ | ||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); | ||
} | ||
ESP_LOGI(TAG, "connect to the AP fail"); | ||
} | ||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) | ||
{ | ||
retry_num = 0; | ||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); | ||
} | ||
} | ||
|
||
static esp_err_t http_event_handler(esp_http_client_event_t *evt) | ||
{ | ||
switch (evt->event_id) | ||
{ | ||
case HTTP_EVENT_ERROR: | ||
ESP_LOGI(TAG, "HTTP_EVENT_ERROR"); | ||
break; | ||
case HTTP_EVENT_ON_CONNECTED: | ||
ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED"); | ||
break; | ||
case HTTP_EVENT_HEADER_SENT: | ||
ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT"); | ||
break; | ||
case HTTP_EVENT_ON_HEADER: | ||
ESP_LOGI(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)) | ||
{ | ||
ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); | ||
printf("%.*s", evt->data_len, (char *)evt->data); | ||
} | ||
break; | ||
case HTTP_EVENT_ON_FINISH: | ||
ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH"); | ||
break; | ||
case HTTP_EVENT_DISCONNECTED: | ||
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED"); | ||
break; | ||
} | ||
|
||
return ESP_OK; | ||
} | ||
|
||
void http_get_task(void *pvParameters) | ||
{ | ||
esp_http_client_config_t config = { | ||
.url = "http://192.168.1.100", | ||
.event_handler = http_event_handler, | ||
.buffer_size = 5000, | ||
.buffer_size_tx = 5000, | ||
}; | ||
esp_http_client_handle_t client = esp_http_client_init(&config); | ||
|
||
// Send the GET request | ||
esp_err_t err = esp_http_client_perform(client); | ||
|
||
if (err == ESP_OK) | ||
{ | ||
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d", | ||
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); | ||
|
||
vTaskDelete(NULL); | ||
} | ||
|
||
void app_main(void) | ||
{ | ||
// Initialize 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); | ||
|
||
s_wifi_event_group = xEventGroupCreate(); | ||
|
||
ESP_ERROR_CHECK(esp_netif_init()); | ||
ESP_ERROR_CHECK(esp_event_loop_create_default()); | ||
|
||
esp_netif_create_default_wifi_sta(); | ||
|
||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | ||
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); | ||
|
||
esp_event_handler_instance_t instance_any_id; | ||
esp_event_handler_instance_t instance_got_ip; | ||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, | ||
ESP_EVENT_ANY_ID, | ||
&event_handler, | ||
NULL, | ||
&instance_any_id)); | ||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, | ||
IP_EVENT_STA_GOT_IP, | ||
&event_handler, | ||
NULL, | ||
&instance_got_ip)); | ||
|
||
wifi_config_t wifi_config = { | ||
.sta = { | ||
.ssid = WIFI_SSID, | ||
.password = WIFI_PASS, | ||
.threshold.authmode = WIFI_AUTH_WPA2_PSK, | ||
.pmf_cfg = { | ||
.capable = true, | ||
.required = false}, | ||
}, | ||
}; | ||
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()); | ||
|
||
ESP_LOGI(TAG, "wifi_init_sta finished."); | ||
|
||
// Wait for Wi-Fi connection | ||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, | ||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, | ||
pdFALSE, | ||
pdFALSE, | ||
portMAX_DELAY); | ||
|
||
if (bits & WIFI_CONNECTED_BIT) | ||
{ | ||
ESP_LOGI(TAG, "connected to ap SSID:%s", WIFI_SSID); | ||
} | ||
else if (bits & WIFI_FAIL_BIT) | ||
{ | ||
ESP_LOGI(TAG, "Failed to connect to SSID:%s", WIFI_SSID); | ||
} | ||
else | ||
{ | ||
ESP_LOGE(TAG, "UNEXPECTED EVENT"); | ||
} | ||
|
||
// Get IP address | ||
esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); | ||
esp_netif_ip_info_t ip_info; | ||
esp_netif_get_ip_info(netif, &ip_info); | ||
|
||
// Convert IP address to human-readable form | ||
char str_ip[16]; | ||
esp_ip4addr_ntoa(&ip_info.ip, str_ip, sizeof(str_ip)); | ||
ESP_LOGI(TAG, "IP Address: %s", str_ip); | ||
|
||
xTaskCreate(&http_get_task, "http_get_task", 10000, NULL, 5, NULL); | ||
} |
Oops, something went wrong.