Skip to content

Repository files navigation

IoT Device Monitoring & Data Visualization

A university web programming project built with Node.js, Express, PostgreSQL, and vanilla JavaScript + Chart.js.


📁 Project Structure

webbbbbb/
├── backend/
│   ├── config/db.js          # DB connection pool
│   ├── controllers/          # Business logic
│   ├── middleware/           # JWT auth & error handling
│   ├── models/               # SQL query functions
│   ├── routes/               # Express routers
│   ├── utils/                # Helpers (API key gen)
│   ├── schema.sql            # DB schema & indexes
│   ├── app.js                # Express setup
│   └── server.js             # Entry point
├── frontend/
│   ├── index.html            # Login page
│   ├── register.html
│   ├── dashboard.html        # Overview + charts
│   ├── devices.html          # Device CRUD
│   ├── device-detail.html    # Per-device chart
│   ├── alerts.html           # Alert notifications
│   ├── css/style.css
│   └── js/                   # Page-specific JS
├── simulator.js              # Fake IoT device sender
└── package.json

⚙️ Setup Instructions

1. Install Dependencies

npm install

2. Configure Environment Variables

Copy .env.example to .env and fill in your values:

cp backend/.env.example backend/.env

Edit backend/.env:

PORT=3000
DB_HOST=localhost
DB_PORT=3306
DB_NAME=iot_monitoring
DB_USER=root
DB_PASSWORD=yourpassword
JWT_SECRET=your_super_secret_jwt_key_here

3. Create PostgreSQL Database

mysql -u root -p -e "CREATE DATABASE iot_monitoring CHARACTER SET utf8mb4;"
mysql -u root -p iot_monitoring < backend/schema.sql

4. Start the Server

# Development (with auto-reload)
npm run dev

# Production
npm start

5. Open the Frontend

Open your browser and navigate to:

http://localhost:3000

🤖 Running the Simulator

If you don't have a physical IoT device, use the simulator:

# First, create a device via the UI and copy its API key
# Then run:
node simulator.js <YOUR_API_KEY>

# Example:
node simulator.js abc123def456...

The simulator sends temperature + humidity readings every 5 seconds.


🔌 API Endpoints

Auth

Method Endpoint Description
POST /api/auth/register Register a new user
POST /api/auth/login Login, returns JWT token

Devices (JWT required)

Method Endpoint Description
GET /api/devices List all your devices
POST /api/devices Add a new device
GET /api/devices/:id Get device details
PUT /api/devices/:id Update device
DELETE /api/devices/:id Delete device

Sensor Data

Method Endpoint Auth Description
POST /api/sensor/data API Key header Submit sensor reading
GET /api/sensor/data/:deviceId JWT Get readings
GET /api/sensor/stats/:deviceId JWT Get statistics

Alerts (JWT required)

Method Endpoint Description
GET /api/alerts Get all alerts
PATCH /api/alerts/:id/read Mark as read
DELETE /api/alerts/:id Delete alert

📊 Database Schema

  • users — Registered users with hashed passwords
  • devices — IoT devices with unique API keys and alert thresholds
  • sensor_data — Time-series temperature & humidity readings (indexed)
  • alerts — Auto-generated alerts when thresholds are exceeded

🛠️ Technologies Used

  • Backend: Node.js, Express.js, JWT, bcryptjs
  • Database: MySQL (with mysql2 driver)
  • Frontend: HTML5, CSS3, Bootstrap 5, Vanilla JS, Chart.js
  • Architecture: MVC pattern, RESTful API

About

Lightweight Node.js app that simulates sensors, devices, and alerts.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages