Skip to main content

IoT Device Simulator for developing and testing downstream logic relying on IoT physical devices

Project description

IoT Device Simulator

Coverage PyPI Version Python Version License Docker Image

Python IoT device simulator consisting of an IoT Container running units representing IoT devices defined in a json file (see iotunits.json), a smart elevator and a temperature sensor implementation can be found in the examples folder.

Usage guide - Run examples

If you don't know what's uv -> astral-uv

Open folder in devcontainer, then you should be good to go

uv sync (optional)

create json config, use config-default.json as template

{
    "logger": {
        "file_path": "./iotsim.log",
        "verbosity": "DEBUG"
    },
    "client": {
        "name": "test_client",
        "type": "mqtt",
        "host": "localhost",
        "port": 1883,
        "root_ca_path": "",
        "client_certificate_path": "",
        "client_key_path": ""
    },
    "units": {
        "units_list_file_path": "", #leaving it blank will load examples
        "units_py_module_path": ""  #leaving it blank will load examples
    }
}

Run simulator

uv run iotsim/main.py --config <path-to-config.json> #if no argument is set the app will make use of the default config-default.json

Docker

docker build -t iotsim:local .
docker run --network=host iotsim:local

or

docker pull ghcr.io/mockasort-studio/iotsim:latest

Implement your own IoT Units

An IoT unit can be anything you wish, the package makes use of importlib to import user defined module at runtime just by declaring them in the iotunits.json.

The Units make use of user defined registers (simulating the volatile memory of the device) to manipulate and share data between control_loop, publishers and subscribers. Register works in a dictionary fashion and the keys and init value must be defined in the json definition of your unit.

{
    "name": "iot-unit", #name of the unit
    "control_loop_module": "user-define-unit.control-loop-module", #null if periodic activity is not needed
    "control_loop_sleep_ms": cyle_time, #cycle time of the periodic activity
    "registers": {}, #register
    "publishers": [{...}], #publishers list
    "subscribers": [{...}] #subscribers list
}
Control loop

Unit package structure

|-- user-defined-unit/
    |-- __init__.py
    |-- control-loop-module.py

Control loop function must have the signature below:

        iotsim.core.stateregistry.StateRegistry
            |
def run(registers,):
    #TODO Implement your logic
    #registers.update('key', some-value)
    #registers.get_value('key')
Publishers

Publishers are mapped to a register key and publish, with a given frequency, the value corresponding to the key.

Json definition

{
    "id": "publisher-id",
    "publish_frequency_ms",       #publish frequency in ms
    "read": "register-key",       #register key accessed by publisher
    "topic": "mqtt-topic"         #mqtt-topic of the subscription
}
Subscribers

Subscribers are triggered by a message received on their mqtt-topic and write some data received on topic to the register value they're mapped to

Json definition

{
    "id": "subscriber-id",
    "write": "register-key",         #register key accessed by subscriber
    "topic": "unit/robot_request"    #mqtt-topic of the subscription
}

TODO LIST

  • Code clean up
  • Unit testing
  • Improve documentation
  • Improve overall abstraction
  • Improve Python packaging
  • Refactor project in order to make use of an arbitrary communication protocol

Contributors

Contribution

If you wish to contribute get in touch with MockaSort-Studio

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iotsim-1.0.1.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iotsim-1.0.1-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file iotsim-1.0.1.tar.gz.

File metadata

  • Download URL: iotsim-1.0.1.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for iotsim-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ac0ca059efc8b0e22e6d1af0a14582981c989b4025daff4f521e36d1a8cbdf23
MD5 2cf9ac8451e6fb8509a1871111615bf8
BLAKE2b-256 b8695e96be8e8dc6c7b20500ee96838840a646f0aabab6b7b469a7e3a2f124f9

See more details on using hashes here.

File details

Details for the file iotsim-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: iotsim-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for iotsim-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d23c5c507a3ae3e4968ef3823de2c6b19c315b22250c62716c216a7a8f7e154b
MD5 ce6ad951748f4b93e1c67769b5c196e7
BLAKE2b-256 cf51124a190534a7fba505efb4240d2567091ec53ccd266470281a0985a02e8c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page