https://github.com/luckyjoy/virtual_sensor_simulation
Create 100 virtual sensors that the MQTTTransport successfully connects to a MQTT broker.
https://github.com/luckyjoy/virtual_sensor_simulation
asyncio-python mqtt mqtt-broker python3 sensors simulations
Last synced: 30 days ago
JSON representation
Create 100 virtual sensors that the MQTTTransport successfully connects to a MQTT broker.
- Host: GitHub
- URL: https://github.com/luckyjoy/virtual_sensor_simulation
- Owner: luckyjoy
- Created: 2025-10-11T16:17:34.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-10-11T18:54:03.000Z (8 months ago)
- Last Synced: 2025-10-11T19:02:15.127Z (8 months ago)
- Topics: asyncio-python, mqtt, mqtt-broker, python3, sensors, simulations
- Language: Python
- Homepage: https://github.com/luckyjoy/virtual_sensor_simulation
- Size: 47.9 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# π Virtual Sensor Simulator (100+ Sensors)
[](https://www.python.org/)
[](LICENSE)
[](https://github.com/luckyjoy/virtual_sensor_simulation/actions)
[](https://hub.docker.com/)
[](https://mqtt.org/)
A high-performance **virtual IoT sensor simulator** built for **embedded systems** and **firmware CI/CD** environments.
It enables **largeβscale load testing**, **data validation**, and **MQTT / HTTP transport** performance benchmarking.
---
## π Overview
The simulator uses **Python asyncio** to achieve high concurrency, while allowing full configuration of:
- Message rate, jitter, and duration
- Sensor payload schemas and noise
- Fault injection and recovery
- Logging and telemetry analysis
### π― Primary Goals
- Validate sensor message generation and JSON serialization
- Benchmark MQTT and HTTP transports
- Assess system throughput (e.g. 100+ msgs/sec)
- Provide consistent configuration via CLI or YAML
---
### βοΈ Installation
```bash
git clone git clone https://github.com/luckyjoy/virtual_sensor_simulation.git
cd robotics_tdd
pip install -r requirements.txt # Optional for local testing
```
---
## π MQTT Architecture
**MQTT (Message Queuing Telemetry Transport)** is a lightweight pub/sub protocol suited for IoT or constrained / lossy networks.
| Role | Description |
|-------------|---------------------------------------------|
| **Publisher** | Sends sensor data messages to the broker |
| **Subscriber** | Receives messages from broker subscriptions |
| **Broker** | Routes messages between publishers & subscribers |
---
## πΊοΈ MQTT Flow Diagram
```mermaid
graph TD
A["Virtual Sensors"] --> B["MQTT Broker (Mosquitto)"]
B --> C["Subscribers / Consumers"]
C --> D["Data Platform or Dashboard"]
```
---
## π Project Structure
```
virtual_sensor_simulation/
βββ .github/
β βββ workflows/
β βββ ci.yml
βββ sensor_sim/
β βββ __init__.py
β βββ config.yaml
β βββ simulator.py
β βββ β¦ (other modules, classes, utils, etc.)
βββ Dockerfile
βββ docker-compose.yml
βββ requirements.txt
βββ run_sim.py
βββ .dockerignore
βββ .gitignore
βββ README.md
```
---
## βοΈ Features
- π§© Async simulation supporting **100β10,000 sensors** (host / network dependent)
- π Two transport modes: *MQTT* (QoS 0 / 1 via `asyncio-mqtt`) or *HTTP* (POST via `aiohttp`)
- β‘ Fully configurable: message rate, jitter, duration, schemas, and fault injection
- π§ͺ Per-sensor identity: battery models, location, noise profiles
- π Optional CSV logging of all emitted messages
- π§ Graceful shutdown & built-in backpressure handling
- π§± CIβfriendly: designed to run in GitHub Actions, Jenkins, or other pipelines
---
## π Quick Start
### 1. Create & activate a Python virtual environment
```bash
python3 -m venv .venv && source .venv/bin/activate
# On Windows:
python -m venv .venv && .venv\Scripts\activate
```
### 2. Install dependencies
```bash
pip install -r requirements.txt
```
### 3. Run 100 sensors over MQTT
```bash
python run_sim.py --count 100 --transport mqtt --mqtt-host localhost --mqtt-port 1883 --topic-prefix sim/sensors --rate 1.0
```
### 4. Run over HTTP
```bash
python run_sim.py --count 100 --transport http --http-url http://localhost:8080/ingest --rate 1.0
```
### 5. Use a config file
```bash
python run_sim.py --config sensor_sim/config.yaml
```
### 6. Using Docker + Mosquitto
```bash
docker compose up --build
# In another terminal:
docker compose run --rm simulator python run_sim.py --count 100 --transport mqtt --mqtt-host mosquitto --topic-prefix sim/sensors
```
---
## π§ͺ Example Payload
```json
{
"sensor_id": "vs-0042",
"ts": "2025-08-28T21:00:00.123Z",
"battery_pct": 92.3,
"temperature_c": 24.8,
"humidity_pct": 41.2,
"firmware": "1.2.3",
"status": "OK",
"seq": 512
}
```
---
## π§± CI/CD Smoke Test Example
Launch 20 sensors at 1β―Hz for 60 seconds:
```bash
python run_sim.py --count 20 --duration 60 --rate 1 --transport mqtt --mqtt-host $BROKER_HOST
```
---
## π§ Tips & Best Practices
- Start small (e.g. 20 sensors) before scaling up.
- Use flags like `--drop-rate`, `--spike-rate`, `--fault-every` to simulate edge conditions.
- Prefer asyncio concurrency over multiple processes for efficiency.
- Monitor CPU, memory, and network utilization at scale.
---
## π§© License & Contributions
This project is licensed under the **MIT License**.
Contributions, issues, and pull requests are welcome β feel free to open discussions or propose enhancements.
---
Β© 2025 Virtual Sensor Simulator | Maintained by [luckyjoy](https://github.com/luckyjoy)