This project implements a Web of Things (WoT) environment simulating a smart laboratory. It consists of three interconnected virtual devices: a Lamp, a Motion Sensor, and a Thermostat.
The system is built on a distributed architecture where each device operates as an independent web service. A central Gateway manages the device registry, user authentication, and real-time event propagation. Interaction is handled via RESTful interfaces and WebSockets for event-driven communication.
- Microservice Architecture: Each device (Lamp, Thermostat, Sensor) is an independent Node.js service.
- Discovery and Registration: Devices automatically register with the gateway on startup using a secure "System Token".
- Real-Time Dashboard: A web interface connects via Socket.IO to receive live status updates and control the devices.
- Secure Authentication: Token-based authentication system for users, with password hashing (bcrypt).
- Rule Engine: A central service on the gateway applies automation rules based on device states.
- Event Logging: All actions (connections, manual actions, state changes) are logged to the MongoDB database.
- Physical Simulation: The thermostat actively simulates temperature changes based on its current mode and target.
The system is composed of the following services, each running on a separate port:
-
Gateway:
http://localhost:3000- Central coordination point.
- Manages the device registry, authentication (users and system), and the Socket.IO server.
- Hosts the rule engine.
-
Lamp Service:
http://localhost:3001- Exposes actions (
turnOn,turnOff,setColor,setBrightness) and properties (on,color,brightness).
- Exposes actions (
-
Thermostat Service:
http://localhost:3002- Exposes actions (
setMode,setTarget) and properties (currentTemperature,targetTemperature,mode). - Simulates its own temperature.
- Exposes actions (
-
Motion Sensor Service:
http://localhost:3003- Exposes the action (
simulateMotion) and properties (motion,lastDetected).
- Exposes the action (
-
Database:
mongodb://localhost:27017/smartlab- Stores users and event logs.
-
Dashboard (Client)
- Web interface (HTML, CSS, JS) for user interaction.
- Backend: Node.js
- Framework: Express.js
- Real-time Communication: Socket.IO
- Database: MongoDB with Mongoose
- Frontend: Vanilla JavaScript (ES6+), HTML5, CSS3
- Security:
bcrypthashing, Custom authentication tokens.
- Node.js (v18+)
npm(oryarn)- MongoDB (local server running)
-
Clone the repository (if applicable)
git clone [YOUR_REPO_URL] cd [YOUR_PROJECT_FOLDER] -
Install dependencies (Assuming a
package.jsonexists at the root or in each service folder)npm install
Note: You may need to run this in each service folder (
gateway,services/lamp, etc.) if dependencies are managed separately. -
Start the database Ensure your MongoDB service is running on the default port (27017).
-
Start the services (Order is Important!)
You must start the Gateway first. On its first launch, it creates the
systemuser and generates aSYSTEM_TOKENin a.envfile at the root. The other services need this token to register.Open 4 separate terminals:
-
Terminal 1: Gateway
# Option 1: Using node node gateway/index.js # Option 2: Using npm script npm run dev # Wait to see: "Gateway on :3000" and "SYSTEM_TOKEN saved to .env"
-
Terminal 2: Lamp Service
# Option 1: Using node node services/lamp/app.js # Option 2: Using npm script npm run dev # Should display: "Lamp registered with ID: lamp-..."
-
Terminal 3: Thermostat Service
# Option 1: Using node node services/thermostat/app.js # Option 2: Using npm script npm run dev # Should display: "Thermostat registered with ID: thermostat-..."
-
Terminal 4: Motion Sensor Service
# Option 1: Using node node services/motion/app.js # Option 2: Using npm script npm run dev # Should display: "Motion Sensor registered with ID: motion-..."
(Note: Adjust paths like
gateway/index.jsbased on your project structure.npm run devassumes you have adevscript defined in thepackage.jsonof each service.) -
-
Access the Dashboard Open the
dashboard/index.htmlfile in your browser. You can use a browser extension like "Live Server" in VS Code to serve it locally.
The Rule Engine (RuleEngine.js) applies the following logic (based on the source code):
-
Light on Detection
- If:
motionis detected (becomestrue) AND the lamp is off. - Then: The lamp turns on (
turnOn). - And: The lamp turns off (
turnOff) automatically after 2 seconds.
- If:
-
Comfort Heating
- If: The thermostat's
currentTemperatureis < 18°C ANDmotionis active. - Then: The thermostat switches to
comfortmode.
- If: The thermostat's
-
Energy Saving (Inactivity)
- If: 5 minutes have passed since the
lastDetectedmotion. - Then: The lamp turns off (
turnOff) AND the thermostat switches toecomode. - (This rule is checked every 30 seconds).
- If: 5 minutes have passed since the
-
Manual Override
- If: A user manually changes the thermostat mode (
mode: 'manual'). - Then: All automation rules (above) are suspended for 30 seconds to respect the user's intent.
- If: A user manually changes the thermostat mode (