100%
ul-temp-sensor kicad_temp view
Description

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

TierBattery RangeWake IntervalBLE RateDescription
Normal4.2V - 3.8V5 minutes1 HzFull performance
Conserve3.8V - 3.6V15 minutes0.2 HzReduced frequency
Reserve3.6V - 3.4V30 minutes0.1 HzMinimal operation
Survival3.4V - 3.2V60 minutes0.1 HzCritical 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

  1. 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
  2. High power consumption

    • Verify deep sleep is working (measure current <3ยตA)
    • Check BLE advertising intervals
    • Monitor battery voltage thresholds
  3. 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

  1. Extend the sensor_adv_data structure
  2. Update the Python decoder
  3. Modify database schema
  4. 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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. 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.

Comments
Sign in to comment

No comments yet. Be the first to ask about this board.