A modular monolith IoT platform built with Fastify that manages business data and IoT device communications.
The platform uses a dual-database approach:
- PostgreSQL: For business data (enterprises, employees, device registrations)
- MongoDB: For IoT data (device readings, sensor data)
- MQTT: For real-time device communication
- Modular Monolith Architecture
- Real-time device data processing
- Device command & control
- Scalable data storage
- Type-safe development with TypeScript
- Node.js (v16 or higher)
- Docker and Docker Compose
- TypeScript knowledge
- Basic understanding of MQTT protocol
- Clone the repository
git clone <repository-url>
cd iot-platform
- Install dependencies
npm install
- Setup environment variables
Create a
.env
file in the root directory:
PORT=3000
MONGO_URI=mongodb://root:example@localhost:27017/iot_data?authSource=admin
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=iot_business
JWT_SECRET=your-secret-key
- Start the infrastructure
docker-compose up -d
This will start:
- MongoDB (port 27017)
- MongoDB Express (port 8081)
- PostgreSQL (port 5432)
- pgAdmin (port 8082)
- MQTT Broker (port 1883)
- Run the application
npm run dev
src/
├── modules/
│ ├── business/ # Business module (PostgreSQL)
│ │ ├── entities/
│ │ ├── routes/
│ │ └── services/
│ ├── iot/ # IoT module (MongoDB)
│ │ ├── models/
│ │ ├── routes/
│ │ └── services/
│ └── mqtt/ # MQTT module
│ ├── plugin.ts
│ └── service.ts
├── shared/
│ ├── config/
│ ├── plugins/
│ └── types/
└── tools/
└── device-simulator.ts
GET /api/business/enterprises
- List enterprisesPOST /api/business/enterprises
- Create enterpriseGET /api/business/enterprises/:id
- Get enterprise details
GET /api/iot/readings
- Get device readingsGET /api/iot/readings/aggregated
- Get aggregated readings
POST /api/mqtt/devices/:deviceId/command
- Send command to devicePOST /api/mqtt/publish
- Publish MQTT message
The project includes a device simulator for testing:
# Start a simulated device
ts-node src/tools/device-simulator.ts
- Update Publishing Interval
curl -X POST http://localhost:3000/api/mqtt/devices/{deviceId}/command \
-H "Content-Type: application/json" \
-d '{
"command": "updateInterval",
"payload": {
"interval": 10000
}
}'
- Reset Device
curl -X POST http://localhost:3000/api/mqtt/devices/{deviceId}/command \
-H "Content-Type: application/json" \
-d '{
"command": "reset",
"payload": {}
}'
- Sleep/Wake Commands
# Sleep
curl -X POST http://localhost:3000/api/mqtt/devices/{deviceId}/command \
-H "Content-Type: application/json" \
-d '{
"command": "sleep",
"payload": {}
}'
# Wake
curl -X POST http://localhost:3000/api/mqtt/devices/{deviceId}/command \
-H "Content-Type: application/json" \
-d '{
"command": "wake",
"payload": {}
}'
- Create entity in
src/modules/business/entities/
- Create routes in
src/modules/business/routes/
- Register routes in
src/modules/business/index.ts
- Create model in
src/modules/iot/models/
- Create service in
src/modules/iot/services/
- Add routes in
src/modules/iot/routes/
- MongoDB Express: http://localhost:8081
- pgAdmin: http://localhost:8082
- Email: admin@admin.com
- Password: admin
[Add contribution guidelines here]
[Add license information here]