This project simulates data publishing to an MQTT broker, generating dynamic data based on configuration files. The simulation supports multiple data types (numeric, boolean, datetime, string, etc.) and topics, making it ideal for testing IoT or distributed systems where data is exchanged through MQTT.
- Features
- Installation
- Configuration
- Usage
- MQTT Client Configuration
- Data Generation Logic
- Error Handling
- Logging
- License
- Simulates MQTT data publishing with customizable configuration.
- Supports multiple data types: boolean, numeric, datetime, string, UUID.
- Incremental and bounded value generation for numeric tags.
- Configurable MQTT client with support for authentication and TLS encryption.
- Automatic handling of data publishing intervals.
- Provides colored terminal output for better readability.
-
Clone the repository:
git clone https://github.com/chriscrcodes/talk-to-your-factory.git cd artifacts/mqtt-data-simulator
-
Install the required dependencies:
pip install -r requirements.txt
Dependencies include:
paho-mqtt
colorama
pyfiglet
The simulator reads configuration from a config.json
file. Below is an example configuration:
{
"mqtt_broker": {
"address": "broker.hivemq.com",
"port": 1883,
"username": "your_username",
"password": "your_password",
"use_tls": false
},
"root_topic": "home/sensor",
"publish_interval": 5,
"topics": [
{
"topics": ["temperature", "humidity"],
"tags": [
{
"tag": "temp_sensor",
"type": "numeric",
"min_value": 10,
"max_value": 35,
"increment_step": 0.5,
"reset": true
},
{
"tag": "humidity_sensor",
"type": "numeric",
"min_value": 20,
"max_value": 80,
"increment_step": 1
}
]
}
]
}
- mqtt_broker: MQTT broker settings (address, port, username, password, TLS).
- root_topic: The root topic under which all sub-topics will be published.
- publish_interval: Time interval (in seconds) between consecutive data publications.
- topics: A list of topic configurations. Each entry includes:
topics
: List of topic names.tags
: List of tags with their respective data type and generation parameters.
-
Prepare your
config.json
file with the desired settings. -
Run the simulator:
python mqtt_data_simulator.py
-
The simulator will:
- Connect to the MQTT broker using the settings in
config.json
. - Continuously publish data to the specified topics at intervals.
- Connect to the MQTT broker using the settings in
The MQTT client is configured with the following parameters:
- Username/Password Authentication: If provided, the simulator will authenticate with the broker using a username and password.
- TLS Encryption: The simulator supports secure communication using TLS, configurable through the
config.json
file. Setuse_tls
totrue
and provide thecertfile
andkeyfile
for authentication.
The simulator can generate different types of data for each tag in the configuration. Here's a detailed explanation of each type and the relevant configuration parameters:
-
Boolean
- Randomly generates either
True
orFalse
. - Example configuration:
{ "tag": "door_sensor", "type": "boolean" }
- Randomly generates either
-
Integer and Float (Numeric Ranges)
- Generates a number (integer or float) within a specified range, optionally increasing or decreasing by a step on each publish cycle.
- min_value: The minimum value the tag can generate.
- max_value: The maximum value the tag can generate.
- increment_step: The value to increment or decrement by on each publish cycle.
- reset: Whether to reset the value to
min_value
if it exceedsmax_value
. - Example configuration:
{ "tag": "temp_sensor", "type": "numeric", "min_value": 10, "max_value": 35, "increment_step": 0.5, "reset": true }
-
Constant
- Always generates the same predefined value for every publish cycle.
- Example configuration:
{ "tag": "static_value", "constant": 42 }
-
Datetime
- Generates the current UTC timestamp in ISO 8601 format.
- Example configuration:
{ "tag": "timestamp", "type": "datetime" }
-
String
- Generates a random string, typically in the format
"SampleString_<random_number>"
. - Example configuration:
{ "tag": "device_id", "type": "string" }
- Generates a random string, typically in the format
-
UUID
- Generates a unique identifier (UUID v4) for each publish cycle.
- Example configuration:
{ "tag": "session_id", "type": "guid" }
-
mean and deviation
- These parameters are used to generate values that fall within a range centered around a
mean
value with a random variation defined bydeviation
. The generated value will be betweenmean - deviation
andmean + deviation
. - Example configuration:
{ "tag": "random_temperature", "type": "numeric", "mean": 25, "deviation": 5 }
- This would generate temperatures between 20 and 30.
- These parameters are used to generate values that fall within a range centered around a
-
min_value and max_value
- These define the lower and upper bounds for the values a tag can generate.
- They are mainly used with numeric tags to set boundaries for the data.
- Example configuration:
{ "tag": "pressure_sensor", "type": "numeric", "min_value": 50, "max_value": 100 }
-
increment_step
- This defines how much a numeric value should increase or decrease on each cycle.
- The value increments or decrements based on the step size and loops back to
min_value
ifreset
istrue
. - Example configuration:
{ "tag": "flow_rate", "type": "numeric", "min_value": 1, "max_value": 10, "increment_step": 0.5, "reset": true }
- The numeric type can use either the
mean
/deviation
approach or themin_value
/max_value
withincrement_step
. You can choose one depending on your use case. - The boolean, constant, datetime, string, and UUID types do not support
min_value
,max_value
, orincrement_step
.
These settings allow for flexible data generation to simulate real-world scenarios where sensor values change over time or remain constant.
The code is wrapped with exception handling using a custom decorator @handle_exception
. This decorator ensures any errors during function execution are logged, providing detailed error information. Each decorated method in the simulator catches exceptions, logs them, and re-raises the error.
The application logs information using Python's logging
module. By default, logs are displayed in the terminal and include timestamps and logging levels. The logging level can be configured to display different levels of verbosity.
INFO
: General information and success messages (default).DEBUG
: Detailed information about the data generation process.ERROR
: Captures errors during execution, along with the function where they occurred.
Terminal outputs are colorized using colorama
to enhance readability:
- Green: Successful MQTT connections.
- Red: Errors.
- Blue: Published data.
This project is licensed under the MIT License. See the LICENSE file for more details.