Imported from GitHub: alexDickhans/ul-temp-sensor ยท commit 98d383c
Description
Adaptive BLE Sensor Node System
README
Adaptive BLE Sensor Node System
A complete IoT sensor system featuring adaptive power management, BLE communication, and data logging. The system consists of a Raytac MDBT50Q (nRF52840) sensor node and a Raspberry Pi host application.
๐๏ธ System Architecture
โโโโโโโโโโโโโโโโโโโ BLE Advertisement โโโโโโโโโโโโโโโโโโโ
โ Sensor Node โ โโโโโโโโโโโโโโโโโโโโโโโบ โ Raspberry Pi โ
โ โ โ Host โ
โ โข nRF52840 โ โ โ
โ โข BME280 โ โ โข BLE Scanner โ
โ โข Battery โ โ โข Data Logger โ
โ โข Adaptive PM โ โ โข SQLite DB โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
๐ Features
MCU Firmware (nRF52840)
- Adaptive Power Management: 4-tier power system based on battery voltage
- BME280 Integration: Temperature, pressure, and humidity sensing
- BLE Advertising: Non-connectable advertisements with sensor data
- Deep Sleep: System OFF mode with RTC wake-up
- Battery Monitoring: ADC-based voltage sensing
Raspberry Pi Host
- BLE Scanner: Continuous scanning for sensor advertisements
- Data Decoding: Parses manufacturer-specific data payloads
- SQLite Database: Local data storage with indexing
- Data Viewer: Query and display sensor data
- Systemd Service: Automatic startup and management
๐ Power Management Tiers
| Tier | Battery Range | Wake Interval | BLE Rate | Description |
|---|---|---|---|---|
| Normal | 4.2V - 3.8V | 5 minutes | 1 Hz | Full performance |
| Conserve | 3.8V - 3.6V | 15 minutes | 0.2 Hz | Reduced frequency |
| Reserve | 3.6V - 3.4V | 30 minutes | 0.1 Hz | Minimal operation |
| Survival | 3.4V - 3.2V | 60 minutes | 0.1 Hz | Critical battery |
๐ฆ Hardware Requirements
Sensor Node
- MCU: Raytac MDBT50Q (nRF52840 inside)
- Sensor: Bosch BME280 (IยฒC)
- Battery: Li-Po pouch (3.7V, 1000mAh+)
- PMIC: BQ25570 (solar charging)
- Connections: IยฒC, ADC, RTC
Host System
- Platform: Raspberry Pi (any modern Pi with BLE)
- OS: Raspberry Pi OS or Ubuntu
- Storage: SD card with 8GB+ free space
๐ Quick Start
1. MCU Firmware Setup
# Install nRF Connect SDK
# Follow: https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/gs_installing.html
# Clone and build
cd mcu_firmware
./build.sh
# Flash to device
west flash -d build
2. Raspberry Pi Host Setup
# Install dependencies
sudo apt update
sudo apt install python3-pip python3-venv bluetooth bluez
# Create virtual environment
cd pi_host
python3 -m venv venv
source venv/bin/activate
# Install Python packages
pip install -r requirements.txt
# Run scanner
python3 sensor_scanner.py
3. View Data
# List known devices
python3 data_viewer.py --list-devices
# View recent data
python3 data_viewer.py --hours 24 --stats
# Filter by device
python3 data_viewer.py --device AA:BB:CC:DD:EE:FF
๐ BLE Data Format
The sensor node broadcasts manufacturer-specific data with the following structure:
struct sensor_adv_data {
uint8_t version; // Protocol version (1)
uint8_t tier; // Power tier (0-3)
uint16_t battery_mv; // Battery voltage in mV
int16_t temperature; // Temperature * 100 (ยฐC)
uint16_t pressure; // Pressure * 10 (hPa)
uint16_t humidity; // Humidity * 100 (%)
uint32_t timestamp; // Unix timestamp
};
Company ID: Nordic Semiconductor (0x0059)
๐ง Configuration
MCU Configuration
Edit mcu_firmware/prj.conf for:
- BLE power settings
- IยฒC configuration
- ADC settings
- Power management options
Host Configuration
Command-line options for sensor_scanner.py:
--db: Database file path--scan-duration: BLE scan duration (seconds)--log-level: Logging verbosity
๐ Data Analysis
Database Schema
CREATE TABLE sensor_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
device_address TEXT NOT NULL,
device_name TEXT,
timestamp DATETIME NOT NULL,
temperature REAL,
pressure REAL,
humidity REAL,
battery_mv INTEGER,
power_tier INTEGER,
rssi INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
Example Queries
-- Average temperature by hour
SELECT strftime('%Y-%m-%d %H:00:00', timestamp) as hour,
AVG(temperature) as avg_temp
FROM sensor_data
GROUP BY hour
ORDER BY hour DESC;
-- Battery status over time
SELECT timestamp, battery_mv, power_tier
FROM sensor_data
WHERE device_address = 'AA:BB:CC:DD:EE:FF'
ORDER BY timestamp DESC;
๐ Troubleshooting
Common Issues
-
No BLE advertisements detected
- Check device is powered and firmware is flashed
- Verify BLE is enabled on Raspberry Pi
- Check for interference or range issues
-
High power consumption
- Verify deep sleep is working (measure current <3ยตA)
- Check BLE advertising intervals
- Monitor battery voltage thresholds
-
Sensor reading errors
- Check IยฒC connections
- Verify BME280 address (0x76 or 0x77)
- Check power supply stability
Debug Commands
# Monitor MCU serial output
west espressif monitor -d build
# Check BLE devices
sudo hcitool lescan
# Monitor system logs
journalctl -u sensor-scanner -f
# Check database integrity
sqlite3 sensor_data.db "PRAGMA integrity_check;"
๐ Systemd Service
Create /etc/systemd/system/sensor-scanner.service:
[Unit]
Description=BLE Sensor Scanner
After=bluetooth.service
Wants=bluetooth.service
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/sensor-system/pi_host
Environment=PATH=/home/pi/sensor-system/pi_host/venv/bin
ExecStart=/home/pi/sensor-system/pi_host/venv/bin/python sensor_scanner.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
Enable and start:
sudo systemctl enable sensor-scanner
sudo systemctl start sensor-scanner
๐ Development
Project Structure
temp-sensor-code/
โโโ mcu_firmware/ # nRF52840 firmware
โ โโโ src/ # Source files
โ โโโ boards/ # Device tree overlays
โ โโโ prj.conf # Zephyr configuration
โ โโโ build.sh # Build script
โโโ pi_host/ # Raspberry Pi application
โ โโโ sensor_scanner.py # Main scanner
โ โโโ data_viewer.py # Data query tool
โ โโโ requirements.txt # Python dependencies
โโโ README.md # This file
Adding New Sensors
- Extend the
sensor_adv_datastructure - Update the Python decoder
- Modify database schema
- Update data viewer
Power Optimization
- Use forced mode for BME280 (not continuous)
- Minimize BLE advertising duration
- Optimize RTC wake intervals
- Monitor current consumption with multimeter
๐ License
This project is open source. See LICENSE file for details.
๐ค Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
๐ Support
For issues and questions:
- Check the troubleshooting section
- Review the code comments
- Open an issue on GitHub
Note: This system is designed for educational and prototyping purposes. For production use, consider additional security measures and environmental protection.
No comments yet. Be the first to ask about this board.