This repository contains a Flask application for managing and visualizing smart home sensor data with MongoDB.
- Python 3.6+
- MongoDB Atlas account
- Git
- AWS account (for Queryable Encryption)
-
Clone this repository:
git clone https://github.com/yourusername/mongodb-smart-home.git cd mongodb-smart-home -
Create and activate a virtual environment:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
The setup.py script handles the initial application setup:
python setup.pyThis script will:
- Install all required Python packages
- Create a
.secretsfile with your MongoDB credentials - Export the credentials to your environment
If you need to update your MongoDB credentials:
- Delete the existing
.secretsfile - Run
setup.pyagain - Enter your new credentials when prompted
The setup_queryable_encryption.py script configures AWS credentials for Queryable Encryption:
python setup_queryable_encryption.pyThis script will:
- Check for existing AWS credentials in
.secrets - If credentials exist, it will:
- Display the existing credentials
- Export them to the environment
- Exit without prompting
- If credentials don't exist, it will:
- Prompt for AWS credentials
- Save them to
.secrets - Export them to the environment
Required AWS credentials:
- AWS Access Key
- AWS Secret Key
- AWS KMS Key ID
- AWS KMS ARN
To update AWS credentials:
- Delete the
.secretsfile - Run
setup.pyto set up MongoDB credentials - Run
setup_queryable_encryption.pyto set up AWS credentials
After setting up both MongoDB and AWS credentials:
-
Start the application:
./run_app.sh
The application will start on http://localhost:5000
-
Access the application:
- Main dashboard: http://localhost:5000
- Queryable Encryption demo: http://localhost:5000/qe_demo
The application uses environment variables for configuration. These are managed through the .secrets file:
{
"MONGODB_URI": "your_cluster.mongodb.net",
"MONGODB_USERNAME": "your_username",
"MONGODB_PASSWORD": "your_password",
"AWS_ACCESS_KEY": "your_aws_access_key",
"AWS_SECRET_KEY": "your_aws_secret_key",
"AWS_KMS_KEY_ID": "your_kms_key_id",
"AWS_KMS_ARN": "your_kms_arn"
}- Real-time electricity usage visualization
- 5-minute interval data aggregation
- Time series data storage in MongoDB
- Queryable encryption support for sensitive data
The application stores sensor readings with the following structure:
{
"deviceId": "string",
"brand": "string",
"model": "string",
"device_name": "string",
"category": "string",
"current_usage": number,
"temperature": number,
"pressure": number,
"device_state": "string",
"battery_level": number,
"Timestamp": ISODate
}If you encounter connection issues:
-
Verify your MongoDB credentials:
env | grep MONGODB -
Verify your AWS credentials:
env | grep AWS -
Check MongoDB Atlas:
- Ensure your IP is whitelisted
- Verify your database user has correct permissions
- Confirm your cluster is running
-
Check the application logs:
./run_app.sh
-
Common errors:
- "Connection refused": Check your MongoDB URI and credentials
- "Invalid URI host": Verify your MongoDB URI format
- "Authentication failed": Check your username and password
- "KMS error": Verify your AWS credentials and KMS permissions
- The
.secretsfile contains sensitive credentials - Add
.secretsto your.gitignorefile - Never commit credentials to version control
- Use strong passwords for your MongoDB user
- For Queryable Encryption, ensure your CMK is stored securely
- Regularly rotate your encryption keys
This project is licensed under the MIT License - see the LICENSE file for details.