This web engine began in ARMbasic, Coridium’s compiled BASIC for ARM microcontrollers, as a home-monitoring hub on the RP2350 (Pico 2 W) serving a live dashboard. The hub was then ported to C with the same pages and the same engine, and the C version now also runs on the XIAO ESP32-S3.
A running program serves its own live pages with no on-chip compiler: pages are plain HTML with <?web ?> tags, and both the tags and /?name URLs resolve against a table of the program’s own globals and functions.
Overview
- One engine, two languages.
web_template.crenders pages for the original ARMbasic program and the C port alike; only the symbol lookup differs. This write-up covers the C side, with the BASIC equivalents noted where they help. - Where it runs. The RP2350W (Pico 2 W), and the XIAO ESP32-S3 from the same C source.
- Built on lwIP’s httpd.
/?nameis an httpd CGI handler; pages come through httpd’s custom-file hook, which renders any file containing a<?webtag before sending it.
A /?name request goes straight to the handler; a page request loads the file and renders its tags. Both look names up in the same symbol table, and a function’s web_printf output flows back into the page being rendered.
Page templating: <?web ?>
Any served file containing <?web is rendered before it is sent: each tag is replaced by what it evaluates to, and everything else passes through untouched.
| Tag | What it does |
|---|---|
<?web count ?> |
Inserts a variable: integers as %d, floats as %f, strings as-is |
<?web tempC * 9 / 5 + 32 ?> |
Inserts the value of an expression |
<?web setpoint = 25 ?> |
Assigns to a global or array element; inserts nothing |
<?web samples[3] ?> |
Inserts an array element; samples(3) works too |
<?web lanai_p_str ?> |
Calls a function and inserts its return value (integer, float or string) |
<?web avg(3, 4) ?> |
Calls a function with integer arguments |
<?web showlog ?> |
Calls a void function; whatever it writes with web_printf lands at the tag |
<?web if t > 30 ?> … <?web elseif t > 20 ?> … <?web else ?> … <?web endif ?> |
Includes one branch; nests up to 8 deep |
Expression rules:
- No precedence. Operators apply strictly left to right, so
a + b * cmeans(a + b) * c. There are no grouping parentheses; anything more involved belongs in a function the page calls. - Operators:
+ - * / %and the comparisons> < >= <= = <>(==also accepted). A comparison gives 1 or 0. If either side is a float, the arithmetic is done in floating point. - Operands: number literals, variables, array elements with a literal index, and function calls. A function’s arguments must be single operands, not nested expressions.
- Strings work only as the whole tag (
<?web name ?>), not inside arithmetic. In<?web if name ?>a string counts as true when it is non-empty. - Unknown names evaluate to 0, and a branch that isn’t taken is never evaluated, so a function inside it is not called.
- Each tag holds up to 79 characters.
Emitting HTML: web_printf
A void function called from a page builds a whole block of HTML (a table, a list of nodes) by formatting straight into the page as it renders, with web_printf. In the original ARMbasic program the same call is the WPRINTF keyword, with the same format strings.
// called from a page as <?web shownodes ?>
void shownodes(void) {
web_printf("<table>");
for (int i = 0; i < node_count; i++)
web_printf("<tr><td>%s</td><td>%d</td></tr>", node_role[i], node_temp[i]);
web_printf("</table>");
}
- Same format strings as
printf. Output is appended at the current position in the page, so it appears exactly where the tag sits. printfstill goes to the console. Onlyweb_printfwrites into the page.- Buffer: one rendered page holds 16 KB. On the RP2350,
web_printfdoesn’t check the limit, so a page must stay under it. On the ESP32-S3 it is built onvsnprintfand stops at the end of the buffer instead. - The Coridium formatter used on the RP2350 handles
%d %x %s %c %f %e %g, widths and zero padding.l,handzsize modifiers are accepted and ignored, and%uprints like%d, so values of 231 or more print negative; use%xfor those.
URL functions: /?name
Any exposed global or function can be read, written or called with a plain HTTP GET, and the board answers with one line of text, name = value. That makes it scriptable from a browser, curl, another board or a button on a page.
| Request | Effect | Reply |
|---|---|---|
/?board_f |
Read a variable | board_f = 78 |
/?setpoint=25 |
Write an integer, float or string global | setpoint = 25 (the new value) |
/?samples[3] or /?samples(3) |
Read an array element | samples[3] = 512 |
/?samples[3]=40 |
Write an array element | samples[3] = 40 |
/?lr_on |
Call a void function | lr_on called |
/?lanai_p_str |
Call a function | lanai_p_str = 30.01 |
/?avg=3,4 |
Call with integer arguments, comma-separated | avg = 3 |
/?nosuch |
Unknown name | nosuch = ?undef |
- Real uses in the home network. Sensor nodes push readings to the hub with writes like
/?soil1_pct=42. The dashboard’s light buttons are links like/?lr_on. The hub reads each node’s signal with/?node_rssi. - Values are URL-decoded (
%20,+,%2C), and a string write is cut to the variable’s size. - No bounds check on array indexes: the index is trusted, so a page or script must stay inside the array.
- One name per request. Only the first
?parameter is used. - A bare
/with no parameter servesindex.html.
The symbol table: what pages can see
Pages and /?name URLs can only reach names in the program’s symbol table, a list of each exposed name with its type, address and size (or argument count) that is built on the PC at compile time, so the board never needs a compiler.
- Built by
bin/websym_scan.py, which reads the debug info (DWARF) in the program’s-gobject files and writesweb_symtab.c, compiled and linked into the firmware. MiC runs it on the RP2350; an ESP-IDF build step runs it on the ESP32-S3. - Exposed: non-
staticglobals and functions. Mark anythingstaticto hide it. Names are case-sensitive. - Types:
char,shortandint(signed or unsigned),float,char[]strings, and integer and float arrays. - Calls: functions with no arguments or up to 4
intarguments, returningvoid, an integer,floatorchar *. - In ARMbasic the same table comes from the PC compiler’s symbol dump, appended to the program when BASICtools downloads it.
The C scanner skips pointers, structs, unions, double and variadic functions, since there is no safe way to show or pass them over HTTP. It also leaves out the runtime’s own entry points (main, main_embedded, web_user_setup, web_user_loop, and app_main on the ESP32). Files under the shared library folders aren’t scanned, so only the user program’s own names appear.
Serving pages
Pages are ordinary files: .html, .css, images, and so on. A file containing <?web is rendered on the way out, and anything else is sent as-is with a content type taken from its extension.
| RP2350W (Pico 2 W) | XIAO ESP32-S3 | |
|---|---|---|
| Where pages live | A 1 MB FAT drive in flash, shown on the PC over USB | A microSD card if one is inserted (Sense board), otherwise a 1 MB read-only flash partition |
| How to load them | Drag files onto the drive at boot, then eject it or press a key to save | Copy them onto the FAT32 card, or list them in coridium_web/CMakeLists.txt and run idf.py flash |
| Folders | Top level only | Any path on the card |
| Static files | Sent straight from flash, no copy | Read into a buffer per request |
/servesindex.html.- Getting back to the drive on the RP2350: Ctrl-C resets the board into the writable drive window, and
&reopens it without a reset. - Fragmented files are skipped on the RP2350. A file has to sit in one contiguous run on the drive, which is what a fresh copy onto the drive gives.
- Built-in test pages on the RP2350:
/wtest.htmland/demo.htmlexercise the template engine with no drive needed.
Network helpers for web programs
The same programs that serve pages also talk to other devices. These are the wifi.h calls the home network uses; each has an ARMbasic keyword equivalent (HTTP(), UDPIN, RSSI and so on).
| C call | What it does |
|---|---|
wifi_http_get, wifi_http_post |
Send a request to another device; returns the body with headers removed |
g_http_timeout_ms |
Request timeout, 10 s by default |
wifi_udp_in, wifi_udp_out |
Receive (non-blocking) and send UDP, including broadcasts |
wifi_udp_last_ip, wifi_udp_last_port |
Sender of the last UDP packet |
wifi_http_remote_ip |
Address of the browser making the current request |
wifi_rssi |
This board’s WiFi signal, dBm |
wifi_ntp_sync, wifi_get_time |
Set and read Unix time |
lt_localtime, lt_tz_offset_min, lt_dst_rule |
Local time with US or EU daylight saving |
wifi_resolve, wifi_ping |
Name lookup and ping |
g_last_latency_ms, g_total_run_ms, g_total_idle_ms |
Last page’s render time; run and idle totals |
wifi_mqtt_* |
MQTT client |
wifi_http_remote_ipas a safety check: home_monitor compares it with the address of the Cloudflare tunnel box and refuses light commands that arrive through the tunnel.- The ESP32-S3 port so far provides HTTP get/post, UDP, RSSI, NTP/time, local time and
wifi_http_remote_ip: the calls home_monitor uses. MQTT, ping, raw TCP and POST bodies are not ported yet.
Limits and gotchas
The engine is deliberately small; these are the limits worth knowing before writing pages or web functions.
- Web functions must not block. Pages render, and
/?nameruns, inside the web server: from the SysTick interrupt on the RP2350, in the network task on the ESP32-S3. A function called from a page that makes an HTTP request or waits on the network can hang it. Set a flag instead and do the work in the main loop; home_monitor’srefresh_rssi(sets a flag) andpoll_node_rssi(main loop) are the model. - Pages can see half-updated values. A render can interrupt the main loop partway through updating a group of variables. Write the value that marks a change last, as the OLED status mailbox does with its sequence byte.
- 16 KB per rendered page. A template file of 16 KB or more is sent unrendered, tags and all.
- One render buffer. The RP2350 renders one page at a time into a single buffer; the ESP32-S3 copies each response so overlapping requests don’t collide.
- Short
/?namereplies only. A reply holds 95 characters (159 in the ARMbasic build); a longer value, or a very long name in the URL, is cut off at that length. Keep exposed strings short when the whole value matters. - No operator precedence, no loops in tags (see the templating section): logic belongs in C, with the page calling it.
