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:
- 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 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
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 (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:
-
๐ CLI-based Authorization
- Follow OAuth2 login prompts in terminal
- Authenticate with IoT Platform credentials via browser
- Grant device management permissions
-
โ๏ธ 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.
-
-
๐พ 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 wrapperpython -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
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.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5dcde364f2efafa1f54050f052bf1cf44e32de23abd42465ad0971439307b9f
|
|
| MD5 |
48bcd084c722119121b428a1522e05c2
|
|
| BLAKE2b-256 |
5d2a5293133d9032638d26ffac683e56575d187f194f3b9d9e3cf53c4d556871
|
File details
Details for the file tgiot_device_simulator-1.0.0-py3-none-any.whl.
File metadata
- Download URL: tgiot_device_simulator-1.0.0-py3-none-any.whl
- Upload date:
- Size: 47.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6519408267cc8aabf630d53dac99b711f03f26bb4652df0717b2612f2c4a56b9
|
|
| MD5 |
d4492a85c978ef7cda7323193572d30a
|
|
| BLAKE2b-256 |
f2422abb2ee22d83257068fd16487e5096b268488252f0c11011f570196ad416
|