|
| 1 | +# Chat App |
| 2 | + |
| 3 | +ESP-NOW based chat application with channel-based messaging. Devices with the same encryption key can communicate in real-time without requiring a WiFi access point or internet connection. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Channel-based messaging**: Join named channels (e.g. `#general`, `#random`) to organize conversations |
| 8 | +- **Broadcast support**: Messages with empty target are visible in all channels |
| 9 | +- **Configurable nickname**: Identify yourself with a custom name (max 23 characters) |
| 10 | +- **Encryption key**: Optional shared key for private group communication |
| 11 | +- **Persistent settings**: Nickname, key, and current chat channel are saved across reboots |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +- ESP32 with WiFi support (not available on ESP32-P4) |
| 16 | +- ESP-NOW service enabled |
| 17 | + |
| 18 | +## UI Layout |
| 19 | + |
| 20 | +``` |
| 21 | ++------------------------------------------+ |
| 22 | +| [Back] Chat: #general [List] [Gear] | |
| 23 | ++------------------------------------------+ |
| 24 | +| alice: hello everyone | |
| 25 | +| bob: hey alice! | |
| 26 | +| You: hi there | |
| 27 | +| (scrollable message list) | |
| 28 | ++------------------------------------------+ |
| 29 | +| [____input textarea____] [Send] | |
| 30 | ++------------------------------------------+ |
| 31 | +``` |
| 32 | + |
| 33 | +- **Toolbar title**: Shows `Chat: <channel>` with the current channel name |
| 34 | +- **List icon**: Opens channel selector to switch channels |
| 35 | +- **Gear icon**: Opens settings panel (nickname, encryption key) |
| 36 | +- **Message list**: Shows messages matching the current channel or broadcast messages |
| 37 | +- **Input bar**: Type and send messages to the current channel |
| 38 | + |
| 39 | +## Channel Selector |
| 40 | + |
| 41 | +Tap the list icon to change channels. Enter a channel name (e.g. `#general`, `#team1`) and press OK. The message list refreshes to show only messages matching the new channel. |
| 42 | + |
| 43 | +Messages are sent with the current channel as the target. Only devices viewing the same channel will display the message. Broadcast messages (empty target) appear in all channels. |
| 44 | + |
| 45 | +## First Launch |
| 46 | + |
| 47 | +On first launch (when no settings file exists), the settings panel opens automatically so users can configure their nickname before chatting. |
| 48 | + |
| 49 | +## Settings |
| 50 | + |
| 51 | +Tap the gear icon to configure: |
| 52 | + |
| 53 | +| Setting | Description | Default | |
| 54 | +|---------|-------------|---------| |
| 55 | +| Nickname | Your display name (max 23 chars) | `Device` | |
| 56 | +| Key | Encryption key as 32 hex characters (16 bytes) | All zeros (empty field) | |
| 57 | + |
| 58 | +Settings are stored in `/data/settings/chat.properties`. The encryption key is stored encrypted using AES-256-CBC. |
| 59 | + |
| 60 | +When the key field is left empty, the default all-zeros key is used. All devices using the default key can communicate without configuration. |
| 61 | + |
| 62 | +Changing the encryption key causes ESP-NOW to restart with the new configuration. |
| 63 | + |
| 64 | +## Wire Protocol |
| 65 | + |
| 66 | +Variable-length packed struct broadcast over ESP-NOW (ESP-NOW v2.0): |
| 67 | + |
| 68 | +``` |
| 69 | +Offset Size Field |
| 70 | +------ ---- ----- |
| 71 | +0 4 header (magic: 0x31544354 "TCT1") |
| 72 | +4 1 protocol_version (0x01) |
| 73 | +5 24 sender_name (null-terminated, zero-padded) |
| 74 | +29 24 target (null-terminated, zero-padded) |
| 75 | +53 1-1417 message (null-terminated, variable length) |
| 76 | +``` |
| 77 | + |
| 78 | +- **Minimum packet**: 54 bytes (header + 1 byte message) |
| 79 | +- **Maximum packet**: 1470 bytes (ESP-NOW v2.0 limit) |
| 80 | +- **v1.0 compatibility**: Messages < 250 bytes work with ESP-NOW v1.0 devices |
| 81 | + |
| 82 | +Messages with incorrect magic/version or invalid length are silently discarded. |
| 83 | + |
| 84 | +### Target Field Semantics |
| 85 | + |
| 86 | +| Target Value | Meaning | |
| 87 | +|-------------|---------| |
| 88 | +| `""` (empty) | Broadcast - visible in all channels | |
| 89 | +| `#channel` | Channel message - visible only when viewing that channel | |
| 90 | +| `username` | Direct message | |
| 91 | + |
| 92 | +## Architecture |
| 93 | + |
| 94 | +``` |
| 95 | +ChatApp - App lifecycle, ESP-NOW send/receive, settings management |
| 96 | +ChatState - Message storage (deque, max 100), channel filtering, mutex-protected |
| 97 | +ChatView - LVGL UI: toolbar, message list, input bar, settings/channel panels |
| 98 | +ChatProtocol - Variable-length Message struct, serialize/deserialize (v2.0 support) |
| 99 | +ChatSettings - Properties file load/save with encrypted key storage |
| 100 | +``` |
| 101 | + |
| 102 | +All files are guarded with `#if defined(CONFIG_SOC_WIFI_SUPPORTED) && !defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED)` to exclude from P4 builds. |
| 103 | + |
| 104 | +## Message Flow |
| 105 | + |
| 106 | +### Sending |
| 107 | + |
| 108 | +1. User types message and taps Send |
| 109 | +2. Message serialized into `Message` struct with current nickname and channel as target |
| 110 | +3. Broadcast via ESP-NOW to nearby devices |
| 111 | +4. Own message stored and displayed locally |
| 112 | + |
| 113 | +### Receiving |
| 114 | + |
| 115 | +1. ESP-NOW callback fires with raw data |
| 116 | +2. Validate: size within valid range (54-1470 bytes), magic and version must match |
| 117 | +3. Copy into aligned local struct (avoids unaligned access on embedded platforms) |
| 118 | +4. Extract sender name, target, and message as strings |
| 119 | +5. Store in message deque |
| 120 | +6. Display if target matches current channel or is broadcast (empty) |
| 121 | + |
| 122 | +## Limitations |
| 123 | + |
| 124 | +- Maximum 100 stored messages (oldest discarded when full) |
| 125 | +- Nickname: 23 characters max |
| 126 | +- Channel name: 23 characters max |
| 127 | +- Message text: 1416 characters max (ESP-NOW v2.0) |
| 128 | +- No message persistence across app restarts (messages are in-memory only) |
| 129 | +- All communication is broadcast; channel filtering is client-side only |
| 130 | +- Messages > 250 bytes only received by devices running ESP-NOW v2.0 |
0 commit comments