A complete real-time weight monitoring system with multiple communication protocols (Socket.IO and MQTT), featuring a modern web dashboard with live graphs and data persistence.
- Real-time Monitoring: Live weight display with 1-second updates
- 5-Minute Graph: Interactive Chart.js graph with historical data
- Dual Protocol Support:
- Socket.IO (WebSocket) for direct connections
- MQTT for IoT standard protocol
- Server-Side Persistence: Data survives page refreshes
- Connection Monitoring: Visual status indicators
- Activity Logging: Comprehensive event logging
- CSV Export: Download historical data
- Responsive Design: Beautiful UI that works on all devices
- Node.js (v14 or higher)
- npm or yarn
- MQTT Broker (optional, for MQTT version):
- Local: Mosquitto, EMQX, HiveMQ
- Or use public test broker
-
Clone the repository:
git clone <repository-url> cd IOT-Lab
-
Install dependencies:
npm install
-
Configure environment:
cp .env.example .env
Edit
.envto configure your setup. See CONFIGURATION.md for details.
# Terminal 1 - Start server
npm start
# Terminal 2 - Start weight sensor
npm run weight
# Browser - Open dashboard
http://localhost:3000# Terminal 1 - Start MQTT server
npm run start:mqtt
# Terminal 2 - Start MQTT weight sensor
npm run mqtt:device
# Browser - Open dashboard
http://localhost:3000See QUICKSTART-MQTT.md for detailed MQTT setup.
IOT-Lab/
โโโ server/
โ โโโ server.js # Socket.IO server
โ โโโ server-mqtt.js # MQTT to WebSocket bridge
โโโ device/
โ โโโ weight-sensor.js # Socket.IO weight sensor
โ โโโ mqtt-weight-sensor.js # MQTT weight sensor
โโโ public/
โ โโโ index.html # Web dashboard
โ โโโ styles.css # Styling
โ โโโ app.js # Frontend logic
โโโ .env.example # Environment template
โโโ .env # Your configuration (git-ignored)
โโโ package.json # Dependencies
| Command | Description |
|---|---|
npm start |
Start Socket.IO server |
npm run start:mqtt |
Start MQTT-to-WebSocket server |
npm run dev |
Development mode with auto-restart (Socket.IO) |
npm run dev:mqtt |
Development mode with auto-restart (MQTT) |
npm run weight |
Start Socket.IO weight sensor simulator |
npm run mqtt:device |
Start MQTT weight sensor simulator |
Environment variables in .env:
PORT=3000 # Server port
MQTT_BROKER=mqtt://test.mosquitto.org:1883 # MQTT broker URL
MQTT_TOPIC=weight/sensor/# # MQTT topic pattern
DEVICE_ID=WEIGHT_SCALE_001 # Device identifier
MAX_HISTORY_MINUTES=5 # Data retention timeSee CONFIGURATION.md for complete configuration guide.
- README-MQTT.md - MQTT implementation details
- README-WEIGHT.md - Weight monitoring system details
- CONFIGURATION.md - Complete configuration guide
- QUICKSTART-MQTT.md - MQTT quick start guide
ESP32/Arduino:
#include <WiFi.h>
#include <PubSubClient.h>
const char* mqtt_server = "your-server-ip";
const char* topic = "weight/sensor/SCALE_001";
void loop() {
float weight = readScale(); // Your sensor code
char msg[10];
dtostrf(weight, 6, 2, msg);
client.publish(topic, msg);
delay(1000);
}Raspberry Pi/Python:
import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect("localhost", 1883)
while True:
weight = read_weight_sensor() # Your sensor code
client.publish("weight/sensor/SCALE_001", str(weight))
time.sleep(1)JavaScript/Node.js:
const io = require('socket.io-client');
const socket = io('http://localhost:3000');
socket.emit('register-device', {
deviceId: 'SCALE_001',
deviceType: 'Weight Scale'
});
setInterval(() => {
const weight = readWeightSensor();
socket.emit('device-data', weight.toString());
}, 1000);GET /api/health
Returns server status and statistics.
GET /api/history
Returns last 5 minutes of weight data.
Device (Socket.IO) โ Server (WebSocket) โ Web Dashboard
Device (MQTT) โ MQTT Broker โ Server (Bridge) โ Web Dashboard (WebSocket)
For production deployment:
- โ Use HTTPS/WSS
- โ Enable authentication
- โ Use secure MQTT (mqtts://)
- โ Implement access control
- โ
Keep
.envout of version control - โ Use strong passwords
- โ Enable TLS certificates
# Windows - Find and kill process on port 3000
Get-NetTCPConnection -LocalPort 3000 | Select-Object -ExpandProperty OwningProcess | ForEach-Object { Stop-Process -Id $_ -Force }- Check MQTT broker is running
- Verify broker URL in
.env - Test with public broker:
mqtt://test.mosquitto.org:1883
- Check browser console for errors
- Verify device is connected (check server logs)
- Ensure topic patterns match (MQTT)
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License
Auto-restart during development:
npm run dev # Socket.IO version
npm run dev:mqtt # MQTT version- Large Weight Display: 8rem font size, center-positioned
- Live Graph: Chart.js with 300 data points (5 minutes ร 60 seconds)
- Server Persistence: Data stored in server memory
- Auto-cleanup: Old data automatically removed after 5 minutes
- Dual Status: Separate indicators for WebSocket and MQTT
- Export Functionality: Download logs as CSV
- Responsive Design: Glassmorphism UI with gradient background
Weight data should be sent as a string:
"75.50"
The system will parse and display it automatically.
| Feature | Socket.IO | MQTT |
|---|---|---|
| Setup Complexity | Simple | Requires broker |
| Protocol Standard | Proprietary | IoT standard |
| Scalability | Medium | High |
| Reliability | Good | Excellent |
| IoT Integration | Custom | Native |
| Message Queuing | No | Yes |
| Offline Messages | No | Yes (with broker) |
| Best Use Case | Direct connections | IoT deployments |
For issues and questions:
- Check documentation files
- Review server/device logs
- Test with simulators first
- Verify environment configuration
Made with โค๏ธ for IoT weight monitoring