Add external configuration file system

Breaking Changes:
- Configuration now via config.json instead of editing Python code
- Remove all hardcoded paths (no more /home/berwn)
- Separate system config (config.json) from runtime config (user_config.json)

Features:
- config.example.json with all configurable options
- GPIO pin and state configuration
- Audio device index configuration
- Customizable paths (relative or absolute)
- Web port and upload size settings
- No code editing required for deployment

Configuration Structure:
- gpio: hook_pin, hook_pressed_state
- audio: device_index, chunk_size, channels, sample_rate, max_record_seconds
- paths: base_dir, recordings_dir, sounds_dir
- web: port, max_upload_size_mb
- system: active_greeting, default_volume

Script automatically:
- Checks for config.json on startup
- Provides helpful error if missing
- Uses relative paths by default
- Loads test_complete.py config from same file

Updated Documentation:
- Complete configuration guide in README
- Setup instructions without hardcoded paths
- Troubleshooting for config errors
- Device index discovery command

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-24 14:48:43 +07:00
parent 1657a242fd
commit ef0373e60b
5 changed files with 196 additions and 74 deletions

138
README.md
View File

@@ -86,7 +86,43 @@ chmod +x configure_hifiberry.sh
Or follow the manual instructions in `AUDIO_FIX.md`.
### 5. Test Your Audio
### 5. Create Configuration File
Copy the example configuration and customize it:
```bash
cp config.example.json config.json
nano config.json # or use your preferred editor
```
**Important settings to configure:**
```json
{
"gpio": {
"hook_pin": 17, // GPIO pin for hookswitch
"hook_pressed_state": "LOW" // "LOW" or "HIGH"
},
"audio": {
"device_index": 1, // HiFiBerry device index
"sample_rate": 44100
},
"paths": {
"base_dir": "./rotary_phone_data" // Relative or absolute path
},
"web": {
"port": 8080
}
}
```
**Finding your audio device index:**
```bash
python3 -c "import pyaudio; p=pyaudio.PyAudio(); [print(f'{i}: {p.get_device_info_by_index(i)[\"name\"]}') for i in range(p.get_device_count())]"
```
### 6. Test Your Audio
```bash
python3 test_complete.py
@@ -97,15 +133,6 @@ This will test:
- Dial tone generation
- Microphone recording
### 6. Configure GPIO Pin
Edit `rotary_phone_web.py` and set your hookswitch GPIO pin:
```python
HOOK_PIN = 17 # Change to your GPIO pin number
HOOK_PRESSED = GPIO.LOW # Or GPIO.HIGH depending on your switch
```
### 7. Run the System
```bash
@@ -161,6 +188,7 @@ wedding-phone/
├── rotary_phone_web.py # Main application
├── test_complete.py # Audio testing script
├── configure_hifiberry.sh # HiFiBerry setup script
├── config.example.json # Example configuration (copy to config.json)
├── pyproject.toml # UV/pip package configuration
├── AUDIO_FIX.md # Audio configuration guide
├── README.md # This file
@@ -172,47 +200,59 @@ wedding-phone/
### Runtime Data (Auto-created)
```
/home/berwn/rotary_phone_data/
rotary_phone_data/ # Default location (configurable)
├── recordings/ # Voice recordings from guests
├── sounds/ # Greeting message WAV files
└── config.json # Active greeting configuration
└── user_config.json # Runtime settings (volume, active greeting)
```
## Configuration
### Audio Settings
All configuration is done via the `config.json` file. **No need to edit Python code!**
Edit these constants in `rotary_phone_web.py`:
### Configuration File Structure
```python
CHUNK = 1024 # Audio buffer size
FORMAT = pyaudio.paInt16 # 16-bit audio
CHANNELS = 1 # Mono
RATE = 44100 # 44.1kHz sample rate
RECORD_SECONDS = 300 # Max recording time (5 minutes)
The `config.json` file contains all system settings:
```json
{
"gpio": {
"hook_pin": 17, // GPIO pin number for hookswitch
"hook_pressed_state": "LOW" // "LOW" or "HIGH" depending on switch
},
"audio": {
"device_index": 1, // Audio device index (run test to find)
"chunk_size": 1024, // Audio buffer size
"format": "paInt16", // Audio format (16-bit)
"channels": 1, // Mono audio
"sample_rate": 44100, // 44.1kHz sample rate
"max_record_seconds": 300 // Max recording time (5 minutes)
},
"paths": {
"base_dir": "./rotary_phone_data", // Data directory (relative or absolute)
"recordings_dir": "recordings", // Subdirectory for recordings
"sounds_dir": "sounds" // Subdirectory for greeting sounds
},
"web": {
"port": 8080, // Web interface port
"max_upload_size_mb": 50 // Max upload file size
},
"system": {
"active_greeting": "dialtone.wav", // Default greeting
"volume": 70 // Default volume (0-100)
}
}
```
### HiFiBerry Device Index
### Finding Your Audio Device
If your HiFiBerry is at a different device index:
```python
# Line ~103 and ~138 in rotary_phone_web.py
output_device_index=1, # Change this number
input_device_index=1, # Change this number
```
Find your device index:
To find your HiFiBerry or other audio device index:
```bash
python3 -c "import pyaudio; p=pyaudio.PyAudio(); [print(f'{i}: {p.get_device_info_by_index(i)[\"name\"]}') for i in range(p.get_device_count())]"
```
### Web Server Port
```python
WEB_PORT = 8080 # Change to your preferred port
```
Look for your HiFiBerry device and note its index number, then set it in `config.json`.
## Troubleshooting
@@ -249,9 +289,10 @@ WEB_PORT = 8080 # Change to your preferred port
### GPIO Not Detecting Hookswitch
1. Verify GPIO pin number
1. Verify GPIO pin number in `config.json`
2. Check if switch is normally open or closed
3. Test with a simple script:
3. Update `hook_pressed_state` in `config.json` ("LOW" or "HIGH")
4. Test with a simple script:
```python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
@@ -262,9 +303,18 @@ WEB_PORT = 8080 # Change to your preferred port
### Web Interface Not Accessible
1. Check if Flask is running: `ps aux | grep python`
2. Verify firewall: `sudo ufw allow 8080`
3. Check IP address: `hostname -I`
4. Try localhost: `http://127.0.0.1:8080`
2. Verify port in `config.json` matches URL
3. Check firewall: `sudo ufw allow 8080` (or your configured port)
4. Check IP address: `hostname -I`
5. Try localhost: `http://127.0.0.1:8080`
### Configuration Errors
If the script won't start:
1. Ensure `config.json` exists (copy from `config.example.json`)
2. Validate JSON syntax: `python3 -m json.tool config.json`
3. Check all paths exist or can be created
4. Verify audio device index is correct
## Auto-start on Boot
@@ -274,7 +324,7 @@ Create a systemd service:
sudo nano /etc/systemd/system/wedding-phone.service
```
Add:
Add (replace `/path/to/wedding-phone` with your actual path):
```ini
[Unit]
@@ -283,9 +333,9 @@ After=network.target sound.target
[Service]
Type=simple
User=berwn
WorkingDirectory=/home/berwn/wedding-phone
ExecStart=/usr/bin/python3 /home/berwn/wedding-phone/rotary_phone_web.py
User=pi
WorkingDirectory=/path/to/wedding-phone
ExecStart=/usr/bin/python3 /path/to/wedding-phone/rotary_phone_web.py
Restart=always
RestartSec=10