Skip to content

Commit 7b30b80

Browse files
committed
CPU temp and compile options for size
1 parent eea554d commit 7b30b80

15 files changed

Lines changed: 52 additions & 19 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# in this exact order for cmake to work correctly
33
cmake_minimum_required(VERSION 3.16)
44

5-
set(PROJECT_VER "2.4.9")
5+
set(PROJECT_VER "2.4.10")
66
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
77
project(esp32_nat_router)
88

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Starting from this code base I started several spin-off projects with slightly d
3939
- **OLED Display**: Status display on 72x40 I2C SSD1306 OLEDs (as found on some ESP32-C3 mini boards)
4040
- **MQTT Home Assistant**: Publish telemetry and per-client stats to MQTT with HA auto-discovery
4141
- **MCP Bridge (AI-Ready)**: BETA - Control the router from AI assistants (Claude, etc.) via the Model Context Protocol
42-
- **mDNS**: The router is reachable as `esp32-nat-router.local` via mDNS/Bonjour — no need to look up the IP address (not available on ESP32-C5 due to flash size constraints)
42+
- **mDNS**: The router is reachable as `esp32-nat-router.local` via mDNS/Bonjour — no need to look up the IP address.
4343
- **OTA Updates**: Flash new firmware directly from the Web UI
4444

4545
The maximum number of simultaniously connected WiFi clients is 8 (5 on the ESP32c3) due to RAM limitations (uses about 5KB per client). Each of the features: Web Interface, PCAP Capture, Wireguard VPN, Remote Console, WPA Enterprise and MQTT Home Assistant require several KB of additional RAM. So using all of them at once will probably burst the ESP32's ressources. Unused/disabled features are optimized for minimal to no RAM usage. Have a look at remaining heap size if in doubt.

components/acl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ idf_component_register(
88
REQUIRES lwip esp_netif esp_timer
99
PRIV_REQUIRES nvs_flash cmd_router
1010
)
11+
target_compile_options(${COMPONENT_LIB} PRIVATE -O2)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
idf_component_register(SRCS "cmd_router.c"
22
INCLUDE_DIRS . "${CMAKE_SOURCE_DIR}/include"
33
REQUIRES console nvs_flash esp_wifi driver spi_flash pcap_capture acl remote_console syslog oled_display mbedtls app_update)
4+
target_compile_options(${COMPONENT_LIB} PRIVATE -Os)

components/cmd_router/cmd_router.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
#include "esp_random.h"
3737

3838
#include "driver/gpio.h"
39+
#include "soc/soc_caps.h"
40+
#if SOC_TEMP_SENSOR_SUPPORTED
41+
#include "driver/temperature_sensor.h"
42+
#endif
3943
#include "router_globals.h"
4044
#include "cmd_router.h"
4145
#include "pcap_capture.h"
@@ -1360,18 +1364,39 @@ static int show(int argc, char **argv)
13601364
printf("Uplink IP: none\n");
13611365
}
13621366

1363-
// Byte counts
1364-
printf("Bytes sent/received: %" PRIu64 " / %" PRIu64 " bytes\n", get_sta_bytes_sent(), get_sta_bytes_received());
1365-
1366-
// Free heap
1367-
printf("Free heap: %lu bytes\n", (unsigned long)esp_get_free_heap_size());
1368-
13691367
// AP interface state
13701368
printf("AP interface: %s\n", ap_disabled ? "disabled" : "enabled");
13711369

13721370
// Connected clients
13731371
resync_connect_count();
13741372
printf("Connected clients: %u\n", connect_count);
1373+
1374+
// Byte counts
1375+
char _sent_buf[16], _recv_buf[16];
1376+
format_bytes_human(get_sta_bytes_sent(), _sent_buf, sizeof(_sent_buf));
1377+
format_bytes_human(get_sta_bytes_received(), _recv_buf, sizeof(_recv_buf));
1378+
printf("Bytes sent/received: %s / %s\n", _sent_buf, _recv_buf);
1379+
1380+
1381+
#if SOC_TEMP_SENSOR_SUPPORTED
1382+
// CPU temperature
1383+
{
1384+
temperature_sensor_handle_t tsens;
1385+
temperature_sensor_config_t tsens_cfg = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
1386+
if (temperature_sensor_install(&tsens_cfg, &tsens) == ESP_OK) {
1387+
float celsius = 0.0f;
1388+
temperature_sensor_enable(tsens);
1389+
if (temperature_sensor_get_celsius(tsens, &celsius) == ESP_OK) {
1390+
printf("CPU temperature: %.1f °C\n", celsius);
1391+
}
1392+
temperature_sensor_disable(tsens);
1393+
temperature_sensor_uninstall(tsens);
1394+
}
1395+
}
1396+
#endif
1397+
// Free heap
1398+
printf("Free heap: %lu bytes\n", (unsigned long)esp_get_free_heap_size());
1399+
13751400
if (connect_count > 0) {
13761401
connected_client_t clients[8];
13771402
int count = get_connected_clients(clients, 8);
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
idf_component_register(SRCS "cmd_system.c"
22
INCLUDE_DIRS .
3-
REQUIRES console spi_flash driver nvs_flash cmd_router lwip)
3+
REQUIRES console spi_flash driver nvs_flash cmd_router lwip)
4+
target_compile_options(${COMPONENT_LIB} PRIVATE -Os)

components/dhcpserver/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ idf_component_register(
1717

1818
# Suppress address comparison warnings (same as upstream lwip component)
1919
# Suppress unused-but-set-variable for ESP_LOG macros that may be compiled out
20-
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-address -Wno-unused-but-set-variable)
20+
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-address -Wno-unused-but-set-variable -O2)
2121

2222
# List of all public dhcpserver functions to wrap
2323
set(DHCPS_WRAP_FUNCTIONS

components/http_server/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ idf_component_register(
66
remote_console dhcpserver wpa_supplicant
77
app_update esp_app_format mbedtls
88
)
9-
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-function)
9+
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-function -Os)

components/http_server/http_server.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,9 +1229,10 @@ static esp_err_t index_get_handler(httpd_req_t *req)
12291229
/* Stream Bytes row (sent/received combined) */
12301230
uint64_t bytes_sent = get_sta_bytes_sent();
12311231
uint64_t bytes_received = get_sta_bytes_received();
1232-
float sent_mb = bytes_sent / (1024.0 * 1024.0);
1233-
float received_mb = bytes_received / (1024.0 * 1024.0);
1234-
snprintf(row, sizeof(row), "<tr><td>Bytes:</td><td>%.1f MB sent / %.1f MB received</td></tr>", sent_mb, received_mb);
1232+
char sent_buf[16], recv_buf[16];
1233+
format_bytes_human(bytes_sent, sent_buf, sizeof(sent_buf));
1234+
format_bytes_human(bytes_received, recv_buf, sizeof(recv_buf));
1235+
snprintf(row, sizeof(row), "<tr><td>Bytes:</td><td>%s sent / %s received</td></tr>", sent_buf, recv_buf);
12351236
httpd_resp_send_chunk(req, row, HTTPD_RESP_USE_STRLEN);
12361237

12371238
/* Stream Monitoring row */

components/mqtt_ha/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ idf_component_register(
99
INCLUDE_DIRS "include"
1010
PRIV_REQUIRES mqtt esp_event esp_wifi esp_netif esp_timer nvs_flash console cmd_router remote_console
1111
)
12+
target_compile_options(${COMPONENT_LIB} PRIVATE -Os)

0 commit comments

Comments
 (0)