|
| 1 | +#include <stdio.h> |
| 2 | + |
| 3 | +#include <unistd.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#include <esp_system.h> |
| 7 | +#include <esp_tls.h> |
| 8 | +#include <esp_wifi.h> |
| 9 | +#include <nvs_flash.h> |
| 10 | + |
| 11 | +#include <lwip/dns.h> |
| 12 | + |
| 13 | +#include <freertos/event_groups.h> |
| 14 | + |
| 15 | +//================ WIFI ============= |
| 16 | +#define ESP_WIFI_SSID "SSID" |
| 17 | +#define ESP_WIFI_PASS "p4zzw0rd" |
| 18 | +#define ESP_MAXIMUM_RETRY 5 |
| 19 | +#define WIFI_CONNECTED_BIT BIT0 |
| 20 | + |
| 21 | + |
| 22 | +static bool s_is_wifi_connected = false; |
| 23 | +static EventGroupHandle_t s_event_group_handler; |
| 24 | +static int s_retry_count = 0; |
| 25 | +//=================================== |
| 26 | + |
| 27 | +ip4_addr_t server_ip; //DNS server IP |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +//================ TLS ============= |
| 32 | +extern const uint8_t ca_crt_start[] asm("_binary_ca_crt_start"); |
| 33 | +extern const uint8_t ca_crt_end[] asm("_binary_ca_crt_end"); |
| 34 | + |
| 35 | +char * hostname = "sslserver.home"; |
| 36 | +//================================== |
| 37 | + |
| 38 | +//event handler of the WiFi |
| 39 | +static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) |
| 40 | +{ |
| 41 | + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) |
| 42 | + { |
| 43 | + esp_wifi_connect(); |
| 44 | + } |
| 45 | + else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) |
| 46 | + { |
| 47 | + if (s_retry_count < ESP_MAXIMUM_RETRY) |
| 48 | + { |
| 49 | + esp_wifi_connect(); |
| 50 | + s_retry_count++; |
| 51 | + } |
| 52 | + } |
| 53 | + else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) |
| 54 | + { |
| 55 | + xEventGroupSetBits(s_event_group_handler, WIFI_CONNECTED_BIT); |
| 56 | + s_retry_count = 0; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void wifi_init_sta(void) |
| 61 | +{ |
| 62 | + s_event_group_handler = xEventGroupCreate(); |
| 63 | + |
| 64 | + ESP_ERROR_CHECK(esp_netif_init()); |
| 65 | + |
| 66 | + ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 67 | + esp_netif_create_default_wifi_sta(); |
| 68 | + |
| 69 | + wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); |
| 70 | + ESP_ERROR_CHECK(esp_wifi_init(&config)); |
| 71 | + |
| 72 | + esp_event_handler_instance_t handler_any_id; |
| 73 | + esp_event_handler_instance_t handler_got_ip; |
| 74 | + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, |
| 75 | + ESP_EVENT_ANY_ID, |
| 76 | + &event_handler, |
| 77 | + NULL, |
| 78 | + &handler_any_id)); |
| 79 | + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, |
| 80 | + IP_EVENT_STA_GOT_IP, |
| 81 | + &event_handler, |
| 82 | + NULL, |
| 83 | + &handler_got_ip)); |
| 84 | + |
| 85 | + wifi_config_t wifi_config = { |
| 86 | + .sta = { |
| 87 | + .ssid = ESP_WIFI_SSID, |
| 88 | + .password = ESP_WIFI_PASS, |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
| 93 | + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); |
| 94 | + ESP_ERROR_CHECK(esp_wifi_start()); |
| 95 | + |
| 96 | + EventBits_t bits = xEventGroupWaitBits(s_event_group_handler, |
| 97 | + WIFI_CONNECTED_BIT, |
| 98 | + pdFALSE, |
| 99 | + pdFALSE, |
| 100 | + portMAX_DELAY); |
| 101 | + |
| 102 | + if (bits & WIFI_CONNECTED_BIT) |
| 103 | + s_is_wifi_connected = true; |
| 104 | + |
| 105 | + ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, handler_got_ip)); |
| 106 | + ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, handler_any_id)); |
| 107 | + vEventGroupDelete(s_event_group_handler); |
| 108 | +} |
| 109 | + |
| 110 | +void app_main(void) |
| 111 | +{ |
| 112 | + {// set wifi connection |
| 113 | + esp_err_t ret = nvs_flash_init(); |
| 114 | + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
| 115 | + ESP_ERROR_CHECK(nvs_flash_erase()); |
| 116 | + ret = nvs_flash_init(); |
| 117 | + } |
| 118 | + ESP_ERROR_CHECK(ret); |
| 119 | + |
| 120 | + printf("\n[WIFI] Connecting to WiFi...\n"); |
| 121 | + wifi_init_sta(); |
| 122 | + while (!s_is_wifi_connected) |
| 123 | + { |
| 124 | + printf("[WIFI] Waiting for the WiFi to connect\n"); |
| 125 | + sleep(1); |
| 126 | + } |
| 127 | + |
| 128 | + printf("[WIFI] Connected to WiFi\n"); |
| 129 | +} |
| 130 | + |
| 131 | + IP4_ADDR(&server_ip, 192, 168, 1, 199); |
| 132 | + dns_setserver(0, &server_ip); |
| 133 | + |
| 134 | + |
| 135 | + esp_tls_t *tls = esp_tls_init(); |
| 136 | + if(!tls){ |
| 137 | + printf("[TLS] not initialised successfully\n"); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + ESP_ERROR_CHECK(esp_tls_set_global_ca_store(ca_crt_start, ca_crt_end - ca_crt_start)); |
| 142 | + |
| 143 | + esp_tls_cfg_t cfg = { |
| 144 | + .use_global_ca_store = true |
| 145 | + }; |
| 146 | + |
| 147 | + |
| 148 | + esp_tls_conn_new_sync(hostname, strlen(hostname) + 1, 6000, &cfg, tls); |
| 149 | + esp_tls_conn_write(tls, "some encrypted data", strlen("some encrypted data")); |
| 150 | + |
| 151 | + esp_tls_conn_destroy(tls); |
| 152 | + |
| 153 | + printf("bye ;)\n"); |
| 154 | +} |
0 commit comments