// WiFi (own access point + optional home network), clock sync, and the web // interface / JSON API. #include #include #include #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "freertos/task.h" #include "cJSON.h" #include "esp_app_desc.h" #include "esp_event.h" #include "esp_heap_caps.h" #include "esp_http_server.h" #include "esp_log.h" #include "esp_netif.h" #include "esp_netif_sntp.h" #include "esp_ota_ops.h" #include "esp_timer.h" #include "esp_wifi.h" #include "nvs.h" #include "app.h" #include "display.h" #define AP_SSID "ESP32-VGA" #define AP_PASS "esp32vga" #define DEFAULT_TZ "NZST-12NZDT,M9.5.0,M4.1.0/3" static const char *TAG = "web"; extern const char index_html_start[] asm("_binary_index_html_start"); extern const char index_html_end[] asm("_binary_index_html_end"); static SemaphoreHandle_t net_lock; static net_info_t net; static char tz[64] = DEFAULT_TZ; static esp_netif_t *sta_netif; static esp_timer_handle_t reconnect_timer; static bool sntp_started; static uint8_t *upload_buf; // --------------------------------------------------------------------------- // Settings static void nvs_get_string(const char *key, char *out, size_t size) { nvs_handle_t h; if (nvs_open("vga", NVS_READONLY, &h) == ESP_OK) { nvs_get_str(h, key, out, &size); nvs_close(h); } } static void nvs_set_string(const char *key, const char *val) { nvs_handle_t h; if (nvs_open("vga", NVS_READWRITE, &h) == ESP_OK) { nvs_set_str(h, key, val); nvs_commit(h); nvs_close(h); } } static void apply_tz(void) { setenv("TZ", tz, 1); tzset(); } // --------------------------------------------------------------------------- // WiFi void net_get_info(net_info_t *out) { if (!net_lock) { // WiFi isn't up yet; web_start() redraws once it is memset(out, 0, sizeof(*out)); return; } xSemaphoreTake(net_lock, portMAX_DELAY); *out = net; xSemaphoreGive(net_lock); } static void reconnect(void *arg) { esp_wifi_connect(); } static void on_wifi_event(void *arg, esp_event_base_t base, int32_t id, void *data) { if (base == WIFI_EVENT && id == WIFI_EVENT_STA_START) { if (net.sta_ssid[0]) { esp_wifi_connect(); } } else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) { bool was = net.sta_connected; xSemaphoreTake(net_lock, portMAX_DELAY); net.sta_connected = false; net.sta_ip[0] = 0; bool have_ssid = net.sta_ssid[0]; xSemaphoreGive(net_lock); if (was) { app_refresh(); } // Retry slowly: each attempt scans, which briefly disturbs the access point if (have_ssid && !esp_timer_is_active(reconnect_timer)) { esp_timer_start_once(reconnect_timer, 10 * 1000000); } } else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t *e = data; xSemaphoreTake(net_lock, portMAX_DELAY); net.sta_connected = true; snprintf(net.sta_ip, sizeof(net.sta_ip), IPSTR, IP2STR(&e->ip_info.ip)); xSemaphoreGive(net_lock); ESP_LOGI(TAG, "home WiFi connected: http://%s", net.sta_ip); if (!sntp_started) { esp_sntp_config_t cfg = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org"); esp_netif_sntp_init(&cfg); sntp_started = true; } app_refresh(); } } static void set_sta(const char *ssid, const char *pass) { wifi_config_t sta = {0}; strlcpy((char *)sta.sta.ssid, ssid, sizeof(sta.sta.ssid)); strlcpy((char *)sta.sta.password, pass, sizeof(sta.sta.password)); sta.sta.threshold.authmode = pass[0] ? WIFI_AUTH_WPA_PSK : WIFI_AUTH_OPEN; esp_wifi_set_config(WIFI_IF_STA, &sta); } static void wifi_init(void) { ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); esp_netif_create_default_wifi_ap(); sta_netif = esp_netif_create_default_wifi_sta(); wifi_init_config_t init = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&init)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, on_wifi_event, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, on_wifi_event, NULL)); const esp_timer_create_args_t targs = {.callback = reconnect, .name = "reconnect"}; esp_timer_create(&targs, &reconnect_timer); wifi_config_t ap = { .ap = { .ssid = AP_SSID, .ssid_len = strlen(AP_SSID), .password = AP_PASS, .channel = 6, .max_connection = 4, .authmode = WIFI_AUTH_WPA2_PSK, }, }; char pass[65] = ""; nvs_get_string("sta_ssid", net.sta_ssid, sizeof(net.sta_ssid)); nvs_get_string("sta_pass", pass, sizeof(pass)); strcpy(net.ap_ssid, AP_SSID); strcpy(net.ap_pass, AP_PASS); strcpy(net.ap_ip, "192.168.4.1"); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap)); set_sta(net.sta_ssid, pass); // net is complete; from here on the event handler and display share it net_lock = xSemaphoreCreateMutex(); ESP_ERROR_CHECK(esp_wifi_start()); // Modem sleep adds up to ~200 ms latency and slows uploads; this is mains powered esp_wifi_set_ps(WIFI_PS_NONE); ESP_LOGI(TAG, "access point \"%s\" up: http://%s", AP_SSID, net.ap_ip); } // --------------------------------------------------------------------------- // HTTP helpers static esp_err_t send_json(httpd_req_t *req, cJSON *root) { char *s = cJSON_PrintUnformatted(root); cJSON_Delete(root); httpd_resp_set_type(req, "application/json"); esp_err_t err = httpd_resp_sendstr(req, s); free(s); return err; } static esp_err_t send_ok(httpd_req_t *req) { httpd_resp_set_type(req, "application/json"); return httpd_resp_sendstr(req, "{\"ok\":true}"); } static esp_err_t send_error(httpd_req_t *req, const char *status, const char *msg) { httpd_resp_set_status(req, status); httpd_resp_set_type(req, "text/plain"); return httpd_resp_sendstr(req, msg); } // Read a small JSON body. Caller frees with cJSON_Delete. static cJSON *read_json(httpd_req_t *req) { if (req->content_len <= 0 || req->content_len > 2048) { return NULL; } char *buf = malloc(req->content_len + 1); int got = 0; while (got < req->content_len) { int n = httpd_req_recv(req, buf + got, req->content_len - got); if (n <= 0) { free(buf); return NULL; } got += n; } buf[got] = 0; cJSON *root = cJSON_Parse(buf); free(buf); return root; } static const char *json_str(cJSON *root, const char *key, const char *def) { cJSON *v = cJSON_GetObjectItem(root, key); return cJSON_IsString(v) ? v->valuestring : def; } static int json_int(cJSON *root, const char *key, int def) { cJSON *v = cJSON_GetObjectItem(root, key); return cJSON_IsNumber(v) ? v->valueint : def; } static void url_decode(char *s) { char *o = s; for (; *s; s++) { if (*s == '+') { *o++ = ' '; } else if (*s == '%' && s[1] && s[2]) { char hex[3] = {s[1], s[2], 0}; *o++ = strtol(hex, NULL, 16); s += 2; } else { *o++ = *s; } } *o = 0; } static bool query_param(httpd_req_t *req, const char *key, char *out, size_t size) { char q[256]; if (httpd_req_get_url_query_str(req, q, sizeof(q)) != ESP_OK || httpd_query_key_value(q, key, out, size) != ESP_OK) { return false; } url_decode(out); return true; } static void colour_hex(uint8_t c, char out[8]) { snprintf(out, 8, "#%02x%02x%02x", (c >> 5) * 255 / 7, ((c >> 2) & 7) * 255 / 7, (c & 3) * 255 / 3); } static uint8_t parse_colour(const char *hex, uint8_t def) { if (!hex || hex[0] != '#' || strlen(hex) != 7) { return def; } unsigned long v = strtoul(hex + 1, NULL, 16); return rgb888_to_332(v >> 16, (v >> 8) & 255, v & 255); } // --------------------------------------------------------------------------- // Handlers static esp_err_t h_index(httpd_req_t *req) { httpd_resp_set_type(req, "text/html"); return httpd_resp_send(req, index_html_start, index_html_end - index_html_start); } static esp_err_t h_state(httpd_req_t *req) { show_t s; net_info_t n; app_get_show(&s); net_get_info(&n); cJSON *root = cJSON_CreateObject(); cJSON_AddNumberToObject(root, "w", W); cJSON_AddNumberToObject(root, "h", H); cJSON_AddNumberToObject(root, "hz", REFRESH_HZ); cJSON_AddStringToObject(root, "content", content_key(s.content)); cJSON_AddNumberToObject(root, "slot", s.slot); cJSON *list = cJSON_AddArrayToObject(root, "contents"); for (int i = 0; i < CONTENT_COUNT; i++) { cJSON *c = cJSON_CreateObject(); cJSON_AddStringToObject(c, "key", content_key(i)); cJSON_AddStringToObject(c, "name", content_name(i)); cJSON_AddItemToArray(list, c); } char hex[8]; cJSON *msg = cJSON_AddObjectToObject(root, "msg"); cJSON_AddStringToObject(msg, "text", s.msg.text); colour_hex(s.msg.fg, hex); cJSON_AddStringToObject(msg, "fg", hex); colour_hex(s.msg.bg, hex); cJSON_AddStringToObject(msg, "bg", hex); cJSON_AddNumberToObject(msg, "size", s.msg.size); cJSON *slots = cJSON_AddArrayToObject(root, "slots"); for (int i = 0; i < gallery_slots(); i++) { char name[GALLERY_NAME_MAX + 1]; cJSON_AddItemToArray(slots, gallery_name(i, name) ? cJSON_CreateString(name) : cJSON_CreateNull()); } cJSON *jn = cJSON_AddObjectToObject(root, "net"); cJSON_AddStringToObject(jn, "ap_ssid", n.ap_ssid); cJSON_AddStringToObject(jn, "ap_ip", n.ap_ip); cJSON_AddStringToObject(jn, "sta_ssid", n.sta_ssid); cJSON_AddStringToObject(jn, "sta_ip", n.sta_ip); cJSON_AddBoolToObject(jn, "sta_connected", n.sta_connected); cJSON_AddStringToObject(root, "tz", tz); cJSON_AddNumberToObject(root, "time", (double)time(NULL)); cJSON_AddStringToObject(root, "version", esp_app_get_description()->version); cJSON_AddStringToObject(root, "built", esp_app_get_description()->date); cJSON_AddStringToObject(root, "partition", esp_ota_get_running_partition()->label); return send_json(req, root); } // Raw W*H RGB332 of what's on screen static esp_err_t h_screenshot(httpd_req_t *req) { // Reuses the upload buffer: handlers run one at a time if (!upload_buf) { upload_buf = heap_caps_malloc(W * H, MALLOC_CAP_SPIRAM); } if (!display_snapshot(upload_buf)) { return send_error(req, "503 Service Unavailable", "display not running"); } const uint8_t *fb = upload_buf; httpd_resp_set_type(req, "application/octet-stream"); for (int off = 0; off < W * H; off += 8192) { if (httpd_resp_send_chunk(req, (const char *)fb + off, MIN(8192, W * H - off)) != ESP_OK) { return ESP_FAIL; } } return httpd_resp_send_chunk(req, NULL, 0); } static esp_err_t h_show(httpd_req_t *req) { cJSON *root = read_json(req); if (!root) { return send_error(req, "400 Bad Request", "bad JSON"); } int c = content_from_key(json_str(root, "content", "")); int slot = json_int(root, "slot", 0); cJSON_Delete(root); if (c < 0) { return send_error(req, "400 Bad Request", "unknown content"); } if (c == CONTENT_IMAGE && (slot < SLOT_LIVE || slot >= gallery_slots())) { return send_error(req, "400 Bad Request", "bad slot"); } if (c != CONTENT_IMAGE) { show_t s; app_get_show(&s); slot = s.slot; // remember the last image slot } app_show(c, slot); return send_ok(req); } static esp_err_t h_message(httpd_req_t *req) { cJSON *root = read_json(req); if (!root) { return send_error(req, "400 Bad Request", "bad JSON"); } show_t s; app_get_show(&s); message_t m = s.msg; strlcpy(m.text, json_str(root, "text", m.text), sizeof(m.text)); m.fg = parse_colour(json_str(root, "fg", NULL), m.fg); m.bg = parse_colour(json_str(root, "bg", NULL), m.bg); m.size = MIN(json_int(root, "size", m.size), MSG_SIZE_FILL); cJSON_Delete(root); app_set_message(&m); return send_ok(req); } // Body: exactly W*H bytes of RGB332, converted in the browser static esp_err_t h_image_upload(httpd_req_t *req) { char arg[48]; int slot = query_param(req, "slot", arg, sizeof(arg)) ? atoi(arg) : SLOT_LIVE; char name[GALLERY_NAME_MAX + 1] = "Image"; if (query_param(req, "name", arg, sizeof(arg)) && arg[0]) { strlcpy(name, arg, sizeof(name)); } if (slot < SLOT_LIVE || slot >= gallery_slots()) { return send_error(req, "400 Bad Request", "bad slot"); } if (req->content_len != W * H) { char msg[64]; snprintf(msg, sizeof(msg), "expected %d bytes for %dx%d", W * H, W, H); return send_error(req, "400 Bad Request", msg); } if (!upload_buf) { upload_buf = heap_caps_malloc(W * H, MALLOC_CAP_SPIRAM); } int got = 0; while (got < W * H) { int n = httpd_req_recv(req, (char *)upload_buf + got, W * H - got); if (n == HTTPD_SOCK_ERR_TIMEOUT) { continue; } if (n <= 0) { return ESP_FAIL; } got += n; } if (slot == SLOT_LIVE) { // Swap buffers rather than copy; the old live image becomes the next upload buffer live_image_lock(); uint8_t *old = live_image; live_image = upload_buf; upload_buf = old; live_image_unlock(); } else if (!gallery_write(slot, name, upload_buf)) { return send_error(req, "500 Internal Server Error", "flash write failed"); } app_show(CONTENT_IMAGE, slot); return send_ok(req); } static esp_err_t h_image_delete(httpd_req_t *req) { char arg[8]; int slot = query_param(req, "slot", arg, sizeof(arg)) ? atoi(arg) : -2; if (!gallery_delete(slot)) { return send_error(req, "400 Bad Request", "bad slot"); } show_t s; app_get_show(&s); if (s.content == CONTENT_IMAGE && s.slot == slot) { app_refresh(); } return send_ok(req); } static esp_err_t h_wifi(httpd_req_t *req) { cJSON *root = read_json(req); if (!root) { return send_error(req, "400 Bad Request", "bad JSON"); } char ssid[33], pass[65]; strlcpy(ssid, json_str(root, "ssid", ""), sizeof(ssid)); strlcpy(pass, json_str(root, "pass", ""), sizeof(pass)); cJSON_Delete(root); nvs_set_string("sta_ssid", ssid); nvs_set_string("sta_pass", pass); xSemaphoreTake(net_lock, portMAX_DELAY); strcpy(net.sta_ssid, ssid); xSemaphoreGive(net_lock); // Reply before reconnecting: the scan can briefly interrupt the access point send_ok(req); esp_timer_stop(reconnect_timer); esp_wifi_disconnect(); set_sta(ssid, pass); if (ssid[0]) { esp_wifi_connect(); } app_refresh(); return ESP_OK; } static esp_err_t h_time(httpd_req_t *req) { cJSON *root = read_json(req); if (!root) { return send_error(req, "400 Bad Request", "bad JSON"); } cJSON *epoch = cJSON_GetObjectItem(root, "epoch"); const char *new_tz = json_str(root, "tz", NULL); if (new_tz && new_tz[0]) { strlcpy(tz, new_tz, sizeof(tz)); nvs_set_string("tz", tz); apply_tz(); } // Browser time is only a fallback; prefer NTP once on the home network if (cJSON_IsNumber(epoch) && !(sntp_started && time(NULL) > 1700000000)) { struct timeval tv = {.tv_sec = (time_t)epoch->valuedouble}; settimeofday(&tv, NULL); } cJSON_Delete(root); return send_ok(req); } static void restart_task(void *arg) { vTaskDelay(pdMS_TO_TICKS(1000)); esp_restart(); } // Body: the firmware .bin static esp_err_t h_ota(httpd_req_t *req) { const esp_partition_t *part = esp_ota_get_next_update_partition(NULL); if (!part || req->content_len <= 0 || req->content_len > part->size) { return send_error(req, "400 Bad Request", "firmware too big or missing"); } esp_ota_handle_t ota; if (esp_ota_begin(part, OTA_WITH_SEQUENTIAL_WRITES, &ota) != ESP_OK) { return send_error(req, "500 Internal Server Error", "OTA begin failed"); } char *buf = malloc(4096); int left = req->content_len; esp_err_t err = ESP_OK; while (left > 0 && err == ESP_OK) { int n = httpd_req_recv(req, buf, MIN(left, 4096)); if (n == HTTPD_SOCK_ERR_TIMEOUT) { continue; } if (n <= 0) { err = ESP_FAIL; break; } err = esp_ota_write(ota, buf, n); left -= n; } free(buf); if (err != ESP_OK) { esp_ota_abort(ota); return send_error(req, "500 Internal Server Error", "OTA write failed"); } if (esp_ota_end(ota) != ESP_OK || esp_ota_set_boot_partition(part) != ESP_OK) { return send_error(req, "400 Bad Request", "firmware image is invalid"); } ESP_LOGI(TAG, "firmware written to %s, restarting", part->label); send_ok(req); xTaskCreate(restart_task, "restart", 2048, NULL, 5, NULL); return ESP_OK; } // --------------------------------------------------------------------------- void web_start(void) { nvs_get_string("tz", tz, sizeof(tz)); apply_tz(); wifi_init(); app_refresh(); // show the network info httpd_config_t cfg = HTTPD_DEFAULT_CONFIG(); cfg.stack_size = 8192; cfg.max_uri_handlers = 16; cfg.core_id = 0; cfg.recv_wait_timeout = 30; cfg.lru_purge_enable = true; httpd_handle_t server; ESP_ERROR_CHECK(httpd_start(&server, &cfg)); const httpd_uri_t uris[] = { {.uri = "/", .method = HTTP_GET, .handler = h_index}, {.uri = "/api/state", .method = HTTP_GET, .handler = h_state}, {.uri = "/api/show", .method = HTTP_POST, .handler = h_show}, {.uri = "/api/screenshot", .method = HTTP_GET, .handler = h_screenshot}, {.uri = "/api/message", .method = HTTP_POST, .handler = h_message}, {.uri = "/api/image", .method = HTTP_POST, .handler = h_image_upload}, {.uri = "/api/image", .method = HTTP_DELETE, .handler = h_image_delete}, {.uri = "/api/wifi", .method = HTTP_POST, .handler = h_wifi}, {.uri = "/api/time", .method = HTTP_POST, .handler = h_time}, {.uri = "/api/ota", .method = HTTP_POST, .handler = h_ota}, }; for (int i = 0; i < sizeof(uris) / sizeof(uris[0]); i++) { httpd_register_uri_handler(server, &uris[i]); } }