Prerequisites
- Python 3.12 or higher
- Access to a MariaDB instance (test environment:
192.168.0.137:3306) - Access to an MQTT broker (default:
192.168.0.137:1883)
Installation
Install dependencies
Install required Python packages:This installs:
SQLAlchemy>=2.0.0- Database ORMPyMySQL>=1.1.0- MariaDB driverpaho-mqtt>=2.1.0- MQTT clientrequests>=2.32.0- HTTP client for POST_ENDPOINT flowspython-dotenv>=1.0.1- Environment variable management
Configure environment
Copy the example environment file and adjust variables:Edit
.env with your configuration:What happens on startup
When you runpython app.py, the gateway performs these initialization steps:
- Load configuration - Reads environment variables from
.envusingsrc/config.py:36 - Initialize logging - Sets up error logger with daily log rotation in
LOG_DIR - Create database tables - Creates
mqtt_servers,flows, anddatatables if they don’t exist - Seed default broker - Inserts default MQTT broker
192.168.0.137:1883if no enabled broker exists - Load flows - Loads all enabled flows from the database
- Subscribe to topics - Subscribes to MQTT topics defined in enabled flows
- Start reload timer - Reloads flows every
FLOWS_RELOAD_INTERVAL_SECONDS(default: 10 minutes)
app.py:1:
Environment variables
Required variables
These variables must be set or the application will fail to start:DB_HOST- MariaDB host addressDB_NAME- Database nameDB_USER- Database usernameDB_PASSWORD- Database password
Optional variables
These variables have default values defined insrc/config.py:36:
DB_PORT- Database port (default:3306)LOG_DIR- Log directory (default:./log)HTTP_TIMEOUT_SECONDS- HTTP timeout for POST_ENDPOINT (default:10)MQTT_CLIENT_ID- MQTT client ID (default:mqtt-gateway)MQTT_KEEPALIVE- MQTT keepalive seconds (default:60)FLOWS_RELOAD_INTERVAL_SECONDS- Flow reload interval (default:600)
Project structure
The application code is organized in thesrc/ directory:
app.py- Application entry pointsrc/main.py- Main application runner with startup logicsrc/config.py- Configuration loading from environment variablessrc/db.py- Database engine, session factory, and initializationsrc/models.py- SQLAlchemy models for database tablessrc/mqtt_client.py- MQTT client implementationsrc/processor.py- Message processing logicsrc/logging_setup.py- Error logging configurationsrc/constants.py- Application constants
Database setup
The gateway automatically creates required tables on first run. If you need to manually inspect or modify the database:Logs
Error logs are written to daily files in theLOG_DIR directory:
- Format:
YYYY-MM-DD.log(e.g.,2026-03-03.log) - New file created each day
- Logs append throughout the day
- Only errors are logged (not informational messages)
Development workflow
Troubleshooting
Cannot connect to database
Validate network connectivity to the MariaDB server:nc:
No messages being processed
Check that enabled flows exist in the database:POST_ENDPOINT not working
Verify theendpoint_url is valid and the endpoint is reachable. Check logs for HTTP errors.
MQTT connection issues
Verify the MQTT broker configuration in themqtt_servers table: