Viktor Petersson logo

Blog

Sonar Is Back: A Fresh Take on BLE Device Counting

07 MAY • 2025 3 minutes

I’m excited to share that I’ve just given Sonar—a FastAPI-based BLE device counter I built years ago—a full overhaul and relaunched it as an open-source project on GitHub: https://github.com/viktopia/sonar

Why Sonar?

When I first created Sonar, the goal was simple: track Bluetooth Low Energy devices nearby to estimate foot traffic in a space without specialized hardware. Over time, the codebase drifted, dependencies aged, and the project paused. Now, with modern Python tooling and container best practices, Sonar is leaner, easier to deploy, and more powerful than ever.

What’s New

  • Modern service architecture Sonar has been fully migrated from its original Django codebase to a lean FastAPI service, with the old manage.py and Django apps removed and a new app/main.py powering all BLE scanning logic.

  • Improved code quality We’ve adopted Ruff for linting and import sorting, and fortified the test suite with Pytest (including async tests and coverage checks). The new requirements-test.txt lists pytest, pytest-asyncio, pytest-cov, and ruff, ensuring consistent style and at least 80 percent coverage.

  • Docker-first deployments Sonar now ships with a production Dockerfile for building a container image and a simplified docker-compose.yml that sets up the BLE scanner with persistent storage and hardware access.

  • Simplified API A new set of REST endpoints in app/api/endpoints.py provides comprehensive device monitoring capabilities. The /latest endpoint returns current scan results and historical statistics, /time-series offers detailed 24-hour data with customizable intervals, and /health provides system status checks. All endpoints return well-structured JSON responses with robust error handling.

  • Removed legacy code The old Django analytics application, static assets (like jQuery bundles), and deprecated Balena/Raspbian scripts have been stripped away, leaving a focused, modern codebase.

Getting Started

  1. Clone the repo
git clone https://github.com/viktopia/sonar.git
cd sonar
  1. Run with Docker Compose (recommended)
docker-compose up -d --build
  1. Verify it’s working
curl http://localhost:8000/health
# Expect: { "status": "healthy", "message": "System requirements met" }

While Sonar is primarily designed for Raspberry Pi, it should be compatible with most other boards and devices that have Bluez-compatible Bluetooth hardware. Feel free to dive into the API endpoints, inspect the source, and contribute—issues and pull requests are very welcome. Happy scanning!

Enjoyed this post? Check out my podcast!

If you found this interesting, you might enjoy "Nerding Out with Viktor" - my podcast where I dive deep into tech, entrepreneurship, and security with industry experts.

Listen on:
Found an error or typo? File PR against this file.