# 410527416370lh-boop/louiephone architecture

> Architecture diagram of the GitHub repository 410527416370lh-boop/louiephone, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/410527416370lh-boop/louiephone
- Repository: https://github.com/410527416370lh-boop/louiephone

## Overview

1. **System shape**
   LouiePhone is split into two runtime domains: an **ESP32-S3 firmware** that drives the e-ink card device, and a **Python backend** that turns voice requests into app definitions and coordinates messaging. The core architectural boundary is the device-to-server link, with MQTT used for control/state distribution and Tailscale Funnel used for inbound voice capture.

2. **Primary user flow**
   The main path is: **button press on device → audio capture → PCM upload to server → speech-to-text → LLM app generation → JSON app definition → MQTT publish → device updates display/home screen**. This is the central data flow the diagram should emphasize.

3. **Firmware runtime**
   The firmware under `firmware/` is an **ESP-IDF 5.4+ / PlatformIO** application. Its responsibilities are hardware-facing: capture input, record audio, manage WiFi/MQTT connectivity, render the UI, update the e-paper display, and handle power/battery behavior. It is the device execution boundary; no heavy AI logic lives here.

4. **Firmware subsystems**
   The firmware is organized by concern:
   - `audio/` for microphone capture and PCM handling
   - `display/` for EPD drawing and refresh
   - `input/` for buttons/gesture input
   - `net/` for WiFi, MQTT, and a small server/client integration layer
   - `power/` for charging/battery/sleep behavior
   - `ui/` for app rendering and screen composition
   - `core/` for app state and memory management
   - `main.cpp` as the application entry point and orchestration layer

5. **Device state model**
   The device is stateful rather than stateless. `core/app_state.*` and `ui/render.*` imply a local app model that drives what appears on the e-paper screen. The MQTT backend likely supplies app definitions and updates, while the firmware keeps the current screen/app state and renders it to the display.

6. **Network boundary on the device**
   Under `firmware/src/net/`, the device has three distinct network roles:
   - **WiFi connectivity**
   - **MQTT client** for sync/control
   - **server bridge** for the voice/audio upload path  
   This separation matters for the diagram because one path is long-lived control/state over MQTT, while the other is episodic high-bandwidth audio upload to the backend.

7. **Backend runtime**
   The backend under `server/` is a **Python 3.11+ service** that listens for device voice requests and integrates external AI services. `server.py` is the main entry point. `whatsapp_helper.py` indicates a second integration path for forwarding messages from WhatsApp into the system.

8. **Backend external dependencies**
   The backend depends on three outside services that materially shape the architecture:
   - **Deepgram** for speech-to-text
   - **OpenRouter** for LLM-generated app definitions
   - **MQTT broker** for publishing device-facing state/results  
   Tailscale Funnel provides the public ingress for the device audio stream without exposing the backend directly.

9. **Control/data plane split**
   There are effectively two planes:
   - **Control plane:** MQTT-driven device state, home screen updates, todo sync, and generated app definitions
   - **Data plane:** raw audio sent from device to server, transcribed and transformed into structured output  
   Keeping these separate helps explain why the device can remain lightweight while the backend performs AI and message handling.

10. **Protocol and contracts**
   `docs/DEVICE_PROTOCOL.md` suggests an explicit message contract, likely centered on MQTT payload formats and app schema expectations. This is important for the diagram because MQTT is the integration contract between firmware and backend, not just a transport.

11. **Persistence and secondary features**
   The README mentions a **persistent todo list synced over MQTT** and **WhatsApp forwarding**. These are not separate deployments; they are additional application flows layered on top of the same backend/device architecture and likely share the MQTT broker and app-state model.

12. **Build and configuration boundaries**
   Secrets are intentionally split by runtime: firmware credentials live in `firmware/src/config/device_config.h` and backend credentials in `server/.env`. The repo also includes `docs/` for architecture notes, protocol details, build/flash guidance, and battery notes; these are documentation/support artifacts rather than runtime components.

13. **What to show in an architecture diagram**
   The diagram should show:
   - the **ESP32-S3 device** with its internal modules
   - the **Python backend** as the AI orchestration layer
   - the **MQTT broker** as the shared state/control bus
   - **Tailscale Funnel** as the ingress path for voice audio
   - **Deepgram**, **OpenRouter**, and **WhatsApp** as external service dependencies  
   The main visual separation is device hardware vs. cloud/backend services, with MQTT and audio upload as the two cross-boundary flows.

Main entry points: [`firmware/src/main.cpp`](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/main.cpp) (Main), [`server/server.py`](https://github.com/410527416370lh-boop/louiephone/blob/main/server/server.py) (Server), [`firmware/src/ui/render.cpp`](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/ui/render.cpp) (Render), [`firmware/src/core/app_state.cpp`](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/core/app_state.cpp) (App state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_firmware["Firmware"]
  node_fw_main["Main<br/>orchestrator<br/>[main.cpp]"]
  node_fw_state["App state<br/>state model<br/>[app_state.cpp]"]
  node_fw_memory["Memory<br/>allocator<br/>[memory.cpp]"]
  node_fw_audio(("Audio<br/>pcm capture<br/>[audio.cpp]"))
  node_fw_input["Input<br/>buttons<br/>[input.cpp]"]
  node_fw_wifi["WiFi<br/>connectivity<br/>[wifi.cpp]"]
  node_fw_mqtt["MQTT<br/>client<br/>[mqtt.cpp]"]
  node_fw_server["Upload bridge<br/>audio bridge<br/>[server.cpp]"]
  node_fw_power["Power<br/>battery/sleep<br/>[power.cpp]"]
  node_fw_render["Render<br/>ui composition<br/>[render.cpp]"]
  node_fw_draw["Display draw<br/>epaper rendering<br/>[draw.cpp]"]
  node_fw_epd["EPD<br/>display driver<br/>[epd.cpp]"]
end

subgraph group_backend["Backend"]
  node_be_main["Server<br/>python service<br/>[server.py]"]
  node_be_whatsapp["WhatsApp<br/>messaging adapter<br/>[whatsapp_helper.py]"]
end

subgraph group_external["External"]
  node_svc_deepgram(("Deepgram<br/>speech-to-text"))
  node_svc_openrouter(("OpenRouter<br/>llm api"))
  node_svc_mqtt[("MQTT broker<br/>message bus")]
  node_svc_tailscale{{"Funnel<br/>public ingress"}}
end

node_fw_main -->|"reads input"| node_fw_input
node_fw_main -->|"captures audio"| node_fw_audio
node_fw_main -->|"connects network"| node_fw_wifi
node_fw_main -->|"syncs state"| node_fw_mqtt
node_fw_main -->|"manages power"| node_fw_power
node_fw_main -->|"updates ui"| node_fw_render
node_fw_state -->|"provides model"| node_fw_render
node_fw_render -->|"draws screen"| node_fw_draw
node_fw_draw -->|"refreshes panel"| node_fw_epd
node_fw_audio -->|"uploads pcm"| node_fw_server
node_fw_mqtt -->|"publishes/subscribes"| node_svc_mqtt
node_fw_server -->|"ingress path"| node_svc_tailscale
node_be_main -->|"transcribes"| node_svc_deepgram
node_be_main -->|"generates app"| node_svc_openrouter
node_be_main -->|"publishes state"| node_svc_mqtt
node_be_main -->|"forwards messages"| node_be_whatsapp
node_be_main -->|"receives audio"| node_svc_tailscale
node_be_main -.->|"app definition"| node_fw_state

click node_fw_main "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/main.cpp"
click node_fw_state "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/core/app_state.cpp"
click node_fw_memory "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/core/memory.cpp"
click node_fw_audio "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/audio/audio.cpp"
click node_fw_input "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/input/input.cpp"
click node_fw_wifi "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/net/wifi.cpp"
click node_fw_mqtt "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/net/mqtt.cpp"
click node_fw_server "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/net/server.cpp"
click node_fw_power "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/power/power.cpp"
click node_fw_render "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/ui/render.cpp"
click node_fw_draw "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/display/draw.cpp"
click node_fw_epd "https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/display/epd.cpp"
click node_be_main "https://github.com/410527416370lh-boop/louiephone/blob/main/server/server.py"
click node_be_whatsapp "https://github.com/410527416370lh-boop/louiephone/blob/main/server/whatsapp_helper.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_fw_main,node_fw_state,node_fw_memory,node_fw_audio,node_fw_input,node_fw_wifi,node_fw_mqtt,node_fw_server,node_fw_power,node_fw_render,node_fw_draw,node_fw_epd toneBlue
class node_be_main,node_be_whatsapp toneAmber
class node_svc_deepgram,node_svc_openrouter,node_svc_mqtt,node_svc_tailscale toneMint
```

## Components

### Firmware

ESP32-S3 device runtime and hardware-facing subsystems

- [Main](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/main.cpp): orchestrator · Firmware entrypoint and device orchestration · `firmware/src/main.cpp`
- [App state](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/core/app_state.cpp): state model · Local app and screen state · `firmware/src/core/app_state.cpp`
- [Memory](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/core/memory.cpp): allocator · Firmware memory management · `firmware/src/core/memory.cpp`
- [Audio](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/audio/audio.cpp): pcm capture · Microphone capture and PCM handling · `firmware/src/audio/audio.cpp`
- [Input](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/input/input.cpp): buttons · Device input handling · `firmware/src/input/input.cpp`
- [WiFi](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/net/wifi.cpp): connectivity · Wireless network connection · `firmware/src/net/wifi.cpp`
- [MQTT](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/net/mqtt.cpp): client · Device control and state sync client · `firmware/src/net/mqtt.cpp`
- [Upload bridge](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/net/server.cpp): audio bridge · Device-side server integration for voice upload · `firmware/src/net/server.cpp`
- [Power](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/power/power.cpp): battery/sleep · Charging, battery, and low-power behavior · `firmware/src/power/power.cpp`
- [Render](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/ui/render.cpp): ui composition · Screen composition and app rendering · `firmware/src/ui/render.cpp`
- [Display draw](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/display/draw.cpp): epaper rendering · Display drawing primitives and refresh orchestration · `firmware/src/display/draw.cpp`
- [EPD](https://github.com/410527416370lh-boop/louiephone/blob/main/firmware/src/display/epd.cpp): display driver · E-paper panel driver · `firmware/src/display/epd.cpp`

### Backend

Python service that handles voice, app generation, and messaging

- [Server](https://github.com/410527416370lh-boop/louiephone/blob/main/server/server.py): python service · Backend entrypoint and orchestration · `server/server.py`
- [WhatsApp](https://github.com/410527416370lh-boop/louiephone/blob/main/server/whatsapp_helper.py): messaging adapter · WhatsApp forwarding and integration helper · `server/whatsapp_helper.py`

### External

Third-party services and shared infrastructure

- **Deepgram**: speech-to-text · Speech transcription service
- **OpenRouter**: llm api · LLM generation for app definitions
- **MQTT broker**: message bus · Shared control/state broker
- **Funnel**: public ingress · Inbound access path for voice upload

## Connections

- Main → Input: reads input · Main loop consumes button events
- Main → Audio: captures audio · Initiates voice recording flow
- Main → WiFi: connects network · Brings up wireless connectivity
- Main → MQTT: syncs state · Publishes and receives device control messages
- Main → Power: manages power · Coordinates charging and sleep behavior
- Main → Render: updates ui · Drives local app rendering
- App state → Render: provides model · UI renders from current app state
- Render → Display draw: draws screen · Composes display output
- Display draw → EPD: refreshes panel · Pushes rendered frames to e-paper
- Audio → Upload bridge: uploads pcm · Sends captured voice data to backend ingress
- MQTT → MQTT broker: publishes/subscribes · Device state and control exchange
- Upload bridge → Funnel: ingress path · Uses public Funnel path for audio upload
- Server → Deepgram: transcribes · Converts raw audio to text
- Server → OpenRouter: generates app · Turns requests into structured app definitions
- Server → MQTT broker: publishes state · Sends device-facing updates and app payloads
- Server → WhatsApp: forwards messages · Secondary messaging integration path
- Server → Funnel: receives audio · Ingress for device voice requests
- Server → App state: app definition · Conceptual contract: backend output drives device state
