Skip to content

ShyamBhagat2004/ld-350-mqtt

Repository files navigation

Project Documentation: Python Application for LD-350 Lightning Detector Data Handling

Introduction

This document details the Python application designed to interface with LD-350 lightning detectors. The application reads data, converts it to NMEA format, and transmits it over MQTT.

Requirements

  • Python Version: 3.10+
  • Dependencies:
    • usb.core
    • usb.util
    • paho.mqtt.client
    • threading
    • sys
    • time
  • Hardware: LD-350 Lightning Detector with a USB connection.

Setup and Configuration

USB Configuration

  1. Device Identification: Uses vendor ID (0x0403) and product ID (0xf241).
  2. Kernel Driver Handling: Detaches the kernel driver if active to allow direct USB communication.
  3. Interface Management: Claims interface 0 for exclusive device access.
  4. Endpoint Configuration: Sets up endpoints for sending and receiving data.

MQTT Configuration

  1. Broker Configuration: Connects to the MQTT broker at broker.mqtt.cool on port 1883.
  2. Client Initialization: Sets up the MQTT client with a unique ID derived from the current timestamp.
  3. Callbacks: Establishes functions to handle connections and message publishing events.

Core Functionality

Reading and Processing Data

  • Continuous Reading: Reads data packets from the USB continuously.
  • Error Handling: Implements robust error handling to manage USB communication errors.

Data Conversion

  • Format Conversion: Converts raw USB data to ASCII, excluding null bytes, and formats into NMEA sentences.

Data Publishing

  • MQTT Publishing: Sends formatted data to a specified MQTT topic, managing message queuing and asynchronous delivery.

File Management

  • File Writing: Periodically writes NMEA data to nmea_output.txt for logging purposes.
  • File Maintenance: Uses a daemon thread to clear the output file every 120 seconds to manage data storage effectively.

Special Features

Keep-Alive Mechanism

  • USB Connection Maintenance: Sends a keep-alive packet every second to maintain the USB connection. This is ESSENTIAL for continued output, and has to be sent in HEX to the LD-350.

Daemon Thread for File Maintenance

  • Background File Clearing: A daemon thread clears the output file at regular intervals, preventing application termination due to this thread.

Challenges and Resolutions

  • USB Communication Issues: Addressed through comprehensive error handling and retry strategies.
  • MQTT Connection Stability: Implemented reconnection mechanisms for network connectivity issues.

Conclusion

This application integrates hardware interfacing, real-time data processing, and network communication to monitor and analyze lightning data effectively. Future enhancements could include dynamic configuration features and broader device support.