j-finger / qfuse_backend

Python-based backend for receiving, processing, and storing sensor data via MQTT.
0 stars 0 forks source link

Raspberry Pi Backend and Frontend for qfuse Data Acquisition System

Overview

This repository contains the backend and frontend code for the qfuse data acquisition system, designed to run on a Raspberry Pi 4. The Raspberry Pi acts as a central server, hosting an MQTT broker for data ingestion, a Flask web application for data visualization, and handles data storage in a MariaDB database. The backend processes sensor data received from the ESP32C6 modules and stores it for further analysis, while the frontend provides a dashboard for real-time data visualization and monitoring.

Features

Hardware Setup

Components

Network Configuration

GPIO Pins

Software Dependencies

Directory Structure

Setup Instructions

Prerequisites

  1. Update System Packages:

    sudo apt update
    sudo apt upgrade
  2. Install Python 3 and Pip:

    sudo apt install python3 python3-pip
  3. Install MariaDB:

    sudo apt install mariadb-server
  4. Install Required Python Packages:

    pip3 install flask mariadb paho-mqtt RPi.GPIO

Database Configuration

  1. Secure MariaDB Installation:

    sudo mysql_secure_installation
    • Set a root password.
    • Remove anonymous users.
    • Disallow root login remotely.
    • Remove test database.
    • Reload privilege tables.
  2. Create Database and User:

    sudo mariadb

    In the MariaDB shell:

    CREATE DATABASE sensor_data_db;
    CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON sensor_data_db.* TO 'username'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;

    Replace 'username' and 'password' with your desired credentials.

  3. Initialize Database Tables:

    python3 scripts/initialize_databases.py

MQTT Broker Setup

  1. Install Mosquitto MQTT Broker:
    sudo apt install mosquitto mosquitto-clients
  2. Configure Mosquitto (Optional):
    • Edit /etc/mosquitto/mosquitto.conf to adjust configurations if necessary.
    • Restart Mosquitto service:
      sudo systemctl restart mosquitto

Flask Application Setup

  1. Set Environment Variables:
    export FLASK_APP=app.py
    export FLASK_ENV=development  # Remove or change to 'production' in production environment
  2. Run the Flask Application:
    flask run --host=0.0.0.0 --port=5000
    • The application will be accessible at http://<raspberry_pi_ip>:5000.

Backend Services Setup

  1. Set Up MQTT Listener as a Service:

    • Create a systemd service file /etc/systemd/system/mqtt_listener.service:
      
      [Unit]
      Description=MQTT Listener Service
      After=network.target

[Service] ExecStart=/usr/bin/python3 /path/to/scripts/mqtt_listener.py WorkingDirectory=/path/to/scripts StandardOutput=inherit StandardError=inherit Restart=always User=pi

[Install] WantedBy=multi-user.target

 Replace `/path/to/scripts` with the actual path to your `scripts/` directory.
   - Reload systemd and start the service:
     ```bash
     sudo systemctl daemon-reload
     sudo systemctl enable mqtt_listener.service
     sudo systemctl start mqtt_listener.service
  1. Set Up Network Switcher Script:
    • Ensure the network_switcher.py script is running at startup.
    • Add it to rc.local or create a systemd service for it.

GPIO Permissions

Usage Instructions

Running the Backend

Accessing the Frontend Dashboard

Switching Network Modes

Code Organization

app.py

scripts/

templates/index.html

static/js/charts.js

Configuration

Database Configuration

MQTT Broker Configuration

Network Configuration

Flask Application

Troubleshooting

Future Work

License

This project is licensed under the MIT License.

Acknowledgments