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

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.4.tar.gz (39.4 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.4-py3-none-any.whl (47.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for tgiot_device_simulator-1.0.0.4.tar.gz
Algorithm Hash digest
SHA256 c3e3cde04f5cb9e44cae33f841d017b5b38db628438cfe264fa6ecc2374608e1
MD5 dbc92ba6faf621e2fd124a6781bf0f44
BLAKE2b-256 7bdf0984e3080ba32e3431d095f40c135e6b3ed3ef931278ef4008f4a989cd69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tgiot_device_simulator-1.0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8af0d28a208f614b7cbcdabdcbaff1d4a2ffd345a834f49deb0ddebe07188248
MD5 47b3df87295af19b1614041d109d455b
BLAKE2b-256 6fbc0122b8e5a65876b94c666c285bbc9ae4c1cf2e475522a683c54859fed388

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