|
6 | 6 | software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
7 | 7 | CONDITIONS OF ANY KIND, either express or implied.
|
8 | 8 | */
|
9 |
| -#include <sys/param.h> |
10 | 9 |
|
11 |
| -#include "esp_event.h" |
12 |
| -#include "esp_log.h" |
13 |
| -#include "esp_netif.h" |
14 |
| -#include "esp_system.h" |
15 |
| -#include "freertos/FreeRTOS.h" |
16 |
| -#include "freertos/task.h" |
17 |
| -#include "nvs.h" |
18 |
| -#include "nvs_flash.h" |
19 |
| -#include "protocol_examples_common.h" |
| 10 | +#include <esp_wifi.h> |
| 11 | +#include <esp_event_loop.h> |
| 12 | +#include <esp_log.h> |
| 13 | +#include <esp_system.h> |
| 14 | +#include <nvs_flash.h> |
| 15 | +#include <sys/param.h> |
20 | 16 |
|
21 | 17 | #include <esp_http_server.h>
|
22 | 18 |
|
23 |
| -static const char *TAG = "APP"; |
24 |
| - |
25 |
| -esp_err_t hello_get_handler(httpd_req_t *req) { |
26 |
| - /* Send response with custom headers and body set as the |
27 |
| - * string passed in user context*/ |
28 |
| - const char *resp_str = (const char *)req->user_ctx; |
29 |
| - httpd_resp_send(req, resp_str, strlen(resp_str)); |
30 |
| - |
31 |
| - /* After sending the HTTP response the old HTTP request |
32 |
| - * headers are lost. Check if HTTP request headers can be read now. */ |
33 |
| - if (httpd_req_get_hdr_value_len(req, "Host") == 0) { |
34 |
| - ESP_LOGI(TAG, "Request headers lost"); |
35 |
| - } |
| 19 | +#include "memfault/components.h" |
36 | 20 |
|
37 |
| - return ESP_OK; |
| 21 | +/* A simple example that demonstrates how to create GET and POST |
| 22 | + * handlers for the web server. |
| 23 | + * The examples use simple WiFi configuration that you can set via |
| 24 | + * 'make menuconfig'. |
| 25 | + * If you'd rather not, just change the below entries to strings |
| 26 | + * with the config you want - |
| 27 | + * ie. #define EXAMPLE_WIFI_SSID "mywifissid" |
| 28 | +*/ |
| 29 | +#define EXAMPLE_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID |
| 30 | +#define EXAMPLE_WIFI_PASS CONFIG_EXAMPLE_WIFI_PASSWORD |
| 31 | + |
| 32 | +static const char *TAG="TOASTBOARD"; |
| 33 | + |
| 34 | +/* An HTTP GET handler */ |
| 35 | +esp_err_t hello_get_handler(httpd_req_t *req) |
| 36 | +{ |
| 37 | + /* Send response with custom headers and body set as the |
| 38 | + * string passed in user context*/ |
| 39 | + const char* resp_str = (const char*) req->user_ctx; |
| 40 | + httpd_resp_send(req, resp_str, strlen(resp_str)); |
| 41 | + |
| 42 | + /* After sending the HTTP response the old HTTP request |
| 43 | + * headers are lost. Check if HTTP request headers can be read now. */ |
| 44 | + if (httpd_req_get_hdr_value_len(req, "Host") == 0) { |
| 45 | + ESP_LOGI(TAG, "Request headers lost"); |
| 46 | + } |
| 47 | + return ESP_OK; |
38 | 48 | }
|
39 | 49 |
|
40 |
| -httpd_uri_t hello = {.uri = "/hello", |
41 |
| - .method = HTTP_GET, |
42 |
| - .handler = hello_get_handler, |
43 |
| - /* Let's pass response string in user |
44 |
| - * context to demonstrate it's usage */ |
45 |
| - .user_ctx = "Hello World!"}; |
46 |
| - |
47 |
| -httpd_handle_t start_webserver(void) { |
48 |
| - httpd_handle_t server = NULL; |
49 |
| - httpd_config_t config = HTTPD_DEFAULT_CONFIG(); |
50 |
| - |
51 |
| - // Start the httpd server |
52 |
| - ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port); |
53 |
| - if (httpd_start(&server, &config) == ESP_OK) { |
54 |
| - // Set URI handlers |
55 |
| - ESP_LOGI(TAG, "Registering URI handlers"); |
56 |
| - httpd_register_uri_handler(server, &hello); |
57 |
| - return server; |
58 |
| - } |
59 |
| - |
60 |
| - ESP_LOGI(TAG, "Error starting server!"); |
61 |
| - return NULL; |
| 50 | +httpd_uri_t hello = { |
| 51 | + .uri = "/hello", |
| 52 | + .method = HTTP_GET, |
| 53 | + .handler = hello_get_handler, |
| 54 | + /* Let's pass response string in user |
| 55 | + * context to demonstrate it's usage */ |
| 56 | + .user_ctx = "Hello World!" |
| 57 | +}; |
| 58 | + |
| 59 | +httpd_handle_t start_webserver(void) |
| 60 | +{ |
| 61 | + httpd_handle_t server = NULL; |
| 62 | + httpd_config_t config = HTTPD_DEFAULT_CONFIG(); |
| 63 | + |
| 64 | + // Start the httpd server |
| 65 | + ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port); |
| 66 | + if (httpd_start(&server, &config) == ESP_OK) { |
| 67 | + // Set URI handlers |
| 68 | + ESP_LOGI(TAG, "Registering URI handlers"); |
| 69 | + httpd_register_uri_handler(server, &hello); |
| 70 | + return server; |
| 71 | + } |
| 72 | + |
| 73 | + ESP_LOGI(TAG, "Error starting server!"); |
| 74 | + return NULL; |
62 | 75 | }
|
63 | 76 |
|
64 |
| -void stop_webserver(httpd_handle_t server) { |
65 |
| - // Stop the httpd server |
66 |
| - httpd_stop(server); |
| 77 | +void stop_webserver(httpd_handle_t server) |
| 78 | +{ |
| 79 | + // Stop the httpd server |
| 80 | + httpd_stop(server); |
67 | 81 | }
|
68 | 82 |
|
69 |
| -static httpd_handle_t server = NULL; |
70 |
| - |
71 |
| -static void disconnect_handler(void *arg, esp_event_base_t event_base, |
72 |
| - int32_t event_id, void *event_data) { |
73 |
| - httpd_handle_t *server = (httpd_handle_t *)arg; |
74 |
| - if (*server) { |
75 |
| - ESP_LOGI(TAG, "Stopping webserver"); |
76 |
| - stop_webserver(*server); |
77 |
| - *server = NULL; |
78 |
| - } |
| 83 | +static esp_err_t event_handler(void *ctx, system_event_t *event) |
| 84 | +{ |
| 85 | + httpd_handle_t *server = (httpd_handle_t *) ctx; |
| 86 | + /* For accessing reason codes in case of disconnection */ |
| 87 | + system_event_info_t *info = &event->event_info; |
| 88 | + |
| 89 | + switch(event->event_id) { |
| 90 | + case SYSTEM_EVENT_STA_START: |
| 91 | + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); |
| 92 | + ESP_ERROR_CHECK(esp_wifi_connect()); |
| 93 | + break; |
| 94 | + case SYSTEM_EVENT_STA_GOT_IP: |
| 95 | + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); |
| 96 | + ESP_LOGI(TAG, "Got IP: '%s'", |
| 97 | + ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); |
| 98 | + |
| 99 | + /* Start the web server */ |
| 100 | + if (*server == NULL) { |
| 101 | + *server = start_webserver(); |
| 102 | + } |
| 103 | + break; |
| 104 | + case SYSTEM_EVENT_STA_DISCONNECTED: |
| 105 | + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); |
| 106 | + ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); |
| 107 | + if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { |
| 108 | + /*Switch to 802.11 bgn mode */ |
| 109 | + esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); |
| 110 | + } |
| 111 | + ESP_ERROR_CHECK(esp_wifi_connect()); |
| 112 | + |
| 113 | + /* Stop the web server */ |
| 114 | + if (*server) { |
| 115 | + stop_webserver(*server); |
| 116 | + *server = NULL; |
| 117 | + } |
| 118 | + break; |
| 119 | + default: |
| 120 | + break; |
| 121 | + } |
| 122 | + return ESP_OK; |
79 | 123 | }
|
80 | 124 |
|
81 |
| -static void connect_handler(void *arg, esp_event_base_t event_base, |
82 |
| - int32_t event_id, void *event_data) { |
83 |
| - httpd_handle_t *server = (httpd_handle_t *)arg; |
84 |
| - if (*server == NULL) { |
85 |
| - ESP_LOGI(TAG, "Starting webserver"); |
86 |
| - *server = start_webserver(); |
87 |
| - } |
| 125 | +static void initialise_wifi(void *arg) |
| 126 | +{ |
| 127 | + tcpip_adapter_init(); |
| 128 | + ESP_ERROR_CHECK(esp_event_loop_init(event_handler, arg)); |
| 129 | + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 130 | + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); |
| 131 | + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
| 132 | + wifi_config_t wifi_config = { |
| 133 | + .sta = { |
| 134 | + .ssid = EXAMPLE_WIFI_SSID, |
| 135 | + .password = EXAMPLE_WIFI_PASS, |
| 136 | + }, |
| 137 | + }; |
| 138 | + ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); |
| 139 | + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
| 140 | + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); |
| 141 | + ESP_ERROR_CHECK(esp_wifi_start()); |
88 | 142 | }
|
89 | 143 |
|
90 |
| -void app_main() { |
91 |
| - ESP_ERROR_CHECK(nvs_flash_init()); |
92 |
| - ESP_ERROR_CHECK(esp_netif_init()); |
93 |
| - ESP_ERROR_CHECK(esp_event_loop_create_default()); |
94 |
| - |
95 |
| - ESP_ERROR_CHECK(example_connect()); |
96 |
| - |
97 |
| - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, |
98 |
| - &connect_handler, &server)); |
99 |
| - ESP_ERROR_CHECK(esp_event_handler_register( |
100 |
| - WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); |
| 144 | +void app_main() |
| 145 | +{ |
| 146 | + memfault_platform_boot(); |
101 | 147 |
|
102 |
| - server = start_webserver(); |
| 148 | + static httpd_handle_t server = NULL; |
| 149 | + ESP_ERROR_CHECK(nvs_flash_init()); |
| 150 | + initialise_wifi(&server); |
103 | 151 | }
|
0 commit comments