/LightningDetector

Primary LanguageC++MIT LicenseMIT

Lightning Detection System - PlatformIO Heltec V3

Build Status Tests Platform Framework

A distributed lightning detection system built for Heltec WiFi LoRa 32 V3 boards with ESP32-S3, featuring modular architecture, comprehensive hardware abstraction, and GPS tracking capabilities.

๐Ÿš€ Features

Core Functionality

  • Lightning Detection - AS3935 sensor integration with noise filtering
  • LoRa Communication - Long-range data transmission using SX1262 radio
  • GPS Tracking - UC6580 GNSS support for precise location data
  • OLED Display - Real-time status and data visualization
  • WiFi Connectivity - Multi-network support with automatic fallback
  • OTA Updates - Over-the-air firmware updates via WiFi

Hardware Abstraction Layer (HAL)

  • Modular Design - Clean hardware abstraction for easy portability
  • Comprehensive API - GPIO, I2C, SPI, PWM, ADC, Timer, Power, Memory management
  • Test Coverage - 51 unit tests ensuring reliability
  • ESP32-S3 Optimized - Tailored for Heltec WiFi LoRa 32 V3 hardware

Advanced Features

  • Multi-Board Support - Sender/receiver architecture
  • Power Management - Sleep modes and battery monitoring
  • Error Handling - Robust error detection and recovery
  • Debugging Tools - Serial logging and diagnostic information

๐Ÿ“‹ Hardware Requirements

Supported Boards

Key Specifications

  • MCU: ESP32-S3 (240MHz, dual-core, 8MB Flash, 512KB SRAM)
  • Radio: SX1262 LoRa transceiver (915MHz)
  • Display: 128x64 OLED (SSD1306)
  • GPS: UC6580 GNSS (L1+L5/L2 dual frequency)
  • Connectivity: WiFi 802.11 b/g/n

Sensors & Peripherals

  • AS3935 Lightning sensor
  • Battery voltage monitoring
  • External GPIO expansion
  • I2C/SPI device support

๐Ÿ› ๏ธ Quick Start

Prerequisites

# Install PlatformIO Core
pip install platformio

# Clone the repository
git clone https://github.com/Skeyelab/LightningDetector.git
cd LightningDetector

Build Options

Unified Firmware (single binary for Sender or Receiver)

# Build
pio run -e unified

# Upload (USB serial bootloader)
pio run -e unified -t upload

# Monitor serial output
pio device monitor

After flashing, the board boots as Sender by default. Change the role at runtime:

Web UI

  1. Connect to the deviceโ€™s IP or esp32.local
  2. Scroll to Device Role โ†’ click Toggle Role (sender โ†” receiver)
  3. Reboot to apply

HTTP API

curl -X POST http://<ip>/api/v1/config \
     -H 'Content-Type: application/json' \
     -d '{"role":"receiver"}'

The choice is stored in NVS, so future boots keep the selected role.

๐ŸŒ Web Flasher

Browser-Based Firmware Flashing

The project includes a web-based ESP32 firmware flasher that allows you to flash devices directly from your browser without installing additional software.

Features

  • ๐ŸŒ Web Interface: Flash ESP32 devices from any modern browser
  • ๐ŸŒŸ Unified Firmware: Single firmware supports both TX and RX modes
  • ๐Ÿ“ฑ Legacy Support: Transmitter and receiver firmware flashing (backward compatibility)
  • ๐Ÿ“ File Upload: Support for custom firmware files
  • ๐Ÿ”Œ Serial Port: Direct USB connection to ESP32 devices
  • ๐Ÿ“Š Progress Tracking: Real-time flashing progress and status updates

Quick Start

  1. Visit: Web Flasher
  2. Connect: Plug your ESP32 device via USB
  3. Select: Choose Unified Firmware (recommended) or legacy transmitter/receiver
  4. Flash: Click "Connect & Flash" and follow the prompts
  5. Configure: After flashing unified firmware, use web interface to set device role

Requirements

  • Browser: Chrome or Edge (Web Serial API support)
  • Hardware: ESP32 device in download mode
  • USB Cable: Data cable (not just charging cable)

Manual Firmware Upload

  • Upload custom .bin files for unified, transmitter, or receiver firmware
  • Support for both pre-built and custom firmware
  • Automatic file validation and size checking
  • Recommended: Use unified firmware for maximum flexibility

Configuration

WiFi Networks

// Copy and modify wifi_networks_example.h
cp wifi_networks_example.h src/wifi_networks.h
// Edit src/wifi_networks.h with your network credentials

Hardware Pins

// Heltec V3 Pin Configuration (src/config/system_config.h)
#define VEXT_PIN 36        // Power control
#define OLED_RST_PIN 21    // OLED reset
#define BUTTON_PIN 0       // User button
#define LORA_NSS 8         // LoRa chip select
#define LORA_DIO1 14       // LoRa interrupt

๐Ÿ“ Repository Structure

The repository is organized for easy navigation and maintenance:

Scripts (scripts/)

  • ci/ - Continuous Integration scripts
    • run_tests.sh - Comprehensive test suite
    • run_static_analysis.sh - Code analysis tools
    • run_tidy.sh - Clang-tidy formatting
  • dev/ - Development and deployment scripts
    • flash_unified.sh - Flash unified firmware to single device
    • flash_both.sh - Flash unified firmware to both devices
    • create_release.sh - Create release packages
  • optimize/ - Performance optimization tools
    • optimize_clang_tidy_cache.sh - Build cache optimization

Documentation (docs/)

  • development/ - Development guides
    • QUICK_START_RELEASE.md - Release quick start
    • RELEASE_README.md - Release procedures
    • TROUBLESHOOTING.md - Common issues and solutions
  • deployment/ - Deployment documentation
    • OTA_README.md - Over-the-air update guide
    • WEB_FLASHER_DEPLOYMENT.md - Web flasher setup
    • WIFI_MULTI_NETWORK_README.md - Multi-network WiFi config
  • testing/ - Testing documentation
    • NEW_TESTS_NEEDED.md - Test coverage gaps
    • STATIC_ANALYSIS.md - Static analysis setup
  • project/ - Project management
    • CURSOR_PROJECT_STATUS.md - Current project status
    • CI_CACHING_OPTIMIZATION.md - CI performance notes

๐Ÿงช Testing

Run All Tests

# Execute comprehensive test suite
./scripts/ci/run_tests.sh

Individual Test Suites

# Hardware Abstraction Layer tests
pio test -e native -f test_hardware_abstraction

# Integration tests
pio test -e native -f test_integration

# WiFi functionality tests
pio test -e native -f test_wifi_manager

Static Analysis

# Code quality checks
./run_static_analysis.sh

# Clang-tidy analysis
./run_tidy.sh

๐Ÿ“ Project Structure

โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ hardware/              # Hardware Abstraction Layer
โ”‚   โ”‚   โ”œโ”€โ”€ hardware_abstraction.h
โ”‚   โ”‚   โ””โ”€โ”€ hardware_abstraction.cpp
โ”‚   โ”œโ”€โ”€ sensors/               # Sensor implementations
โ”‚   โ”‚   โ”œโ”€โ”€ gps_sensor.h/.cpp
โ”‚   โ”‚   โ”œโ”€โ”€ lightning_sensor.h
โ”‚   โ”‚   โ””โ”€โ”€ sensor_interface.h
โ”‚   โ”œโ”€โ”€ communication/         # Communication protocols
โ”‚   โ”œโ”€โ”€ system/               # System management
โ”‚   โ”œโ”€โ”€ config/               # Configuration files
โ”‚   โ””โ”€โ”€ main.cpp              # Application entry point
โ”œโ”€โ”€ test/                     # Unit tests
โ”œโ”€โ”€ docs/                     # Additional documentation
โ””โ”€โ”€ platformio.ini           # PlatformIO configuration

๐Ÿ”ง Hardware Abstraction Layer

GPIO Operations

#include "hardware/hardware_abstraction.h"
using namespace HardwareAbstraction;

// Initialize HAL
initialize();

// Configure GPIO
GPIO::pinMode(2, GPIO::Mode::MODE_OUTPUT);
GPIO::digitalWrite(2, GPIO::Level::LEVEL_HIGH);

I2C Communication

// Initialize I2C for OLED
I2C::initialize(21, 22, 400000); // SDA, SCL, frequency

// Write to device
uint8_t data[] = {0x00, 0xFF};
I2C::beginTransmission(0x3C);
I2C::write(data, sizeof(data));
I2C::endTransmission();

GPS Integration

#include "sensors/gps_sensor.h"

// Initialize GPS for Wireless Tracker
GPS::Config config = GPS::getWirelessTrackerV11Config();
GPS::initializeGPS(config);

// Read GPS data
if (GPS::hasGPSFix()) {
    const GPS::Data& data = GPS::getGPSData();
    printf("Position: %.6f, %.6f\n", data.latitude, data.longitude);
}

๐Ÿ“ก Communication Protocol

LoRa Messaging

// Message format
struct LightningMessage {
    uint32_t timestamp;
    float latitude;
    float longitude;
    uint16_t strike_count;
    uint8_t noise_level;
    uint16_t battery_mv;
};

WiFi Multi-Network

  • Automatic network selection
  • Connection priority management
  • Fallback mechanisms
  • OTA update support

๐Ÿ”‹ Power Management

Sleep Modes

// Deep sleep for battery conservation
Power::sleep(Power::Mode::DEEP_SLEEP, 300000); // 5 minutes

// Wake on button press or timer

Battery Monitoring

// Check battery status
float voltage = Power::getBatteryVoltage();
uint8_t percent = Power::getBatteryPercent();

๐Ÿ“Š Monitoring & Debugging

Serial Logging

// Debug output levels
#define LOG_DEBUG   0
#define LOG_INFO    1
#define LOG_WARN    2
#define LOG_ERROR   3

Performance Metrics

  • Memory usage tracking
  • LoRa transmission statistics
  • GPS fix quality monitoring
  • Power consumption analysis

๐Ÿค Contributing

Development Workflow

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/new-sensor)
  3. Implement changes with tests
  4. Run test suite (./scripts/ci/run_tests.sh)
  5. Submit pull request

Code Standards

  • Follow existing code style
  • Add unit tests for new features
  • Update documentation
  • Pass static analysis checks

Issue Tracking

๐Ÿ“š Documentation

๐ŸŽฏ Roadmap

Phase 1: Foundation โœ…

  • Hardware Abstraction Layer
  • GPS Integration (UC6580)
  • Comprehensive Testing
  • Build System Optimization
  • Unified Firmware Architecture
  • Web Interface & Role Management
  • Web Server Performance Optimization

Phase 2: Lightning Detection ๐Ÿšง

  • AS3935 Sensor Integration
  • Noise Filtering Algorithms
  • Calibration Procedures
  • Real-time Processing

Phase 3: Communication ๐Ÿ”ฎ

  • LoRa Network Protocol
  • Message Encryption
  • Multi-hop Routing
  • Data Aggregation

Phase 4: Analytics ๐Ÿ”ฎ

  • Storm Tracking
  • Prediction Models
  • Web Dashboard
  • Mobile App

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Heltec Automation - Hardware platform and documentation
  • Espressif Systems - ESP32-S3 microcontroller and ESP-IDF
  • RadioLib - LoRa communication library
  • U8g2 - OLED display library
  • PlatformIO - Development platform and toolchain

๐Ÿ“ž Support


Built with โค๏ธ for the lightning detection community