mqtt-simulator
mqtt-simulator copied to clipboard
Easy-to-configure MQTT simulator written in Python to simulate the sending of JSON objects from sensors or devices to a broker.
MQTT Simulator
Easy-to-configure MQTT simulator written in Python 3 to simulate the sending of JSON objects from sensors or devices to a broker.
Features • Getting Started • Configuration • Authors

Features
- Small and easy-to-configure simulator for publishing data to a broker
- Configuration from a single JSON file
- Connection on pre-defined fixed topics
- Connection on multiple topics that have a variable id or items at the end
- Random variation of data generated according to configuration parameters
Getting Started
Prerequisites
- Python 3 (with pip)
Installing Dependencies
To install all dependencies with a virtual environment:
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
Running
The default simulator settings can be changed in the config/settings.json file.
python3 mqtt-simulator/main.py
Runs the simulator according to the settings file.
The terminal will show the simulator event log.
Optionally, you can pass a flag with the path to settings file:
python3 mqtt-simulator/main.py -f <path/settings.json>
Running using Docker
Additionally, you can run via Docker with the included Dockerfile.
Build the image:
docker build -t mqtt-simulator .
Run the container:
docker run mqtt-simulator -f <path/settings.json>
Configuration
-
The
config/settings.jsonfile has the following main configuration parameters:{ "BROKER_URL": "mqtt.eclipse.org", "BROKER_PORT": 1883, "TOPICS": [ ... ] }Key Type Default Description BROKER_URLstring localhost The broker URL where the data will be published BROKER_PORTnumber 1883 The port used by the broker PROTOCOL_VERSIONnumber 4 Sets the paho.mqtt.client protocolparam which is the version of the MQTT protocol to use for this client. Can be either3(MQTTv31),4(MQTTv311) or5(MQTTv5)CLEAN_SESSIONbool True Sets the paho.mqtt.client clean_sessionparam which is a boolean that determines the client type. This property is ignored ifPROTOCOL_VERSIONis5.RETAINbool False Sets the paho.mqtt.publish retainparam which sets the “last known good”/retained message for the topicQOSnumber 2 Sets the paho.mqtt.publish qosparam which is the quality of service level to useTIME_INTERVALnumber 10 Time interval in seconds between submissions towards the topic TOPICSarray<Objects> None Specification of topics and how they will be published
-
The key TOPICS has a array of objects where each one has the format:
{ "TYPE": "multiple", "PREFIX": "temperature", "RANGE_START": 1, "RANGE_END": 2, "TIME_INTERVAL": 25, "DATA": [ ... ] }Key Type Description Required TYPEstring It can be "single","multiple"or"list"yes PREFIXstring Prefix of the topic URL, depending on the TYPEit can be concatenated to/<id>or/<item>yes LISTarray<any> When the TYPEis"list"the topic prefix will be concatenated with/<item>for each item in the arrayif TYPEis"list"RANGE_STARTnumber When the TYPEis"multiple"the topic prefix will be concatenated with/<id>whereRANGE_STARTwill be the first numberif TYPEis"multiple"RANGE_ENDnumber When the TYPEis"multiple"the topic prefix will be concatenated with/<id>whereRANGE_ENDwill be the last numberif TYPEis"multiple"CLEAN_SESSIONbool Overwrites the broker level config value and applies only to this Topic no RETAINbool Overwrites the broker level config value and applies only to this Topic no QOSnumber Overwrites the broker level config value and applies only to this Topic no TIME_INTERVALnumber Overwrites the broker level config value and applies only to this Topic no DATAarray<Objects> Specification of the data that will form the JSON to be sent in the topic yes -
The key DATA inside TOPICS has a array of objects where each one has the format:
{ "NAME": "temperature", "TYPE": "float", "INITIAL_VALUE": 35, "MIN_VALUE": 30, "MAX_VALUE": 40, "MAX_STEP": 0.2, "RETAIN_PROBABILITY": 0.5, "RESET_PROBABILITY": 0.1, "INCREASE_PROBABILITY": 0.7, "RESTART_ON_BOUNDARIES": true }Key Type Description Required NAMEstring JSON property name to be sent yes TYPEstring It can be "int","float","bool","math_expression", or"raw_value"yes INITIAL_VALUEnumber or bool (same as defined in TYPE)Initial value that the property will assume when the simulation starts (random otherwise) optional. Only valid if TYPEis different from"math_expression"MIN_VALUEnumber Minimum value that the property can assume if TYPEis"int"or"float"MAX_VALUEnumber Maximum value that the property can assume if TYPEis"int"or"float"MAX_STEPnumber Maximum change that can be applied to the property from a published data to the next if TYPEis"int"or"float"RETAIN_PROBABILITYnumber Number between 0 and 1 for the probability of the value being retained and sent again yes RESET_PROBABILITYnumber Number between 0 and 1 for the probability of the value being reset to INITIAL_VALUEoptional, default is 0. Only valid ifTYPEis different from"math_expression"INCREASE_PROBABILITYnumber Number between 0 and 1 for the probability of the next value being greater than the previous one optional, default is 0.5(same probability to increase or decrease). Only valid ifTYPEis"int"or"float"RESTART_ON_BOUNDARIESbool When true and the value reaches MAX_VALUEorMIN_VALUEthe next value will be theINITIAL_VALUEoptional, default is false. Only valid if TYPEis"int"or"float"MATH_EXPRESSIONstring Math expression written in a Pythonic way
Also accept functions from Math modulesif TYPEis"math_expression"INTERVAL_STARTnumber Minimum value that the MATH_EXPRESSION's variablexcan assumeif TYPEis"math_expression"INTERVAL_ENDnumber Maximum value that the MATH_EXPRESSION's variablexcan assumeif TYPEis"math_expression"MIN_DELTAnumber Minimum value that can be added to the MATH_EXPRESSION's variablexfrom a published data to the nextif TYPEis"math_expression"MAX_DELTAnumber Maximum value that can be added to the MATH_EXPRESSION's variablexfrom a published data to the nextif TYPEis"math_expression"INDEX_STARTnumber The index to start publishing from the VALUESarrayoptional, default is 0. Only valid ifTYPEis"raw_value"INDEX_ENDnumber The index to end publishing from the VALUESarrayoptional, default is len(values) - 1. Only valid ifTYPEis"raw_value"RESTART_ON_ENDbool When true and the index of the VALUESarray reachesINDEX_ENDthe next value will be theINDEX_START. Otherwise the client willdisconnectwhen reaching the endoptional, default is false. Only valid if TYPEis"raw_value"VALUESarray<any> The values to be published in array order if TYPEis"raw_value"NOTE: Access math_expression.md file for more explanations and a example of
TYPE: "math_expression".


