|
|
преди 5 дни | |
|---|---|---|
| api | преди 5 години | |
| assets | преди 6 години | |
| build | преди 1 седмица | |
| cmd | преди 5 дни | |
| configs | преди 5 години | |
| copy_files | преди 1 месец | |
| deployments | преди 6 години | |
| docs | преди 1 седмица | |
| examples | преди 3 години | |
| githooks | преди 6 години | |
| init | преди 6 години | |
| internal | преди 5 дни | |
| log_files | преди 1 месец | |
| pkg | преди 1 година | |
| scripts | преди 1 седмица | |
| test | преди 1 седмица | |
| third_party | преди 6 години | |
| tools | преди 6 години | |
| web | преди 7 години | |
| website | преди 5 години | |
| .editorconfig | преди 4 години | |
| .gitattributes | преди 2 години | |
| .gitignore | преди 1 седмица | |
| LICENSE.md | преди 8 години | |
| Makefile | преди 1 година | |
| README.md | преди 1 седмица | |
| ResLevis-Diagram-2.0.drawio | преди 1 месец | |
| ResLevis-Diagram.drawio | преди 1 месец | |
| beacon.csv | преди 3 седмици | |
| gateway.csv | преди 3 седмици | |
| go.mod | преди 1 месец | |
| go.sum | преди 1 месец | |
| presence.db | преди 1 месец | |
| presense.container | преди 1 месец | |
A comprehensive Bluetooth Low Energy (BLE) presence detection system that tracks beacon devices in real-time, calculates locations based on signal strength, and integrates with popular IoT platforms like Home Assistant and Node-RED.
The system follows a microservices architecture with Apache Kafka as the central message bus:
┌─────────────┐ MQTT ┌─────────────┐ Kafka ┌─────────────┐
│ MQTT │ ────────► │ Bridge │ ────────► │ Decoder │
│ Gateway │ │ Service │ │ Service │
└─────────────┘ └─────────────┘ └─────────────┘
│
▼
┌─────────────┐ WebSocket ┌─────────────┐ Kafka ┌─────────────┐
│ Web UI │ ◄────────── │ Server │ ◄───────── │ Location │
│ Dashboard │ │ Service │ │ Algorithm │
└─────────────┘ └─────────────┘ └─────────────┘
cmd/bridge/)publish_out/# MQTT topics and forwards messages to Kafka rawbeacons topiccmd/decoder/)alertbeacons Kafka topiccmd/location/)locevents Kafka topiccmd/server/)Clone the repository:
git clone https://github.com/AFASystems/presence.git
cd presence
Start the system:
cd build
docker-compose up -d
Verify services:
Set the following environment variables:
# Web Server
HTTP_HOST_PATH=":8080"
HTTP_WS_HOST_PATH=":8081"
# MQTT Configuration
MQTT_HOST="tcp://mqtt-broker:1883"
MQTT_USERNAME="your_username"
MQTT_PASSWORD="your_password"
# Kafka Configuration
KAFKA_URL="kafka:29092"
# Database
DB_PATH="./volumes/presence.db"
SettingsVal)| Parameter | Type | Default | Description |
|---|---|---|---|
location_confidence |
int64 | 80 | Minimum confidence level for location changes |
last_seen_threshold |
int64 | 300 | Time (seconds) before beacon considered offline |
beacon_metrics_size |
int | 10 | Number of RSSI measurements to keep for averaging |
ha_send_interval |
int64 | 60 | Home Assistant update interval (seconds) |
ha_send_changes_only |
bool | true | Send updates only on location changes |
rssi_min_threshold |
int64 | -90 | Minimum RSSI value for beacon detection |
enforce_rssi_threshold |
bool | true | Filter out weak signals |
GET /api/beacons - List all registered beaconsPOST /api/beacons - Register a new beaconPUT /api/beacons/{id} - Update beacon informationDELETE /api/beacons/{id} - Remove a beaconGET /api/settings - Get current system settingsPOST /api/settings - Update system settings/ws/broadcast - Real-time beacon updates and notificationsThe system automatically generates MQTT messages for Home Assistant:
# Example device tracker configuration
device_tracker:
- platform: mqtt
devices:
beacon_001: "Presence/Beacons/beacon_001"
# Battery level sensor
sensor:
- platform: mqtt
name: "Beacon Battery"
state_topic: "Presence/Beacons/beacon_001/battery"
unit_of_measurement: "%"
device_class: battery
# Button press sensor
binary_sensor:
- platform: mqtt
name: "Beacon Button"
state_topic: "Presence/Beacons/beacon_001/button"
device_class: "button"
type Beacon struct {
Name string `json:"name"`
ID string `json:"beacon_id"`
BeaconType string `json:"beacon_type"`
BeaconLocation string `json:"beacon_location"`
LastSeen int64 `json:"last_seen"`
Distance float64 `json:"distance"`
LocationConfidence int64 `json:"location_confidence"`
HSButtonCounter int64 `json:"hs_button_counter"`
HSBattery int64 `json:"hs_button_battery"`
// ... additional fields
}
type BeaconAdvertisement struct {
Hostname string `json:"hostname"`
MAC string `json:"mac"`
RSSI int64 `json:"rssi"`
Data string `json:"data"`
BeaconType string `json:"beacon_type"`
UUID string `json:"uuid"`
Major string `json:"major"`
Minor string `json:"minor"`
// ... additional fields
}
type LocationChange struct {
Method string `json:"method"`
BeaconRef Beacon `json:"beacon_info"`
Name string `json:"name"`
PreviousLocation string `json:"previous_location"`
NewLocation string `json:"new_location"`
Timestamp int64 `json:"timestamp"`
}
| Service | Image | Ports | Description |
|---|---|---|---|
| kafka | apache/kafka:3.9.0 | 9092, 9093 | Apache Kafka message broker |
| kafdrop | obsidiandynamics/kafdrop | 9000 | Kafka monitoring UI |
| presence-bridge | local/build | - | MQTT to Kafka bridge |
| presence-decoder | local/build | - | BLE beacon decoder |
| presence-location | local/build | - | Location calculation service |
| presence-server | local/build | 8080, 8081 | HTTP API and WebSocket server |
./volumes/presence.db - BoltDB database file./volumes/node-red/ - Node-RED configuration and flows./volumes/kafka-data/ - Kafka persistent dataInstall dependencies:
go mod download
Run individual services:
# Run bridge service
go run cmd/bridge/main.go
# Run decoder service
go run cmd/decoder/main.go
# Run location algorithm
go run cmd/location/main.go
# Run API server
go run cmd/server/main.go
Run tests:
go test ./...
# Build all services
docker build -t presence-system .
# Build individual service
docker build -f build/package/Dockerfile.bridge -t presence-bridge .
/
├── cmd/ # Main application entry points
│ ├── server/ # HTTP API & WebSocket server
│ ├── bridge/ # MQTT to Kafka bridge
│ ├── decoder/ # BLE beacon decoder
│ ├── location/ # Location calculation algorithm
│ └── testbench/ # Testing/development utilities
├── internal/ # Private application code
│ ├── app/ # Application components
│ └── pkg/ # Shared internal packages
│ ├── model/ # Data structures and types
│ ├── kafkaclient/ # Kafka producer/consumer
│ ├── config/ # Configuration management
│ ├── persistence/ # BoltDB operations
│ ├── redis/ # Redis client
│ └── bridge/ # MQTT bridge logic
├── build/ # Build artifacts and Docker configs
│ ├── package/ # Dockerfile for each component
│ └── docker-compose.yaml # Complete system deployment
├── web/ # Web interface files
├── volumes/ # Persistent data and configurations
├── scripts/ # Utility scripts
└── docs/ # Documentation
rawbeacons - Raw BLE beacon advertisementsalertbeacons - Processed beacon events (battery, buttons)locevents - Location change notifications# View service logs
docker-compose logs -f [service-name]
# View all logs
docker-compose logs -f
Pre-configured Node-RED flows are available in /volumes/node-red/:
System publishes to the following MQTT topics:
Presence/Beacons/{beacon_id}/location - Current beacon locationPresence/Beacons/{beacon_id}/battery - Battery levelPresence/Beacons/{beacon_id}/button - Button press eventsPresence/Beacons/{beacon_id}/distance - Distance from nearest gatewaygit checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)This project is licensed under the MIT License - see the LICENSE file for details.
AFA Systems Presence Detection - Real-time BLE beacon tracking and location intelligence for modern IoT environments.