https request with graphics works finally
This commit is contained in:
235
main/main.c
235
main/main.c
@ -16,6 +16,13 @@
|
||||
#include "bsp/display.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*/
|
||||
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 const char *TAG = "wifi station";
|
||||
static const char *TAG = "main";
|
||||
|
||||
static int s_retry_num = 0;
|
||||
|
||||
void textarea_log(char * s);
|
||||
|
||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
@ -49,7 +57,10 @@ 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) {
|
||||
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
|
||||
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
s_retry_num = 0;
|
||||
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;
|
||||
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
|
||||
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY);
|
||||
/* EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, */
|
||||
/* WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, */
|
||||
/* pdFALSE, */
|
||||
/* pdFALSE, */
|
||||
/* portMAX_DELAY); */
|
||||
|
||||
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
|
||||
* happened. */
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
|
||||
WIFI_SSID, WIFI_PASS);
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
|
||||
WIFI_SSID, WIFI_PASS);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||
/* if (bits & WIFI_CONNECTED_BIT) { */
|
||||
/* ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", */
|
||||
/* WIFI_SSID, WIFI_PASS); */
|
||||
/* } else if (bits & WIFI_FAIL_BIT) { */
|
||||
/* ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", */
|
||||
/* WIFI_SSID, WIFI_PASS); */
|
||||
/* } else { */
|
||||
/* 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 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)
|
||||
{
|
||||
|
||||
//Initialize NVS
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
@ -137,48 +281,53 @@ void app_main(void)
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
|
||||
|
||||
// initialize wifi station connection
|
||||
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
||||
wifi_init_sta();
|
||||
|
||||
bsp_display_start();
|
||||
// initialize http client
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
|
||||
|
||||
bsp_display_lock(100);
|
||||
// initalize graphics
|
||||
bsp_display_start();
|
||||
|
||||
bsp_display_brightness_set(20);
|
||||
bsp_display_lock(100);
|
||||
|
||||
font_large = &lv_font_montserrat_18;
|
||||
bsp_display_brightness_set(20);
|
||||
|
||||
lv_style_init(&style_bw);
|
||||
//lv_style_set_text_font(&style_bw, font_large);
|
||||
lv_style_set_bg_color(&style_bw, lv_color_hex(0x000000));
|
||||
lv_style_set_text_color(&style_bw, lv_color_hex(0xffffff));
|
||||
lv_style_set_radius(&style_bw, 0);
|
||||
lv_obj_add_style(lv_screen_active(), &style_bw, LV_PART_MAIN);
|
||||
font_large = &lv_font_montserrat_18;
|
||||
|
||||
/*
|
||||
lv_style_init(&style_bw);
|
||||
//lv_style_set_text_font(&style_bw, font_large);
|
||||
lv_style_set_bg_color(&style_bw, lv_color_hex(0x000000));
|
||||
lv_style_set_text_color(&style_bw, lv_color_hex(0xffffff));
|
||||
lv_style_set_radius(&style_bw, 0);
|
||||
lv_obj_add_style(lv_screen_active(), &style_bw, LV_PART_MAIN);
|
||||
|
||||
/*
|
||||
lv_obj_t * cont = lv_obj_create(lv_screen_active());
|
||||
lv_obj_add_style(cont, &style_bw, LV_PART_MAIN);
|
||||
lv_obj_set_style_border_width(cont, 0, LV_PART_MAIN);
|
||||
lv_obj_set_size(cont, lv_pct(100), lv_pct(100));
|
||||
lv_obj_center(cont);
|
||||
lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
|
||||
*/
|
||||
*/
|
||||
|
||||
lv_obj_t * title = lv_label_create(lv_screen_active());
|
||||
lv_obj_add_style(title, &style_bw, LV_PART_MAIN);
|
||||
lv_label_set_text(title, "freeside minimap");
|
||||
lv_obj_set_align(title, LV_ALIGN_TOP_MID);
|
||||
lv_obj_t * title = lv_label_create(lv_screen_active());
|
||||
lv_obj_add_style(title, &style_bw, LV_PART_MAIN);
|
||||
lv_label_set_text(title, "freeside minimap");
|
||||
lv_obj_set_align(title, LV_ALIGN_TOP_MID);
|
||||
|
||||
lv_obj_t * text = lv_textarea_create(lv_screen_active());
|
||||
lv_obj_add_style(text, &style_bw, LV_PART_MAIN);
|
||||
lv_obj_set_pos(text, 50, 25);
|
||||
lv_textarea_add_text(text, "hello");
|
||||
lv_textarea_add_text(text, "world!");
|
||||
lv_textarea_add_text(text, "\nnewline");
|
||||
text = lv_textarea_create(lv_screen_active());
|
||||
lv_obj_add_style(text, &style_bw, LV_PART_MAIN);
|
||||
lv_obj_set_pos(text, 50, 25);
|
||||
lv_textarea_add_text(text, "hello");
|
||||
lv_textarea_add_text(text, " world!\n");
|
||||
|
||||
|
||||
|
||||
bsp_display_unlock();
|
||||
bsp_display_unlock();
|
||||
while (true) {
|
||||
vTaskDelay(configTICK_RATE_HZ); // 50us
|
||||
ESP_LOGI(TAG, " all done ");
|
||||
}
|
||||
}
|
||||
|
||||
26
update.py
26
update.py
@ -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)
|
||||
Reference in New Issue
Block a user