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.8 or higher
  • 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 iot-device-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 config\config.template.json config\config.json  # Windows
# cp config/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",
    "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/"
    }
  },
  "messaging": {
    "intervals": {
      "measurement": 60,
      "sw_logs": 180,
      "state": 120,
      "events": 300
    },
    "enabled": {
      "measurement": true,
      "sw_logs": false,
      "state": true,
      "events": true
    }
  }
}

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 (modern way)
iot-simulator

# Alternative way
python main.py

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

    • Fill Device Details:

      • 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] Applied default configurations from schema to IoT Hub
[TWIN] Desired property updated from IoT Hub: telemetryInterval = 45
[TWIN] Reported property set to IoT Hub: telemetryInterval = 45

Telemetry Transmission

[D2C] Sending measurement: {"temperature": 23.5, "humidity": 65.2}
[D2C] Sending status: {"batteryLevel": 85, "signalStrength": -67}
[D2C] Sending alert: {"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-08-24T10:30:00Z"}

Project Structure

device-simulator/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ auth/                 # OAuth2 authentication
โ”‚   โ”œโ”€โ”€ config/              # Configuration management
โ”‚   โ”œโ”€โ”€ connectivity/         # IoT Hub connection
โ”‚   โ”œโ”€โ”€ twin/                # Device Twin handling
โ”‚   โ”œโ”€โ”€ telemetry/           # D2C message generation
โ”‚   โ”œโ”€โ”€ commands/            # C2D command processing
โ”‚   โ”œโ”€โ”€ cli/                 # Command-line interface for setup
โ”‚   โ””โ”€โ”€ utils/               # Shared utilities
โ”œโ”€โ”€ config/
โ”‚   โ”œโ”€โ”€ config.json          # Application configuration
โ”‚   โ”œโ”€โ”€ device-config.json   # Device-specific config
โ”‚   โ””โ”€โ”€ config.template.json # Configuration template
โ”œโ”€โ”€ schemas/                 # Device type schemas
โ”œโ”€โ”€ logs/                    # Application logs
โ”œโ”€โ”€ requirements.txt         # Python dependencies
โ”œโ”€โ”€ main.py                  # Application entry point
โ””โ”€โ”€ 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

Authentication Failures

# Clear stored tokens
del config\auth-tokens.json  # Windows
# rm config/auth-tokens.json  # Linux/macOS
python main.py

Connection Issues

# Verify connection string
python -m src.utils.verify_connection

Schema Validation Errors

# Re-download schema
python -m src.config.refresh_schema

Development

Development Setup

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

# 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

Project Structure

โ”œโ”€โ”€ pyproject.toml        # Modern Python packaging configuration
โ”œโ”€โ”€ main.py              # Legacy entry point (wrapper)
โ”œโ”€โ”€ simulator/           # Main package
โ”‚   โ”œโ”€โ”€ main.py         # Primary application entry point
โ”‚   โ”œโ”€โ”€ auth/           # OAuth2 authentication
โ”‚   โ”œโ”€โ”€ config/         # Configuration management  
โ”‚   โ”œโ”€โ”€ connectivity/   # IoT Hub client
โ”‚   โ”œโ”€โ”€ device_setup/   # Device configuration
โ”‚   โ”œโ”€โ”€ provisioning/   # API-based provisioning
โ”‚   โ”œโ”€โ”€ telemetry/      # Telemetry sender
โ”‚   โ”œโ”€โ”€ twin/           # Device twin manager
โ”‚   โ””โ”€โ”€ utils/          # Utilities and logging
โ”œโ”€โ”€ config/             # Configuration files
โ”œโ”€โ”€ tests/              # Test suite
โ””โ”€โ”€ logs/               # Application logs

Entry Points:

  • iot-simulator - Modern console command (recommended)
  • python main.py - Legacy compatibility wrapper
  • python -m simulator.main - Direct module execution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: tgiot_device_simulator-1.0.0.tar.gz
  • Upload date:
  • Size: 38.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for tgiot_device_simulator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e5dcde364f2efafa1f54050f052bf1cf44e32de23abd42465ad0971439307b9f
MD5 48bcd084c722119121b428a1522e05c2
BLAKE2b-256 5d2a5293133d9032638d26ffac683e56575d187f194f3b9d9e3cf53c4d556871

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tgiot_device_simulator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6519408267cc8aabf630d53dac99b711f03f26bb4652df0717b2612f2c4a56b9
MD5 d4492a85c978ef7cda7323193572d30a
BLAKE2b-256 f2422abb2ee22d83257068fd16487e5096b268488252f0c11011f570196ad416

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