# Install dependencies if not already installed
pip3 install flask flask-cors
# Navigate to skeeball directory
cd ~/skeeball # or wherever your skeeball code is
# Copy the new files (if deploying from dev machine)
# - stats_api_server.py
# - updated skeeball_main.py
# Test the stats API server independently
python3 stats_api_server.pyExpected output:
🚀 Skeeball Stats API Server
==================================================
Access at: http://<raspberry-pi-ip>:5002
Press Ctrl+C to stop
# Health check
curl http://sp1.local:5002/health
# Get current game status
curl http://sp1.local:5002/api/game/status
# Get all lanes
curl http://sp1.local:5002/api/lanes
# Get statistics
curl http://sp1.local:5002/api/game/statsAdd to your .env file:
RPI_STATS_HOST=sp1.local
RPI_STATS_PORT=5002Then restart the Flask app:
python app.pyOpen in your browser:
- Control Panel: http://localhost:5000/skeeball/control
- Statistics: http://localhost:5000/skeeball/stats
The pages should now show real-time data from the Raspberry Pi!
Test: What happens when Raspberry Pi is offline?
- Stop the stats API on the Pi (Ctrl+C)
- Refresh the control panel page
- Expected: Page still loads, shows local lane manager data (no error)
Test: Do stats update in real-time?
- Open control panel in browser
- On Raspberry Pi: insert a coin (physical or simulate)
- Watch browser - credits should show 1
- Play some balls
- Watch score and balls counter update
Test: Do stats persist across restarts?
- Play a few games
- Note the total_games and best_score on the stats page
- Restart skeeball_main.py
- Check stats page again - numbers should match
Test: Can multiple browsers view stats at once?
- Open control panel in Chrome
- Open control panel in Firefox (or another device)
- Both should show same data and update together
ssh pi@sp1.local
ps aux | grep python3 | grep stats
ps aux | grep python3 | grep skeeball_mainssh pi@sp1.local
sudo netstat -tlnp | grep 5002ssh pi@sp1.local
curl http://localhost:5002/health
curl http://localhost:5002/api/lanesFrom your Flask server machine:
# Test DNS resolution
ping sp1.local
# Test HTTP connection
curl http://sp1.local:5002/health
# Check with verbose output
curl -v http://sp1.local:5002/healthOn the Raspberry Pi, watch API requests in real-time:
# If running skeeball_main.py manually, you'll see request logs
# Or check systemd logs:
sudo journalctl -u skeeball.service -fCause: Stats API not running on Pi
Fix:
ssh pi@sp1.local
cd ~/skeeball
python3 skeeball_main.pyCause: stats_api_server.py not found or can't be imported
Fix:
ssh pi@sp1.local
cd ~/skeeball
ls -la stats_api_server.py # Verify file exists
python3 -c "from stats_api_server import *" # Test importCause: Data not loading from state file
Fix:
ssh pi@sp1.local
cd ~/skeeball
ls -la skeeball_state.json # Check if file exists
cat skeeball_state.json # Verify contentCause: Environment variable not set or Pi unreachable
Debug:
# On Flask server
echo $RPI_STATS_HOST
echo $RPI_STATS_PORT
# Test connection
curl http://$RPI_STATS_HOST:$RPI_STATS_PORT/healthFix:
Check .env file has correct values, restart Flask app
Cause: Connection timeout (3 seconds default)
Fix: Check network connection between Flask server and Pi
ping -c 5 sp1.local
traceroute sp1.local# Start fresh
curl http://sp1.local:5002/api/game/status
# The game needs to run on the actual Pi hardware,
# but you can verify the API responds# Terminal 1: Watch status continuously
watch -n 1 curl -s http://sp1.local:5002/api/game/status
# Terminal 2: On the Pi, run a simulated game
python3 -c "
from skeeball_main import SkeeballGame
game = SkeeballGame()
game.simulate_coin()
for i in range(9):
game.simulate_ball(50)
"# Test response time
time curl http://sp1.local:5002/api/lanes
# Should be < 100ms on local network# Simple load test (requires 'apache2-utils' package)
ab -n 1000 -c 10 http://sp1.local:5002/api/lanes
# Should handle 100+ requests/second easily✅ Integration is working if:
- Flask control panel shows "lane_1: 🟢 Ready" (or Playing)
- Statistics page shows accumulated totals (games, coins, scores)
- Inserting a coin updates the control panel within 2 seconds
- Playing a ball updates the score in real-time
- Stats persist after restarting skeeball_main.py
- Flask app gracefully falls back to local data if Pi is unreachable
Once testing is successful:
- Set up systemd service for auto-start on boot
- Configure firewall to allow port 5002
- Set up monitoring (e.g., check if service is running)
- Add to documentation for operators
- Consider HTTPS for production deployment
If you encounter issues not covered here:
- Check the main setup guide:
rpi_skeeball/STATS_API_SETUP.md - Review Flask app logs:
logs/flask.log(if logging is configured) - Review Pi logs:
sudo journalctl -u skeeball.service -n 100 - Test each component independently (API server, Flask app, network)