-
Copy the example environment file:
cp .env.example .env
-
Edit
.envwith your specific configuration
- Description: HTTP/WebSocket server port
- Default:
3000 - Example:
PORT=3000
- Description: MQTT broker URL
- Default:
mqtt://localhost:1883 - Examples:
- Local Mosquitto:
mqtt://localhost:1883 - Public test broker:
mqtt://test.mosquitto.org:1883 - Secure connection:
mqtts://broker.example.com:8883 - With credentials:
mqtt://username:password@broker.example.com:1883
- Local Mosquitto:
- Note: Use
mqtts://for secure TLS connections in production
- Description: MQTT topic pattern to subscribe to
- Default:
weight/sensor/# - Examples:
- Single device:
weight/sensor/SCALE_001 - All sensors:
weight/sensor/# - Specific location:
weight/sensor/kitchen/#
- Single device:
- Note: The
#wildcard matches multiple levels
- Description: Unique identifier for the weight sensor device
- Default:
WEIGHT_SCALE_001 - Example:
DEVICE_ID=KITCHEN_SCALE - Note: Used by device simulator. Real devices should have unique IDs
- Description: How long to keep weight data in server memory (minutes)
- Default:
5 - Range:
1to60(recommended) - Example:
MAX_HISTORY_MINUTES=10 - Note: Higher values use more memory but retain more history
PORT=3000
MQTT_BROKER=mqtt://localhost:1883
MQTT_TOPIC=weight/sensor/#
DEVICE_ID=WEIGHT_SCALE_001
MAX_HISTORY_MINUTES=5PORT=3000
MQTT_BROKER=mqtt://test.mosquitto.org:1883
MQTT_TOPIC=weight/sensor/#
DEVICE_ID=WEIGHT_SCALE_001
MAX_HISTORY_MINUTES=5PORT=3000
MQTT_BROKER=mqtts://username:password@mqtt.example.com:8883
MQTT_TOPIC=production/weight/sensor/#
DEVICE_ID=PROD_SCALE_001
MAX_HISTORY_MINUTES=10# Terminal 1 - Server
PORT=3000
MQTT_BROKER=mqtt://localhost:1883
MQTT_TOPIC=weight/sensor/#
MAX_HISTORY_MINUTES=5
# Terminal 2 - Device 1
MQTT_BROKER=mqtt://localhost:1883
DEVICE_ID=SCALE_001
# Terminal 3 - Device 2
MQTT_BROKER=mqtt://localhost:1883
DEVICE_ID=SCALE_002# Edit .env file
nano .env
# Run normally
npm run start:mqtt
npm run mqtt:deviceWindows PowerShell:
$env:MQTT_BROKER = "mqtt://test.mosquitto.org:1883"
$env:DEVICE_ID = "MY_SCALE"
npm run start:mqttLinux/macOS:
export MQTT_BROKER="mqtt://test.mosquitto.org:1883"
export DEVICE_ID="MY_SCALE"
npm run start:mqttWindows PowerShell:
$env:MQTT_BROKER = "mqtt://test.mosquitto.org:1883"; npm run mqtt:deviceLinux/macOS:
MQTT_BROKER="mqtt://test.mosquitto.org:1883" npm run mqtt:device- ✅ Use
.envfile - ✅ Test with public brokers
- ✅ Keep
.envin.gitignore
- ✅ Use environment variables from hosting platform
- ✅ Use secure MQTT (mqtts://)
- ✅ Enable authentication on MQTT broker
- ✅ Use strong passwords
- ✅ Restrict topic access
- ✅ Never commit
.envto version control - ✅ Rotate credentials regularly
- ✅ Use TLS certificates
- ✅ Implement access control lists (ACL)
Solution: Ensure .env file exists in project root
# Check if file exists
ls -la .env
# If not, copy from example
cp .env.example .envSolutions:
- Check MQTT broker is running
- Verify
MQTT_BROKERURL is correct - Check firewall settings
- Test with public broker first
Solution: Check topic patterns match:
# Publisher
weight/sensor/DEVICE_001
# Subscriber patterns that match:
weight/sensor/# ✅
weight/sensor/DEVICE_001 ✅
weight/# ✅
# ✅
# Patterns that DON'T match:
weight/sensor/+ ❌ (+ matches single level only)
sensor/# ❌The application loads environment variables in this order:
- System environment variables (highest priority)
.envfile- Default values in code (lowest priority)
Example:
# .env file has: MQTT_BROKER=mqtt://localhost:1883
# But you run:
$env:MQTT_BROKER = "mqtt://test.mosquitto.org:1883"; npm run start:mqtt
# Result: Uses mqtt://test.mosquitto.org:1883 (command line wins)