https request with graphics works finally

This commit is contained in:
cqc
2026-02-21 16:21:47 +01:00
parent b8d315d7e4
commit a2605672d2
3 changed files with 200 additions and 83 deletions

View File

@ -1,6 +0,0 @@
import update
update.main()
import minimap
minimap.main()

View File

@ -16,6 +16,13 @@
#include "bsp/display.h" #include "bsp/display.h"
#include "secrets.h" #include "secrets.h"
#include "esp_crt_bundle.h"
#include "esp_tls.h"
#include "esp_http_client.h"
#define MAX_HTTP_RECV_BUFFER 512
#define MAX_HTTP_OUTPUT_BUFFER 2048
/* FreeRTOS event group to signal when we are connected*/ /* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group; static EventGroupHandle_t s_wifi_event_group;
@ -27,10 +34,11 @@ static EventGroupHandle_t s_wifi_event_group;
static EventGroupHandle_t s_wifi_event_group; static EventGroupHandle_t s_wifi_event_group;
static const char *TAG = "wifi station"; static const char *TAG = "main";
static int s_retry_num = 0; static int s_retry_num = 0;
void textarea_log(char * s);
static void event_handler(void* arg, esp_event_base_t event_base, static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data) int32_t event_id, void* event_data)
@ -49,6 +57,9 @@ static void event_handler(void* arg, esp_event_base_t event_base,
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
char ipaddr_string[20];
snprintf(ipaddr_string, sizeof(ipaddr_string), "ip:"IPSTR"\n", IP2STR(&event->ip_info.ip));
textarea_log(ipaddr_string);
s_retry_num = 0; s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
} }
@ -103,32 +114,165 @@ void wifi_init_sta(void)
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, /* EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, */
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, /* WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, */
pdFALSE, /* pdFALSE, */
pdFALSE, /* pdFALSE, */
portMAX_DELAY); /* portMAX_DELAY); */
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */ * happened. */
if (bits & WIFI_CONNECTED_BIT) { /* if (bits & WIFI_CONNECTED_BIT) { */
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", /* ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", */
WIFI_SSID, WIFI_PASS); /* WIFI_SSID, WIFI_PASS); */
} else if (bits & WIFI_FAIL_BIT) { /* } else if (bits & WIFI_FAIL_BIT) { */
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", /* ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", */
WIFI_SSID, WIFI_PASS); /* WIFI_SSID, WIFI_PASS); */
} else { /* } else { */
ESP_LOGE(TAG, "UNEXPECTED EVENT"); /* ESP_LOGE(TAG, "UNEXPECTED EVENT"); */
/* } */
}
static char *output_buffer; // Buffer to store response of http request from event handler
static int output_len; // Stores number of bytes read
esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
const char *TAG = "_http_event_hander";
esp_log_level_set(TAG, ESP_LOG_DEBUG);
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_HEADER_SENT:
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
break;
case HTTP_EVENT_ON_DATA:
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
// Clean the buffer in case of a new request
if (output_len == 0 && evt->user_data) {
// we are just starting to copy the output data into the use
memset(evt->user_data, 0, MAX_HTTP_OUTPUT_BUFFER);
} }
/*
* Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data.
* However, event handler can also be used in case chunked encoding is used.
*/
if (!esp_http_client_is_chunked_response(evt->client)) {
// If user_data buffer is configured, copy the response into the buffer
int copy_len = 0;
if (evt->user_data) {
// The last byte in evt->user_data is kept for the NULL character in case of out-of-bound access.
copy_len = MIN(evt->data_len, (MAX_HTTP_OUTPUT_BUFFER - output_len));
if (copy_len) {
memcpy(evt->user_data + output_len, evt->data, copy_len);
}
} else {
int content_len = esp_http_client_get_content_length(evt->client);
if (output_buffer == NULL) {
// We initialize output_buffer with 0 because it is used by strlen() and similar functions therefore should be null terminated.
ESP_LOGI(TAG, "calloc()");
output_buffer = (char *) calloc(content_len + 1, sizeof(char));
output_len = 0;
if (output_buffer == NULL) {
ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
return ESP_FAIL;
}
}
copy_len = MIN(evt->data_len, (content_len - output_len));
if (copy_len) {
memcpy(output_buffer + output_len, evt->data, copy_len);
}
}
output_len += copy_len;
}
break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
if (output_buffer != NULL) {
free(output_buffer);
output_buffer = NULL;
}
output_len = 0;
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
int mbedtls_err = 0;
esp_err_t err = esp_tls_get_and_clear_last_error((esp_tls_error_handle_t)evt->data, &mbedtls_err, NULL);
if (err != 0) {
ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
}
if (output_buffer != NULL) {
ESP_LOGI(TAG, "output_buffer free()");
free(output_buffer);
output_buffer = NULL;
}
output_len = 0;
break;
case HTTP_EVENT_REDIRECT:
ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT");
esp_http_client_set_header(evt->client, "From", "user@example.com");
esp_http_client_set_header(evt->client, "Accept", "text/html");
esp_http_client_set_redirection(evt->client);
break;
}
return ESP_OK;
}
static void http_test_task(void *pvParameters) {
esp_log_level_set("esp-tls-mbedtls", ESP_LOG_DEBUG);
// wait for wifi to be connected
while (!(WIFI_CONNECTED_BIT & xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY)));
char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER + 1] = {0};
esp_http_client_config_t config = {
.url = "https://snieck.us",
.event_handler = _http_event_handler,
.user_data = local_response_buffer, // Pass address of local buffer to get response
/* .crt_bundle_attach = esp_crt_bundle_attach, */
};
ESP_LOGI(TAG, "HTTPS request with url => %s", config.url);
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, "HTTPS Status = %d, content_length = %"PRId64,
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
esp_http_client_cleanup(client);
} else {
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
}
vTaskDelete(NULL);
} }
static const lv_font_t * font_large; static const lv_font_t * font_large;
static lv_style_t style_bw; static lv_style_t style_bw;
static lv_obj_t * text;
void textarea_log(char * s) {
bsp_display_lock(100);
lv_textarea_add_text(text, s);
bsp_display_unlock();
}
void app_main(void) void app_main(void)
{ {
//Initialize NVS //Initialize NVS
esp_err_t ret = nvs_flash_init(); esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
@ -137,11 +281,15 @@ void app_main(void)
} }
ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(ret);
// initialize wifi station connection
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta(); wifi_init_sta();
// initialize http client
ESP_ERROR_CHECK(esp_netif_init());
xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
// initalize graphics
bsp_display_start(); bsp_display_start();
bsp_display_lock(100); bsp_display_lock(100);
@ -171,14 +319,15 @@ void app_main(void)
lv_label_set_text(title, "freeside minimap"); lv_label_set_text(title, "freeside minimap");
lv_obj_set_align(title, LV_ALIGN_TOP_MID); lv_obj_set_align(title, LV_ALIGN_TOP_MID);
lv_obj_t * text = lv_textarea_create(lv_screen_active()); text = lv_textarea_create(lv_screen_active());
lv_obj_add_style(text, &style_bw, LV_PART_MAIN); lv_obj_add_style(text, &style_bw, LV_PART_MAIN);
lv_obj_set_pos(text, 50, 25); lv_obj_set_pos(text, 50, 25);
lv_textarea_add_text(text, "hello"); lv_textarea_add_text(text, "hello");
lv_textarea_add_text(text, "world!"); lv_textarea_add_text(text, " world!\n");
lv_textarea_add_text(text, "\nnewline");
bsp_display_unlock(); bsp_display_unlock();
while (true) {
vTaskDelay(configTICK_RATE_HZ); // 50us
ESP_LOGI(TAG, " all done ");
}
} }

View File

@ -1,26 +0,0 @@
import io
import storage
import wifi
import adafruit_connection_manager
import adafruit_requests
update_url = "https://gitea.departmentofinter.net/cqc/freeside-minimap/raw/branch/memes/minimap.py"
update_file = 'minimap.py'
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)
def run_update():
with io.open(update_file, mode='w') as f:
with requests.get(update_url) as r:
print('\nupdating',update_file)
f.write(r.text)
def main():
try:
run_update()
except Exception as e:
import traceback
traceback.print_exception(e)