diff options
| Commit | 7c9c68eaba5ed7c75495eb6607a8155110c2df43 (patch) | |
|---|---|---|
| Author | 2024-05-27 13:18:32 +0300 | |
| Committer | 2024-05-27 13:18:32 +0300 | |
| Files | 14967cc55390b7d39f70482c6a7102fb776688e9 | |
| uni-projects-7c9c68eaba5ed7c75495eb6607a8155110c2df43.tar uni-projects-7c9c68eaba5ed7c75495eb6607a8155110c2df43.tar.gz uni-projects-7c9c68eaba5ed7c75495eb6607a8155110c2df43.zip | ||
| Parent | 06e659dec139aa0d08d5fff433e9ffdec0a736b5 (diff) | |
[iot/goam] Implemented storing and setting of static network data
Transitioning from #defines to using the NVS memory
6 files changed, 139 insertions, 6 deletions
| -rw-r--r-- | Year 3/Internet of things/goam/main/nvs.c | 55 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/nvs.h | 2 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/webserver.c | 41 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/wifi.c | 31 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/wifi.h | 3 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/webserver-src/index.html | 13 |
diff --git a/Year 3/Internet of things/goam/main/nvs.c b/Year 3/Internet of things/goam/main/nvs.c index add6e69..6f894db 100644 --- a/Year 3/Internet of things/goam/main/nvs.c +++ b/Year 3/Internet of things/goam/main/nvs.c @@ -1,18 +1,65 @@ #include "nvs_flash.h" #include "esp_log.h" +#include <string.h> #include "nvs.h" +#define NVS_GOAM_NAMESPACE "goam" + static const char *TAG = "nvs"; -void nvs_init() { - esp_err_t ret = nvs_flash_init(); +static void nvs_init_partition(const char* partition) { + esp_err_t ret = nvs_flash_init_partition(partition); 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(nvs_flash_erase_partition(partition)); + ret = nvs_flash_init_partition(partition); } ESP_ERROR_CHECK(ret); +} + +void nvs_init() { + nvs_init_partition("nvs"); ESP_LOGI(TAG, "NVS initialized"); } + +void nvs_write(const char* key, const char* value) { + nvs_handle_t outHandle; + ESP_ERROR_CHECK(nvs_open(NVS_GOAM_NAMESPACE, NVS_READWRITE, &outHandle)); + + ESP_ERROR_CHECK(nvs_set_str(outHandle, key, value)); + + ESP_ERROR_CHECK(nvs_commit(outHandle)); + nvs_close(outHandle); +} + +void nvs_read(const char* key, char** output) { + nvs_handle_t inHandle; + esp_err_t err = nvs_open(NVS_GOAM_NAMESPACE, NVS_READONLY, &inHandle); + if (err == ESP_ERR_NVS_NOT_FOUND) { + *output = NULL; + nvs_close(inHandle); + return; + } + ESP_ERROR_CHECK(err); + + size_t size = 0; + err = nvs_get_str(inHandle, key, NULL, &size); + if (err != ESP_OK) { + *output = NULL; + nvs_close(inHandle); + return; + } + if (size == 0) { + *output = NULL; + nvs_close(inHandle); + return; + } + + free(*output); + *output = malloc(size * sizeof(char)); + ESP_ERROR_CHECK(nvs_get_str(inHandle, key, *output, &size)); + + nvs_close(inHandle); +} diff --git a/Year 3/Internet of things/goam/main/nvs.h b/Year 3/Internet of things/goam/main/nvs.h index b6058a2..12aa477 100644 --- a/Year 3/Internet of things/goam/main/nvs.h +++ b/Year 3/Internet of things/goam/main/nvs.h @@ -1,3 +1,5 @@ #pragma once void nvs_init(); +void nvs_write(const char* key, const char* value); +void nvs_read(const char* key, char** output); diff --git a/Year 3/Internet of things/goam/main/webserver.c b/Year 3/Internet of things/goam/main/webserver.c index 14ada7c..948d394 100644 --- a/Year 3/Internet of things/goam/main/webserver.c +++ b/Year 3/Internet of things/goam/main/webserver.c @@ -14,6 +14,7 @@ #include "webserver.h" #include "spiffs.h" +#include "wifi.h" static const char *TAG = "webserver"; @@ -120,6 +121,45 @@ static const httpd_uri_t get_image = { .user_ctx = NULL }; +static void logi_post(httpd_req_t *req) { + /* After sending the HTTP response the old HTTP request + * headers are lost. Check if HTTP request headers can be read now. */ + if (httpd_req_get_hdr_value_len(req, "Host") == 0) { + ESP_LOGI(TAG, "Processed %s", req->uri); + } + else { + ESP_LOGE(TAG, "Couldn't process %s", req->uri); + } +} + +const char ssid_post_param[] = "ssid"; +const char pass_post_param[] = "pass"; + +static esp_err_t post_network_handler(httpd_req_t *req) { + char* buffer = malloc(req->content_len + 1); + httpd_req_recv(req, buffer, req->content_len); + buffer[req->content_len] = '\0'; // just in case + + char* ssid_start = strstr(buffer, ssid_post_param) + sizeof(ssid_post_param); + char* pass_start = strstr(buffer, pass_post_param) + sizeof(pass_post_param); + char* separator = strchr(buffer, '&'); + *separator = '\0'; + + wifi_store_network(ssid_start, pass_start); + + httpd_resp_send(req, "", 0); + free(buffer); + + logi_post(req); + return ESP_OK; +} +static const httpd_uri_t post_network = { + .uri = "/network", + .method = HTTP_POST, + .handler = post_network_handler, + .user_ctx = NULL +}; + /* * On network connection/disconnect */ @@ -180,6 +220,7 @@ void webserver_start() httpd_register_uri_handler(server, &get_index); httpd_register_uri_handler(server, &get_image); + httpd_register_uri_handler(server, &post_network); ESP_LOGI(TAG, "Started server on port %d", config.server_port); } diff --git a/Year 3/Internet of things/goam/main/wifi.c b/Year 3/Internet of things/goam/main/wifi.c index 316d668..c40c512 100644 --- a/Year 3/Internet of things/goam/main/wifi.c +++ b/Year 3/Internet of things/goam/main/wifi.c @@ -9,6 +9,7 @@ #include "wifi.h" #include "display.h" +#include "nvs.h" #define IP_LINE 2 #define AT_WIFI_ICON 1, 4 @@ -161,9 +162,35 @@ static void wifi_init_events() { } /* + * WiFi settings + */ + +#define NVS_SSID_KEY "ssid" +static char* nvs_ssid = NULL; + +#define NVS_PASS_KEY "pass" +static char* nvs_pass = NULL; + +/* * Public interface */ +void wifi_store_network(const char* valueSSID, const char* valuePass) { + nvs_write(NVS_SSID_KEY, valueSSID); + free(nvs_ssid); + nvs_ssid = malloc((strlen(valueSSID) + 1) * sizeof(char)); + strcpy(nvs_ssid, valueSSID); + + nvs_write(NVS_PASS_KEY, valuePass); + free(nvs_pass); + nvs_pass = malloc((strlen(valuePass) + 1) * sizeof(char)); + strcpy(nvs_pass, valuePass); +} +void wifi_load_network() { + nvs_read(NVS_SSID_KEY, &nvs_ssid); + nvs_read(NVS_PASS_KEY, &nvs_pass); +} + void wifi_init() { ESP_ERROR_CHECK(esp_event_loop_create_default()); @@ -174,6 +201,10 @@ void wifi_init() { wifi_init_events(); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + + wifi_load_network(); + if (nvs_ssid != NULL && nvs_pass != NULL) + printf("%s %s\n", nvs_ssid, nvs_pass); } void wifi_launch() { diff --git a/Year 3/Internet of things/goam/main/wifi.h b/Year 3/Internet of things/goam/main/wifi.h index f4e312a..7f92d4a 100644 --- a/Year 3/Internet of things/goam/main/wifi.h +++ b/Year 3/Internet of things/goam/main/wifi.h @@ -2,3 +2,6 @@ void wifi_init(); void wifi_launch(); + +void wifi_store_network(const char* valueSSID, const char* valuePass); +void wifi_load_network(); diff --git a/Year 3/Internet of things/goam/webserver-src/index.html b/Year 3/Internet of things/goam/webserver-src/index.html index 00c17ef..fad90a9 100644 --- a/Year 3/Internet of things/goam/webserver-src/index.html +++ b/Year 3/Internet of things/goam/webserver-src/index.html @@ -27,12 +27,14 @@ article { img { max-width: calc(100vw - 16px - 2em); } -.button { - display: block; +.button, input { border: 0.25em solid gray; padding: 0.25em; background-color: lightgray; color: black; +} +a.button { + display: block; text-decoration: none; text-align: center; } @@ -46,6 +48,13 @@ img { <h1>Latest image</h1> <img src="/spiffs/capture.jpeg"/> <a class="button full-width" href="/spiffs/capture.jpeg" download="capture.jpeg">Click to Download</a> + + <h1>Configure static network</h1> + <form action="/network" method="POST"> + <input name="ssid" type="text" placeholder="Name (SSID)"> + <input name="pass" type="text" placeholder="Password"> + <input class="button" type="submit" value="Save"> + </form> </article> </body> </html> |
