From 3f3e057b846b3edca0688c5680ab5e6a8020d412 Mon Sep 17 00:00:00 2001 From: Sami Yousaf Date: Thu, 1 Aug 2024 20:04:39 +0500 Subject: [PATCH] Update project files. --- Smart Environment using MQTT/.idea/.gitignore | 3 + .../inspectionProfiles/Project_Default.xml | 6 ++ .../inspectionProfiles/profiles_settings.xml | 6 ++ Smart Environment using MQTT/.idea/misc.xml | 10 +++ .../.idea/modules.xml | 8 ++ .../.idea/pythonProject2.iml | 10 +++ .../docs/METHODOLOGY.md | 13 ++++ .../docs/PROGRAMMING APPROACHES.md | 16 ++++ .../docs/PROJECT_MANAGEMENT.md | 17 +++++ .../docs/Prototypes.md | 15 ++++ Smart Environment using MQTT/docs/README.md | 42 +++++++++++ .../docs/requirements.txt | 21 ++++++ .../mqtt_communication.py | 53 +++++++++++++ .../sensor_integration.py | 40 ++++++++++ .../templates/index.html | 74 +++++++++++++++++++ .../ui_data_analysis.py | 68 +++++++++++++++++ 16 files changed, 402 insertions(+) create mode 100644 Smart Environment using MQTT/.idea/.gitignore create mode 100644 Smart Environment using MQTT/.idea/inspectionProfiles/Project_Default.xml create mode 100644 Smart Environment using MQTT/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 Smart Environment using MQTT/.idea/misc.xml create mode 100644 Smart Environment using MQTT/.idea/modules.xml create mode 100644 Smart Environment using MQTT/.idea/pythonProject2.iml create mode 100644 Smart Environment using MQTT/docs/METHODOLOGY.md create mode 100644 Smart Environment using MQTT/docs/PROGRAMMING APPROACHES.md create mode 100644 Smart Environment using MQTT/docs/PROJECT_MANAGEMENT.md create mode 100644 Smart Environment using MQTT/docs/Prototypes.md create mode 100644 Smart Environment using MQTT/docs/README.md create mode 100644 Smart Environment using MQTT/docs/requirements.txt create mode 100644 Smart Environment using MQTT/mqtt_communication.py create mode 100644 Smart Environment using MQTT/sensor_integration.py create mode 100644 Smart Environment using MQTT/templates/index.html create mode 100644 Smart Environment using MQTT/ui_data_analysis.py diff --git a/Smart Environment using MQTT/.idea/.gitignore b/Smart Environment using MQTT/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Smart Environment using MQTT/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Smart Environment using MQTT/.idea/inspectionProfiles/Project_Default.xml b/Smart Environment using MQTT/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..ac21435 --- /dev/null +++ b/Smart Environment using MQTT/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/Smart Environment using MQTT/.idea/inspectionProfiles/profiles_settings.xml b/Smart Environment using MQTT/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/Smart Environment using MQTT/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/Smart Environment using MQTT/.idea/misc.xml b/Smart Environment using MQTT/.idea/misc.xml new file mode 100644 index 0000000..ce32e39 --- /dev/null +++ b/Smart Environment using MQTT/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/Smart Environment using MQTT/.idea/modules.xml b/Smart Environment using MQTT/.idea/modules.xml new file mode 100644 index 0000000..b7e2173 --- /dev/null +++ b/Smart Environment using MQTT/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Smart Environment using MQTT/.idea/pythonProject2.iml b/Smart Environment using MQTT/.idea/pythonProject2.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/Smart Environment using MQTT/.idea/pythonProject2.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Smart Environment using MQTT/docs/METHODOLOGY.md b/Smart Environment using MQTT/docs/METHODOLOGY.md new file mode 100644 index 0000000..8f11356 --- /dev/null +++ b/Smart Environment using MQTT/docs/METHODOLOGY.md @@ -0,0 +1,13 @@ +# Development Methodology + +## Agile Methodology +We used Agile methodology to manage our project. The development process was divided into sprints, with each sprint lasting two weeks. During each sprint, we held daily stand-up meetings to discuss progress and any roadblocks. + +## Tools Used +- **Version Control**: Git +- **Project Management**: Trello +- **Communication**: Slack +## Sprint Planning +- Defined user stories and tasks +- Prioritized the backlog +- Assigned tasks to team members \ No newline at end of file diff --git a/Smart Environment using MQTT/docs/PROGRAMMING APPROACHES.md b/Smart Environment using MQTT/docs/PROGRAMMING APPROACHES.md new file mode 100644 index 0000000..3f9d234 --- /dev/null +++ b/Smart Environment using MQTT/docs/PROGRAMMING APPROACHES.md @@ -0,0 +1,16 @@ +# Programming Approaches and Different Technologies + +## Technologies Used +- **Flask**: For creating the web server and API endpoints. +- **Plotly**: For creating interactive graphs and visualizations. +- **Paho MQTT**: For MQTT communication between sensors and the server. + +## Why These Technologies? +- **Flask**: Lightweight and easy to set up for creating web applications. +- **Plotly**: Provides powerful graphing capabilities with interactive features. +- **Paho MQTT**: Reliable and efficient protocol for IoT communication. + +## Programming Approaches +- **Modular Design**: Each component of the system (sensor integration, MQTT communication, web interface) is designed as a separate module. +- **Code Quality**: Ensured through code reviews, linters, and adhering to PEP 8 guidelines. +- **Testing**: Unit tests were written to verify the functionality of each module. diff --git a/Smart Environment using MQTT/docs/PROJECT_MANAGEMENT.md b/Smart Environment using MQTT/docs/PROJECT_MANAGEMENT.md new file mode 100644 index 0000000..74738b6 --- /dev/null +++ b/Smart Environment using MQTT/docs/PROJECT_MANAGEMENT.md @@ -0,0 +1,17 @@ +# Project Management + +## Roles and Responsibilities +- **Team Leader**: Het pate +- **Developer**: Kartik Sharma +- **UI/UX Designer**: Sami Yousaf + +## Meeting Schedule +- **Weekly Meetings**: Every Monday at 10 AM + +## Meeting Minutes +### Meeting on 2023-07-01 +- **Attendees**: Het pate, Kartik Sharma, Sami Yousaf +- **Discussion Points**: + - Reviewed project progress + - Discussed next steps + - Assigned tasks for the coming week diff --git a/Smart Environment using MQTT/docs/Prototypes.md b/Smart Environment using MQTT/docs/Prototypes.md new file mode 100644 index 0000000..02e9501 --- /dev/null +++ b/Smart Environment using MQTT/docs/Prototypes.md @@ -0,0 +1,15 @@ +# Project Prototype Ideas and Prototypes + +## Ideation Process +- Brainstormed various ideas for environmental monitoring systems. +- Discussed the feasibility, cost, and impact of each idea. +- Selected the idea of a smart environmental monitoring system using MQTT. + +## Prototypes +1. **Initial Prototype**: Basic system with data collection and MQTT communication. +2. **Second Prototype**: Added web interface with real-time graphs. +3. **Final Prototype**: Improved UI, optimized data handling, and added additional features. + +## Iterations +- **Feedback**: Collected feedback from peers and advisors. +- **Improvements**: Made improvements based on feedback, such as enhancing the UI and optimizing data processing. diff --git a/Smart Environment using MQTT/docs/README.md b/Smart Environment using MQTT/docs/README.md new file mode 100644 index 0000000..28229d8 --- /dev/null +++ b/Smart Environment using MQTT/docs/README.md @@ -0,0 +1,42 @@ +# Environmental Monitoring System + +## Overview +This project implements a smart environmental monitoring and control system using MQTT, Flask, and Plotly. It displays real-time data from various sensors on a web interface. + +## Features +- Real-time data monitoring +- Interactive web-based UI with graphs +- MQTT integration for data communication + +## Setup Instructions +1. Install dependencies: + ```bash + pip install -r requirements.txt + ``` + +2. Run the Flask application: + ```bash + python app.py + ``` +3. Access the web interface at `http://127.0.0.1:5000`. + + +## Usage +- The system continuously monitors environmental data and updates the web interface in real-time. +- Graphs for temperature, humidity, and CO2 levels are displayed on the web interface. + +## System Explanation +- **Sensor Integration**: Collects data from various sensors (temperature, humidity, CO2). +- **MQTT Communication**: Sends sensor data to the server using the MQTT protocol. +- **Web Interface**: Displays real-time data using Flask and Plotly. + +## Acknowledgements +We would like to thank ... + +## Methodology +We followed the Agile methodology ... + +## Contributors +- Het patel: Sensor integration and data collection +- Kartik Sharma: MQTT communication and device control +- Sami Yousaf : User interface development and data analysis diff --git a/Smart Environment using MQTT/docs/requirements.txt b/Smart Environment using MQTT/docs/requirements.txt new file mode 100644 index 0000000..b0609a9 --- /dev/null +++ b/Smart Environment using MQTT/docs/requirements.txt @@ -0,0 +1,21 @@ +blinker == 1.8.2 +click == 8.1.7 +colorama == 0.4.6 +contourpy == 1.2.1 +cycler == 0.12.1 +Flask == 3.0.3 +fonttools == 4.53.1 +itsdangerous == 2.2.0 +Jinja2 == 3.1.4 +kiwisolver == 1.4.5 +MarkupSafe == 2.1.5 +matplotlib == 3.9.1 +numpy == 2.0.1 +packaging == 24.1 +paho-mqtt == 2.1.0 +pillow == 10.4.0 +pip == 23.2.1 +pyparsing == 3.1.2 +python-dateutil == 2.9.0.post0 +six == 1.16.0 +Werkzeug == 3.0.3 diff --git a/Smart Environment using MQTT/mqtt_communication.py b/Smart Environment using MQTT/mqtt_communication.py new file mode 100644 index 0000000..a81583b --- /dev/null +++ b/Smart Environment using MQTT/mqtt_communication.py @@ -0,0 +1,53 @@ +# mqtt_communication.py +import random + +import paho.mqtt.client as mqtt +import json +import time + +# MQTT settings +BROKER = "broker.hivemq.com" +PORT = 1883 +TOPIC = "home/environment" + +# Callback when a message is received +def on_message(client, userdata, message): + data = json.loads(message.payload.decode()) + print(f"Received message: {data}") + + # Example: Control devices based on received data + if data["temperature"] > 25: + print("Temperature is high, turning on the fan...") + + elif data["temperature"] < 18: # Adjust the threshold value as needed + print("Temperature is low, turning on the heater...") + else: + print("Temperature is normal, turning off the fan and heater...") + + + +def on_connect(client, userdata, flags, rc): + print("Connected to MQTT broker") + client.subscribe(TOPIC) + +def publish_sensor_data(client, sensor_data): + client.publish(TOPIC, json.dumps(sensor_data)) + print(f"Published sensor data: {sensor_data}") + +if __name__ == "__main__": + client = mqtt.Client() + client.on_connect = on_connect + client.on_message = on_message + + client.connect(BROKER, PORT, 60) + client.loop_start() + + while True: + # Simulating data collection and publishing (this should come from Member 1) + sensor_data = { + "temperature": random.uniform(20.0, 30.0), + "humidity": random.uniform(30.0, 70.0), + "co2": random.uniform(400, 1000) + } + publish_sensor_data(client, sensor_data) + time.sleep(5) diff --git a/Smart Environment using MQTT/sensor_integration.py b/Smart Environment using MQTT/sensor_integration.py new file mode 100644 index 0000000..c5a02a2 --- /dev/null +++ b/Smart Environment using MQTT/sensor_integration.py @@ -0,0 +1,40 @@ +# sensor_integration.py +import random +import time + + +# Simulating sensor data collection +def read_temperature(): + # Code to read temperature from sensor + return random.uniform(20.0, 30.0) + + +def read_humidity(): + # Code to read humidity from sensor + return random.uniform(30.0, 70.0) + + +def read_co2(): + # Code to read CO2 levels from sensor + return random.uniform(400, 1000) + + +def collect_sensor_data(): + while True: + temperature = read_temperature() + humidity = read_humidity() + co2 = read_co2() + + sensor_data = { + "temperature": temperature, + "humidity": humidity, + "co2": co2 + } + + print(f"Collected sensor data: {sensor_data}") + # Simulate a delay between readings + time.sleep(5) + + +if __name__ == "__main__": + collect_sensor_data() diff --git a/Smart Environment using MQTT/templates/index.html b/Smart Environment using MQTT/templates/index.html new file mode 100644 index 0000000..dae523e --- /dev/null +++ b/Smart Environment using MQTT/templates/index.html @@ -0,0 +1,74 @@ + + + + + + + Environmental Monitoring System + + + +

Environmental Monitoring System

+ +
+
+

Temperature (°C)

+
+ +
+
+

Humidity (%)

+
+ +
+
+

CO2 (ppm)

+
+ + + + diff --git a/Smart Environment using MQTT/ui_data_analysis.py b/Smart Environment using MQTT/ui_data_analysis.py new file mode 100644 index 0000000..6be29b8 --- /dev/null +++ b/Smart Environment using MQTT/ui_data_analysis.py @@ -0,0 +1,68 @@ +# app.py +from flask import Flask, render_template, jsonify +import threading +import json +import paho.mqtt.client as mqtt +import time + +app = Flask(__name__) + +# MQTT settings +BROKER = "broker.hivemq.com" +PORT = 1883 +TOPIC = "home/environment" + +# Data storage +data = { + "time": [], + "temperature": [], + "humidity": [], + "co2": [] +} + +# MQTT Callback function +def on_message(client, userdata, message): + global data + msg = json.loads(message.payload.decode()) + current_time = time.strftime('%H:%M:%S', time.localtime()) + + data["time"].append(current_time) + data["temperature"].append(msg.get("temperature")) + data["humidity"].append(msg.get("humidity")) + data["co2"].append(msg.get("co2")) + + # Keep only the last 20 data points for clarity + if len(data["time"]) > 20: + data["time"] = data["time"][-20:] + data["temperature"] = data["temperature"][-20:] + data["humidity"] = data["humidity"][-20:] + data["co2"] = data["co2"][-20:] + +def on_connect(client, userdata, flags, rc): + print("Connected to MQTT broker") + client.subscribe(TOPIC) + +# Flask routes +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/data') +def get_data(): + return jsonify(data) + +def mqtt_thread(): + client = mqtt.Client() + client.on_connect = on_connect + client.on_message = on_message + + client.connect(BROKER, PORT, 60) + client.loop_forever() + +if __name__ == "__main__": + # Start MQTT thread + mqtt_thread = threading.Thread(target=mqtt_thread) + mqtt_thread.start() + + # Start Flask app + app.run(debug=True, use_reloader=False)