Skip to main content

A comprehensive IoT device simulator for Azure IoT Hub and IoT Platform servers

Project description

Device Simulator

A comprehensive IoT device simulator that connects to Azure IoT Hub and IoT Platform servers. This simulator can provision devices, monitor device twins, send telemetry data, and handle commands according to device type schemas.

Features

๐Ÿ”ง Device Configuration

  • Local Configuration: Load device configuration from local files if already configured
  • Interactive CLI Setup: Command-line interface for device configuration and management
  • OAuth2 Authentication: Secure authentication with IoT Platform server
  • Device Provisioning: Automatic device creation in IoT Hub
  • Configuration Management: Get and set device configurations through IoT Hub
  • Schema Management: Download and cache device type schemas locally

๐Ÿ”Œ Connectivity

  • Azure IoT Hub Integration: Connect to IoT Hub using device connection strings
  • Gateway Support: Connect through configured gateways
  • SAS Token Authentication: Secure device authentication

๐Ÿ“Š Device Twin Monitoring

  • Desired Properties: Listen for twin configuration changes from IoT Hub
  • Reported Properties: Update device status and configurations to IoT Hub only if reported section of the device is empty
  • Default Configurations: Apply schema-based default values and sync with IoT Hub

๐Ÿ“ก Telemetry (D2C Messages)

  • Message Types:
    • ๐Ÿšจ Events: Critical notifications and warnings
    • ๐Ÿ“ˆ Measurements: Sensor readings and metrics
    • โšก Status: Device health and operational status
    • ๐Ÿ”ง FW Debug: Firmware debugging and diagnostic information
  • Scheduled Transmission: Configurable intervals for each message type, on each trigger all the messages of this type will be sent (all events/meausrement/etc)
  • Schema Compliance: Values generated according to device type schema
  • Data Continuity: Coordinated values based on previous messages

๐ŸŽฏ Command Handling (C2D)

  • Command Reception: Process commands from IoT Platform
  • Schema Validation: Verify commands against device type schema
  • Response Generation: Automatic success responses when required

๐Ÿ“… Historical Telemetry Generation

You can generate and send historical telemetry data for a device over a custom date range. This is useful for backfilling data or simulating device history.

Example usage:

python -m simulator.main --generate-history --start-date 2026-01-01 --end-date 2026-01-30 --message-count 1000

Arguments:

  • --generate-history: Enable historical data generation mode
  • --start-date: Start date (supports YYYY-MM-DD )
  • --end-date: End date (supports YYYY-MM-DD )
  • --message-count: Number of message sets to generate (each set includes all enabled message types)

If no device is configured, you will be prompted to set one up before generating historical data.

Prerequisites

  • Python 3.9 or higher (supports Python 3.9-3.13)
  • pip package manager
  • Virtual environment
  • Azure IoT Hub instance
  • IoT Platform server with OAuth2 support
  • Device type schemas in IoT Platform server

Installation

Quick Setup (Recommended)

# Clone the repository
git clone <repository-url>
cd tandem-platform-simulator

# Run setup script
# Windows:
setup.bat
# Linux/macOS:
bash setup.sh

The setup script will:

  1. Create a virtual environment
  2. Install the package in development mode with all dependencies
  3. Copy configuration template
  4. Set up logging directory

Manual Installation

# Create virtual environment
python -m venv venv

# Activate virtual environment
# Windows:
venv\Scripts\activate
# Linux/macOS:
source venv/bin/activate

# Install package in development mode with all dependencies
pip install -e ".[dev]"

# Copy configuration template
copy simulator\config\templates\config.template.json config\config.json  # Windows
# cp simulator/config/templates/config.template.json config/config.json  # Linux/macOS

Configuration

Local Device Configuration

If you have existing device configuration, create these files:

config/device-config.json

{
  "deviceId": "your-device-id",
  "gatewayId": "your-gateway-id",
  "deviceType": "your-device-type",
  "lastUpdated": "2025-08-24T10:30:00Z"
}

schemas/device-type-schema.json

Application Configuration

config/config.json

{
  "iotPlatform": {
    "baseUrl": "https://your-iot-platform.com/api",
    "oauth2": {
      "clientId": "your-client-id",
      "authorization_endpoint": "https://your-authority.your-region.cloudapp.azure.com/application/o/authorize/",
      "token_endpoint": "https://your-authority.your-region.cloudapp.azure.com/application/o/token/"
    }
  },
  "provisioner": {
    "baseUrl": "https://your-provisioner-app.azurewebsites.net"
  },
  "iotHub": {
    "hostName": "your-iot-hub-name.azure-devices.net"
  },
  "messaging": {
    "intervals": {
      "measurement": 60,
      "sw_logs": 180,
      "state": 120,
      "events": 300
    },
    "enabled": {
      "measurement": true,
      "sw_logs": false,
      "state": true,
      "events": true
    }
  },
  "logging": {
    "level": "INFO",
    "file": "logs/simulator.log"
  }
}

Message Type Configuration

The messaging configuration has two sections:

  • intervals: How often each message type is sent (in seconds)
  • enabled: Whether each message type should be sent at all

Message Types:

  • measurement: Sensor readings and metrics
  • ๐Ÿ”ง sw_logs: Software logs and debugging information
  • ๐Ÿ”„ state: Device state and operational status
  • โš ๏ธ events: Event notifications and alerts

Default Settings:

  • All message types are enabled by default except sw_logs (disabled)
  • You can disable any message type by setting its enabled value to false
  • Disabled message types will not generate telemetry tasks, saving resources

Usage

1. Start the Simulator

# Activate virtual environment first (if not already active)
# Windows:
venv\Scripts\activate
# Linux/macOS:
source venv/bin/activate

# Start the simulator (recommended way)
iot-simulator

# Initialize configuration if needed
iot-simulator --init

# Alternative way (direct module execution)
python -m simulator.main

2. Device Configuration Flow

Option A: Pre-configured Device

If config/device-config.json and corresponding schema exist:

  • โœ… Device automatically starts tracking
  • ๐Ÿ”— Connects to IoT Hub using stored connection string
  • ๐Ÿ“Š Begins twin monitoring and telemetry transmission

Option B: New Device Setup

If no local configuration exists:

  1. ๐ŸŒ CLI-based Authorization

    • Follow OAuth2 login prompts in terminal
    • Authenticate with IoT Platform credentials via browser
    • Grant device management permissions
  2. โš™๏ธ Device Configuration via CLI

    • New Device Creation:

      • Enter Device ID through command prompts
      • Select from available Managed Groups (displayed in terminal)
      • Choose Device Type from server list (interactive menu)
      • Provision new device in IoT Hub
      • Set initial configurations in IoT Hub device twin
    • OR Choose Existing Device:

      • Select from available devices list displayed in terminal
      • Get device configurations from IoT Hub device twin
      • Get selected device data from server: id, gateway id, managed-group, type.
  3. ๐Ÿ’พ Schema Download

    • Device type schema downloaded from IoT Platform
    • Configuration saved locally for future use

3. Runtime Operations

Twin Configuration Monitoring

[TWIN] Listening for desired property changes from IoT Hub...
[TWIN] Generated default configs from schema
[TWIN] Desired property updated from IoT Hub: {'telemetryInterval': 45}
[TWIN] Patched reported property: {'telemetryInterval': 45}

Telemetry Transmission

[D2C] Sending measurement: {"temperature": 23.5, "humidity": 65.2}
[D2C] Sending state: {"batteryLevel": 85, "signalStrength": -67}
[D2C] Sending events: {"type": "highTemperature", "value": 78.2}
[D2C] Sending sw_logs: {"firmware_version": "1.2.3", "memory_usage": 65, "cpu_temp": 42}

Command Handling

[C2D] Received command: reboot
[C2D] Command validated against schema
[C2D] Sending response: {"status": "success", "timestamp": "2025-10-26T10:30:00Z"}

Project Structure

tandem-platform-simulator/
โ”œโ”€โ”€ pyproject.toml            # Modern Python packaging configuration
โ”œโ”€โ”€ setup.bat/setup.sh        # Setup scripts for development environment
โ”œโ”€โ”€ simulator/               # Main package
โ”‚   โ”œโ”€โ”€ main.py             # Primary application entry point
โ”‚   โ”œโ”€โ”€ api/                # Platform API clients
โ”‚   โ”‚   โ”œโ”€โ”€ device_creation_metadata_api.py
โ”‚   โ”‚   โ””โ”€โ”€ firmware_version_api.py
โ”‚   โ”œโ”€โ”€ auth/               # OAuth2 authentication
โ”‚   โ”‚   โ”œโ”€โ”€ oauth_flow.py
โ”‚   โ”‚   โ””โ”€โ”€ oauth2_client.py
โ”‚   โ”œโ”€โ”€ config/             # Configuration management
โ”‚   โ”‚   โ”œโ”€โ”€ config_manager.py
โ”‚   โ”‚   โ”œโ”€โ”€ schemas.py
โ”‚   โ”‚   โ””โ”€โ”€ templates/
โ”‚   โ”‚       โ””โ”€โ”€ config.template.json
โ”‚   โ”œโ”€โ”€ connectivity/       # IoT Hub client
โ”‚   โ”‚   โ””โ”€โ”€ iot_hub_client.py
โ”‚   โ”œโ”€โ”€ device_setup/       # CLI-based device configuration
โ”‚   โ”‚   โ”œโ”€โ”€ setup_coordinator.py
โ”‚   โ”‚   โ”œโ”€โ”€ configuration_ui.py
โ”‚   โ”‚   โ””โ”€โ”€ device_selector.py
โ”‚   โ”œโ”€โ”€ provisioning/       # Device provisioning
โ”‚   โ”‚   โ””โ”€โ”€ device_provisioner.py
โ”‚   โ”œโ”€โ”€ telemetry/          # D2C message generation
โ”‚   โ”‚   โ”œโ”€โ”€ sender.py
โ”‚   โ”‚   โ”œโ”€โ”€ telemetry_generator.py
โ”‚   โ”‚   โ””โ”€โ”€ message_generators/
โ”‚   โ”‚       โ”œโ”€โ”€ measurement_generator.py
โ”‚   โ”‚       โ”œโ”€โ”€ state_generator.py
โ”‚   โ”‚       โ”œโ”€โ”€ alerts_generator.py
โ”‚   โ”‚       โ””โ”€โ”€ fw_debug_generator.py
โ”‚   โ”œโ”€โ”€ twin/               # Device twin manager
โ”‚   โ”‚   โ”œโ”€โ”€ twin_manager.py
โ”‚   โ”‚   โ”œโ”€โ”€ default_config_applier.py
โ”‚   โ”‚   โ”œโ”€โ”€ firmware_reporter.py
โ”‚   โ”‚   โ””โ”€โ”€ reported_store.py
โ”‚   โ”œโ”€โ”€ commands/           # C2D command processing
โ”‚   โ”‚   โ””โ”€โ”€ command_handler.py
โ”‚   โ””โ”€โ”€ utils/              # Utilities and logging
โ”‚       โ”œโ”€โ”€ logger.py
โ”‚       โ”œโ”€โ”€ version_parser.py
โ”‚       โ””โ”€โ”€ schema/
โ”‚           โ”œโ”€โ”€ schema_generator.py
โ”‚           โ”œโ”€โ”€ schema_reference_resolver.py
โ”‚           โ””โ”€โ”€ schema_value_generator.py
โ”œโ”€โ”€ config/                 # Configuration files
โ”‚   โ”œโ”€โ”€ config.json         # Application configuration (created from template)
โ”‚   โ”œโ”€โ”€ device-config.json  # Device-specific config (auto-generated)
โ”‚   โ””โ”€โ”€ auth-tokens.json    # OAuth tokens (auto-generated)
โ”œโ”€โ”€ schemas/                # Device type schemas (downloaded)
โ”œโ”€โ”€ certificates/           # Device certificates (if using)
โ”œโ”€โ”€ twin_schemas/          # Twin schemas
โ”œโ”€โ”€ tests/                  # Test suite
โ”‚   โ””โ”€โ”€ commands/
โ”œโ”€โ”€ logs/                   # Application logs
โ””โ”€โ”€ README.md

Logging

The simulator provides detailed logging for all operations:

  • ๐Ÿ” Authentication: OAuth2 flow, token management
  • โš™๏ธ Configuration: Device setup, schema downloads
  • ๐Ÿ”— Connectivity: IoT Hub connections, reconnection attempts
  • ๐Ÿ‘ฅ Twin Operations: Desired/reported property changes
  • ๐Ÿ“ก Telemetry: Message generation and transmission
  • ๐ŸŽฏ Commands: Command reception and response generation

Logs are written to both console and logs/simulator.log file.

Error Handling

Common Issues

Configuration Missing

# Initialize configuration if not exists
iot-simulator --init
# Edit the generated config/config.json with your settings

Authentication Failures

# Clear stored tokens and re-authenticate
del config\auth-tokens.json  # Windows
# rm config/auth-tokens.json  # Linux/macOS
iot-simulator

Connection Issues

# Check IoT Hub connectivity and device configuration
# Verify config/config.json settings
# Check config/device-config.json exists and is valid

Schema Validation Errors

# Re-run setup to download fresh schema
# Delete old schema files and device config
del schemas\*-schema.json config\device-config.json  # Windows
# rm schemas/*-schema.json config/device-config.json  # Linux/macOS
iot-simulator

Development

Development Setup

The project uses modern Python packaging with pyproject.toml. Development dependencies are automatically installed with the [dev] extra.

Package name: tgiot-device-simulator

# Install in development mode with all tools
pip install -e ".[dev]"

Available Development Commands

# Code formatting
black .                    # Format all Python files
isort .                   # Sort imports

# Linting
ruff check .              # Fast Python linter
mypy .                    # Type checking

# Testing
pytest                    # Run all tests
pytest tests/ -v          # Verbose test output
pytest --cov=simulator    # Run tests with coverage

# Build and install
pip install -e .          # Install package locally
pip install -e ".[dev]"   # Install with dev dependencies

# Package build (for distribution)
python -m build           # Build wheel and source distribution

Project Structure

โ”œโ”€โ”€ pyproject.toml        # Modern Python packaging configuration
โ”œโ”€โ”€ simulator/           # Main package
โ”‚   โ”œโ”€โ”€ main.py         # Primary application entry point
โ”‚   โ”œโ”€โ”€ api/            # Platform API clients
โ”‚   โ”œโ”€โ”€ auth/           # OAuth2 authentication
โ”‚   โ”œโ”€โ”€ config/         # Configuration management
โ”‚   โ”œโ”€โ”€ connectivity/   # IoT Hub client
โ”‚   โ”œโ”€โ”€ device_setup/   # CLI-based device configuration
โ”‚   โ”œโ”€โ”€ provisioning/   # Device provisioning logic
โ”‚   โ”œโ”€โ”€ telemetry/      # Telemetry sender
โ”‚   โ”‚   โ””โ”€โ”€ message_generators/  # Message type generators
โ”‚   โ”œโ”€โ”€ twin/           # Device twin manager
โ”‚   โ”œโ”€โ”€ commands/       # C2D command processing
โ”‚   โ””โ”€โ”€ utils/          # Utilities and logging
โ”‚       โ””โ”€โ”€ schema/     # Schema utilities
โ”œโ”€โ”€ config/             # Configuration files
โ”œโ”€โ”€ tests/              # Test suite
โ”œโ”€โ”€ schemas/            # Device type schemas
โ”œโ”€โ”€ certificates/       # Device certificates
โ”œโ”€โ”€ twin_schemas/       # Twin schemas
โ””โ”€โ”€ logs/               # Application logs

Entry Points:

  • iot-simulator - Console command (recommended)
  • iot-simulator --init - Initialize configuration
  • python -m simulator.main - Direct module execution

Tandem Platform Device Simulator - Bridging the gap between IoT devices and cloud platforms with intelligent simulation and monitoring capabilities.

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

tgiot_device_simulator-1.0.0.10.tar.gz (44.8 kB view details)

Uploaded Source

Built Distribution

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

tgiot_device_simulator-1.0.0.10-py3-none-any.whl (54.6 kB view details)

Uploaded Python 3

File details

Details for the file tgiot_device_simulator-1.0.0.10.tar.gz.

File metadata

File hashes

Hashes for tgiot_device_simulator-1.0.0.10.tar.gz
Algorithm Hash digest
SHA256 634fb9870505f02b1ed8d21fbac71049aa09bb56efd50676df9f4a517812205b
MD5 0d42f393c01658d6142fee7fdbb5d3e7
BLAKE2b-256 9e0b5a58253f03e565c549dcc610b60de954286f493e1af70dd020077ec16cdc

See more details on using hashes here.

File details

Details for the file tgiot_device_simulator-1.0.0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for tgiot_device_simulator-1.0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 2691037310cef84a9e33e67edd08a1a0e0816aec0074b6dd30ab3d981e9d21e8
MD5 0d81c54e2d0501c108de09cb138653f0
BLAKE2b-256 30abd09bb6bd11da209043c8f7bbdcf696a90c314218b8eb21e31567803c89cf

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