334 lines
12 KiB
C
334 lines
12 KiB
C
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/event_groups.h"
|
|
#include "esp_system.h"
|
|
#include "esp_wifi.h"
|
|
#include "esp_event.h"
|
|
#include "esp_log.h"
|
|
#include "lwip/err.h"
|
|
#include "lwip/sys.h"
|
|
#include "nvs_flash.h"
|
|
#include "nvs.h"
|
|
#include "esp_check.h"
|
|
#include "esp_memory_utils.h"
|
|
#include "lvgl.h"
|
|
#include "bsp/esp-bsp.h"
|
|
#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;
|
|
|
|
/* The event group allows multiple bits for each event, but we only care about two events:
|
|
* - we are connected to the AP with an IP
|
|
* - we failed to connect after the maximum amount of retries */
|
|
#define WIFI_CONNECTED_BIT BIT0
|
|
#define WIFI_FAIL_BIT BIT1
|
|
|
|
static EventGroupHandle_t s_wifi_event_group;
|
|
|
|
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)
|
|
{
|
|
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 (s_retry_num < 10000) {
|
|
esp_wifi_connect();
|
|
s_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) {
|
|
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
|
|
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;
|
|
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
|
}
|
|
}
|
|
|
|
void wifi_init_sta(void)
|
|
{
|
|
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,
|
|
/* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (password len => 8).
|
|
* If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
|
|
* to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
|
|
* WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
|
|
*/
|
|
.threshold.authmode = WIFI_AUTH_OPEN,
|
|
//.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
|
|
//.sae_h2e_identifier = CONFIG_ESP_WIFI_PW_ID,
|
|
// .disable_wpa3_compatible_mode = 0,
|
|
},
|
|
};
|
|
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() );
|
|
|
|
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
|
|
|
/* 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); */
|
|
|
|
/* 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"); */
|
|
/* } */
|
|
}
|
|
|
|
|
|
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) {
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
ret = nvs_flash_init();
|
|
}
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
// initialize wifi station connection
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_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_lock(100);
|
|
|
|
bsp_display_brightness_set(20);
|
|
|
|
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);
|
|
|
|
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();
|
|
while (true) {
|
|
vTaskDelay(configTICK_RATE_HZ); // 50us
|
|
ESP_LOGI(TAG, " all done ");
|
|
}
|
|
}
|