Last piece to connect client to provider: actually downloading the image and displaying it
This commit is contained in:
+44
-35
@@ -37,41 +37,43 @@ extern "C" void app_main()
|
||||
|
||||
setup_led();
|
||||
|
||||
static uint8 display_buffer[96000];
|
||||
bool received_from_provider = false;
|
||||
|
||||
// Connect to WiFi
|
||||
if (false)
|
||||
ESP_LOGI(TAG, "Initializing WiFi connection");
|
||||
initialize_network();
|
||||
|
||||
esp_err_t err = connect_wifi(CONFIG_CALENDINK_WIFI_SSID,
|
||||
CONFIG_CALENDINK_WIFI_PASSWORD, false);
|
||||
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing WiFi connection");
|
||||
initialize_network();
|
||||
|
||||
esp_err_t err = connect_wifi(CONFIG_CALENDINK_WIFI_SSID,
|
||||
CONFIG_CALENDINK_WIFI_PASSWORD, false);
|
||||
|
||||
if (err == ESP_OK)
|
||||
uint8_t retries = 1;
|
||||
do
|
||||
{
|
||||
uint8_t retries = 1;
|
||||
do
|
||||
err = check_wifi_connection(retries);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
err = check_wifi_connection(retries);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "WiFi connection check timeout, retrying... (%d)",
|
||||
retries);
|
||||
led_blink_number(3, 255, 0, 0);
|
||||
}
|
||||
retries++;
|
||||
} while (err == ESP_ERR_TIMEOUT &&
|
||||
retries <= CONFIG_CALENDINK_WIFI_RETRIES);
|
||||
}
|
||||
ESP_LOGW(TAG, "WiFi connection check timeout, retrying... (%d)",
|
||||
retries);
|
||||
led_blink_number(3, 255, 0, 0);
|
||||
}
|
||||
retries++;
|
||||
} while (err == ESP_ERR_TIMEOUT &&
|
||||
retries <= CONFIG_CALENDINK_WIFI_RETRIES);
|
||||
}
|
||||
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
ESP_LOGI(TAG, "Successfully connected to WiFi!");
|
||||
test_provider_communication();
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to connect to WiFi.");
|
||||
}
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
ESP_LOGI(TAG, "Successfully connected to WiFi!");
|
||||
ESP_LOGI(TAG, "Fetching screen from Provider...");
|
||||
received_from_provider = test_provider_communication(display_buffer, sizeof(display_buffer));
|
||||
ESP_LOGI(TAG, "Provider result: %s", received_from_provider ? "SUCCESS" : "FAILED");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to connect to WiFi.");
|
||||
}
|
||||
|
||||
turn_off_led();
|
||||
@@ -79,21 +81,28 @@ extern "C" void app_main()
|
||||
ESP_LOGI(TAG, "Initializing EPD");
|
||||
epd_init();
|
||||
epd_init_display(true);
|
||||
// epd_clear(epd_color::WHITE);
|
||||
epd_draw_bitmap_grayscale(epd_color::WHITE, gImage_4G1);
|
||||
|
||||
if (received_from_provider) {
|
||||
ESP_LOGI(TAG, "Drawing image from Provider");
|
||||
epd_draw_bitmap_grayscale(epd_color::WHITE, display_buffer);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Drawing fallback test image");
|
||||
epd_draw_bitmap_grayscale(epd_color::WHITE, gImage_4G1);
|
||||
}
|
||||
|
||||
epd_refresh();
|
||||
epd_shutdown_display();
|
||||
|
||||
if (false)
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
disconnect_wifi();
|
||||
shutdown_network();
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Waiting 5 seconds before deep sleep...");
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
vTaskDelay(pdMS_TO_TICKS(15000));
|
||||
|
||||
ESP_LOGI(TAG, "Entering Deep Sleep for 60 seconds...");
|
||||
esp_sleep_enable_timer_wakeup(60ULL * 1000000ULL);
|
||||
esp_sleep_enable_timer_wakeup(30ULL * 1000000ULL);
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
|
||||
+58
-38
@@ -32,15 +32,19 @@ static bool resolve_provider_ip(char *out_ip, size_t out_ip_len)
|
||||
|
||||
{
|
||||
esp_ip4_addr_t addr = {};
|
||||
err = mdns_query_a(CONFIG_CALENDINK_PROVIDER_MDNS_HOSTNAME, 5000, &addr);
|
||||
if (err == ESP_OK)
|
||||
constexpr int kMaxRetries = 3;
|
||||
for (int attempt = 1; attempt <= kMaxRetries; attempt++)
|
||||
{
|
||||
snprintf(out_ip, out_ip_len, IPSTR, IP2STR(&addr));
|
||||
ESP_LOGI(TAG, "Provider resolved: %s", out_ip);
|
||||
return true;
|
||||
err = mdns_query_a(CONFIG_CALENDINK_PROVIDER_MDNS_HOSTNAME, 5000, &addr);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
snprintf(out_ip, out_ip_len, IPSTR, IP2STR(&addr));
|
||||
ESP_LOGI(TAG, "Provider resolved: %s (attempt %d)", out_ip, attempt);
|
||||
return true;
|
||||
}
|
||||
ESP_LOGW(TAG, "mDNS attempt %d/%d failed: %s", attempt, kMaxRetries,
|
||||
esp_err_to_name(err));
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "mDNS resolution failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
fallback:
|
||||
@@ -57,13 +61,15 @@ fallback:
|
||||
|
||||
// ── Provider Communication Test ─────────────────────────────────────────────
|
||||
|
||||
void test_provider_communication(void)
|
||||
bool test_provider_communication(uint8 *out_buffer, size_t buffer_size)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
// 1. Resolve Provider IP
|
||||
char provider_ip[16] = {};
|
||||
if (!resolve_provider_ip(provider_ip, sizeof(provider_ip)))
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t provider_port = CONFIG_CALENDINK_PROVIDER_PORT;
|
||||
@@ -74,7 +80,7 @@ void test_provider_communication(void)
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to get WiFi MAC: %s", esp_err_to_name(err));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
char mac_str[18] = {};
|
||||
@@ -84,59 +90,73 @@ void test_provider_communication(void)
|
||||
ESP_LOGI(TAG, "Client MAC: %s", mac_str);
|
||||
|
||||
// 3. Register with Provider: POST /api/devices/register
|
||||
// This may return "already_registered" — that's fine, we continue regardless.
|
||||
{
|
||||
char *url =
|
||||
http_build_url(provider_ip, provider_port, "/api/devices/register");
|
||||
if (url == nullptr)
|
||||
if (url != nullptr)
|
||||
{
|
||||
return;
|
||||
char json_body[64] = {};
|
||||
snprintf(json_body, sizeof(json_body), "{\"mac\":\"%s\"}", mac_str);
|
||||
|
||||
http_text_response_t resp = {};
|
||||
err = http_post_json(url, json_body, &resp);
|
||||
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
ESP_LOGI(TAG, "Register response (%d): %s", resp.status_code,
|
||||
resp.body ? resp.body : "(empty)");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "Register request failed: %s (continuing anyway)",
|
||||
esp_err_to_name(err));
|
||||
}
|
||||
|
||||
free(resp.body);
|
||||
free(url);
|
||||
}
|
||||
|
||||
char json_body[64] = {};
|
||||
snprintf(json_body, sizeof(json_body), "{\"mac\":\"%s\"}", mac_str);
|
||||
|
||||
http_text_response_t resp = {};
|
||||
err = http_post_json(url, json_body, &resp);
|
||||
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
ESP_LOGI(TAG, "Register response (%d): %s", resp.status_code,
|
||||
resp.body ? resp.body : "(empty)");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Register request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
free(resp.body);
|
||||
free(url);
|
||||
}
|
||||
|
||||
// 4. Fetch screen image: GET /api/devices/screen.png?mac=XX:XX:XX:XX:XX:XX
|
||||
// 4. Fetch screen bitmap: GET /api/devices/screen.bin?mac=XX:XX:XX:XX:XX:XX
|
||||
{
|
||||
char path[80] = {};
|
||||
snprintf(path, sizeof(path), "/api/devices/screen.png?mac=%s", mac_str);
|
||||
snprintf(path, sizeof(path), "/api/devices/screen.bin?mac=%s", mac_str);
|
||||
|
||||
char *url = http_build_url(provider_ip, provider_port, path);
|
||||
if (url == nullptr)
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
http_binary_response_t resp = {};
|
||||
err = http_get_binary(url, &resp);
|
||||
|
||||
if (err == ESP_OK)
|
||||
if (err == ESP_OK && resp.status_code == 200)
|
||||
{
|
||||
ESP_LOGI(TAG, "Screen image response (%d): %zu bytes", resp.status_code,
|
||||
resp.data_len);
|
||||
ESP_LOGI(TAG, "Screen bitmap response: %zu bytes", resp.data_len);
|
||||
if (resp.data != nullptr && resp.data_len > 0)
|
||||
{
|
||||
size_t copy_size = (resp.data_len < buffer_size) ? resp.data_len : buffer_size;
|
||||
memcpy(out_buffer, resp.data, copy_size);
|
||||
success = true;
|
||||
|
||||
// Debug: log first 10 bytes (should be 0xFF for white top-left pixels)
|
||||
ESP_LOGI(TAG, "First 10 bytes: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
|
||||
out_buffer[0], out_buffer[1], out_buffer[2], out_buffer[3],
|
||||
out_buffer[4], out_buffer[5], out_buffer[6], out_buffer[7],
|
||||
out_buffer[8], out_buffer[9]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Screen image request failed: %s", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Screen bitmap request failed: %s (status %d)",
|
||||
esp_err_to_name(err), resp.status_code);
|
||||
}
|
||||
|
||||
free(resp.data);
|
||||
free(url);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.hpp"
|
||||
#include <cstddef>
|
||||
|
||||
// Resolve the Provider's IP and run the device registration + screen fetch
|
||||
// test flow. Call once after WiFi is connected.
|
||||
void test_provider_communication(void);
|
||||
bool test_provider_communication(uint8 *out_buffer, size_t buffer_size);
|
||||
|
||||
Reference in New Issue
Block a user