Real-time gas monitoring backend using Node.js, Express, Socket.IO, and PostgreSQL.
This version is configured for demo use:
- no authentication required
- CO2 and gas level only
- default demo user created automatically
- Arduino data ingestion via REST
- Real-time WebSocket broadcast to frontend
- PostgreSQL persistence for readings, alerts, and control state
- Alert generation based on thresholds
- Manual control endpoints (fan, valve, alarm)
- Metrics endpoints for current and historical data
expresssocket.iopgaxioscorsdotenv
backend/
config/database.js
db/schema.sql
db.js
middlewares/
routes/
services/
sockets/
server.js
- Install dependencies:
npm install- Create
.envin the project root:
PORT=3000
HOST=0.0.0.0
DATABASE_URL=postgresql://postgres:YOUR_PASSWORD@localhost:5433/smart_safety
DB_SSL=false
DEFAULT_USER_ID=AGAS-2026-001- Start server:
npm startServer URL: http://localhost:3000
Quick Start:
- Sign up at digitalocean.com
- Get $200 free credit: GitHub Student Pack
- Deploy: cloud.digitalocean.com/apps/new
📖 See DIGITALOCEAN_QUICK_START.md - 5-minute setup! 📖 See DIGITALOCEAN_DEPLOYMENT.md - Full guide
Cost: ~$12/month (FREE for 16+ months with student credits!)
Quick Start:
azd auth login
azd env set DATABASE_ADMIN_PASSWORD "YourSecurePassword123!"
azd up📖 See AZURE_QUICK_START.md for a 5-minute deployment guide. 📖 See AZURE_DEPLOYMENT.md for complete documentation.
Note: Azure for Students may have region restrictions. See AZURE_REGIONS.md.
Only these sensor fields are used now:
{
"sensorId": "SENSOR-001",
"co2": 920,
"gas_level": 6
}Supported payload styles:
- flat payload:
co2,gas_level - nested payload:
{ readings: { co2, gas_level } }
dangerwhenco2 >= 1500orgas_level >= 20warningwhenco2 >= 1000orgas_level >= 10- otherwise
safe
GET /-> health text response
POST /api/gas-dataGET /api/fetch-gas-data?apiUrl=...
Example:
curl -X POST http://localhost:3000/api/gas-data \
-H "Content-Type: application/json" \
-d '{"sensorId":"SENSOR-001","co2":920,"gas_level":6}'GET /v1/metrics/currentGET /v1/metrics/history?period=24h&interval=5mGET /v1/control/statePOST /v1/control/toggleGET /v1/alerts?status=all&limit=50POST /v1/alerts/:id/resolve
Toggle body example:
{
"device": "fan",
"state": true
}Connect client:
const socket = io('http://localhost:3000');Events from server:
connectedgas-data-updatesensor_updatefetch-successfetch-error
- Send sample reading:
curl -X POST http://localhost:3000/api/gas-data \
-H "Content-Type: application/json" \
-d '{"sensorId":"SENSOR-001","co2":920,"gas_level":6}'- Fetch latest metrics:
curl http://localhost:3000/v1/metrics/currentExpected readings includes only:
co2gas_level
- Authentication is disabled intentionally for hackathon demo.
- Default user code seeded:
AGAS-2026-001. v1routes require database availability.
HACKATHON_QUICK_START.mdARDUINO_API_SPEC.mdFRONTEND_API_SPEC.mdDATABASE_SETUP.mdDOCUMENTATION_INDEX.md
ISC