This repository contains the backend for a Smart Greenhouse System designed for remote monitoring and control of environmental conditions.
The backend is built on an IoT architecture using the MQTT protocol for hardware communication and a REST API with WebSockets for client-side interaction.
-
Real-time Data Monitoring
Receive live sensor data (temperature, humidity, soil moisture, etc.) via MQTT and stream it to the dashboard using Socket.io. -
Remote Control
Control actuators (fans, lamps, irrigation systems) securely via the API. -
Advanced Authentication
Secure registration and login with JWT (Access & Refresh Tokens) stored inhttpOnly
cookies. -
Sensor-Specific Data Collections
Each sensor type has its own MongoDB collection, optimized for time-series queries and varying data rates. -
Reporting System
Generate statistical reports (average, minimum, maximum) for custom date ranges. -
Modular & Scalable Architecture
Organized controllers, models, and routes for easy development and maintenance.
- Platform: Node.js
- Framework: Express.js
- Database: MongoDB (Mongoose ODM)
- IoT Protocol: MQTT
- Real-time Communication: Socket.io
- Authentication: JWT (Access & Refresh Tokens)
- Password Security: bcrypt
Hardware (ESP32/Arduino) → MQTT Broker → Backend (this project) → MongoDB
↓
Web Client (Frontend)
- Hardware (ESP32/Arduino): Collects sensor data and publishes it to MQTT topics.
- Backend: Subscribes to MQTT topics, processes data, and stores it in MongoDB.
- Frontend: Communicates via REST API & WebSockets to show live data and send commands.
- Command Dispatch: Backend publishes control commands → hardware executes them.
- Node.js (v16 or higher)
- MongoDB
- MQTT Broker (e.g., Mosquitto)
Clone the repository and install dependencies:
npm install
Create a .env
file (based on .env.example
):
# MongoDB Connection URI
MONGODB_URI=mongodb://localhost:27017/smart_greenhouse
# JWT Secrets
ACCESS_TOKEN_SECRET=your_strong_access_token_secret
REFRESH_TOKEN_SECRET=your_strong_refresh_token_secret
# MQTT Broker
MQTT_BROKER_URL=mqtt://localhost:1883
# Server Port
PORT=2000
Run the server:
npm start
The server will be available at:
http://localhost:2000
POST /auth/register
– Register a new userPOST /auth/login
– Login & receive tokens (cookies)POST /auth/refresh
– Refresh access tokenPOST /auth/logout
– Logout user
GET /sensor-summary
– Latest sensor readings summaryGET /sensor-last10
– Last 10 records for each sensor type
GET /get-control
– Get current actuator status (fan, lamp, etc.)POST /set-control
– Update actuator status
GET /reports
– Generate statistical report- Query Params:
startDate=YYYY-MM-DD&endDate=YYYY-MM-DD
- Query Params:
greenhouse/temperature
greenhouse/humidity
greenhouse/soil
greenhouse/control/command
Shady Nikooei