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 (supportsYYYY-MM-DD)--end-date: End date (supportsYYYY-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:
- Create a virtual environment
- Install the package in development mode with all dependencies
- Copy configuration template
- 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
enabledvalue tofalse - 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:
-
๐ CLI-based Authorization
- Follow OAuth2 login prompts in terminal
- Authenticate with IoT Platform credentials via browser
- Grant device management permissions
-
โ๏ธ 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.
-
-
๐พ 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 configurationpython -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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tgiot_device_simulator-1.0.0.8.tar.gz.
File metadata
- Download URL: tgiot_device_simulator-1.0.0.8.tar.gz
- Upload date:
- Size: 42.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
629e3735a7aa65277304a9e2fa634278afab7305369085817945d3bdfd777064
|
|
| MD5 |
18b4697179b698301b365129881bcf30
|
|
| BLAKE2b-256 |
0dcce4ffac32233fbdf06ec17b6a11e4a6083b32105e82750bb7a98aa0f81707
|
File details
Details for the file tgiot_device_simulator-1.0.0.8-py3-none-any.whl.
File metadata
- Download URL: tgiot_device_simulator-1.0.0.8-py3-none-any.whl
- Upload date:
- Size: 52.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d70507b74022e30474c35ebe7c6e594333af9f80017bdf959a32cfe2bf10a466
|
|
| MD5 |
52898d5f537fbc17bbe289e7d473e556
|
|
| BLAKE2b-256 |
c10e1b37bc25e4640ea2ea3ffdfe735beab02002dbb5d1838fba5e972a472ebf
|