projects

Arduino Code

#include <DHT.h>

#define DHTPIN 2 // Pin where the KY-015 sensor is connected
#define DHTTYPE DHT11 // DHT11 sensor type

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
dht.begin();
}

void loop() {
float humidity = dht.readHumidity(); // Read humidity from KY-015
float temperature = dht.readTemperature(); // Read temperature from KY-015

// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println(“Failed to read from sensor!”);
return;
}

// Send the temperature and humidity data over serial
Serial.print(“Temperature: “);
Serial.print(temperature);
Serial.print(” C, Humidity: “);
Serial.print(humidity);
Serial.println(” %”);

delay(2000); // Wait for 2 seconds before the next reading
}

Python Code

import serial
import tkinter as tk
from tkinter import ttk

# Establish serial connection (make sure the port matches your setup)
ser = serial.Serial(‘/dev/ttyACM0’, 9600) # Replace with ‘COM3’ on Windows

def update_data():
line=ser.readline().decode(‘utf-8’).strip() # Read the data from serial port
print(f”Raw data: {line}”) # Debugging line to see raw data

if”Temperature”inlineand”Humidity”inline:
try:
# Extract numerical values from the line
temperature=line.split(“Temperature: “)[1].split(” C”)[0].strip()
humidity=line.split(“Humidity: “)[1].split(” %”)[0].strip()

# Update the labels with the extracted values
temperature_label.config(text=f”🌡️ Temperature: {temperature} °C”)
humidity_label.config(text=f”💧 Humidity: {humidity} %”)
except (IndexError, ValueError) ase:
print(f”Error processing data: {e}, line: {line}”)
else:
print(f”Incomplete data received: {line}”)

root.after(2000, update_data) # Schedule the update_data function to be called after 2 seconds

# Initialize the GUI
root = tk.Tk()
root.title(“Weather Monitoring Station”)

# Create and place the labels
temperature_label = ttk.Label(root, text=”🌡️ Temperature: — °C”, font=(“Arial”, 14))
temperature_label.pack(pady=10)

humidity_label = ttk.Label(root, text=”💧 Humidity: — %”, font=(“Arial”, 14))
humidity_label.pack(pady=10)

# Start updating the data
root.after(2000, update_data) # Start the update process by calling the function for the first time

# Run the GUI loop
root.mainloop()

Arduino Code

// Example Arduino Code
#include <Arduino.h>

const int heartbeatPin = A0; // Pin connected to heartbeat sensor
const int tempSensorPin = A1; // Pin connected to temperature sensor
int heartbeatValue = 0;
int tempValue = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
heartbeatValue = analogRead(heartbeatPin);
tempValue = analogRead(tempSensorPin);

// Send data over serial
Serial.print(“Heartbeat:”);
Serial.print(heartbeatValue);
Serial.print(“, Temp:”);
Serial.println(tempValue);

delay(1000);
}

Python Code

from flask import Flask, render_template, jsonify
import serial

app = Flask(__name__)

ser = serial.Serial(‘/dev/ttyACM0’, 9600) # Replace with your actual port


@app.route(‘/’)
def index():
returnrender_template(‘index.html’)

@app.route(‘/data’)
def data():
heartbeat_value=”Not available”
temp_value=”Not available”
 
ifser.in_waiting>0:
data=ser.readline().decode(‘utf-8’).strip()
try:
heartbeat, temp=data.split(‘, ‘)
heartbeat_value=heartbeat.split(‘:’)[1]
temp_value=temp.split(‘:’)[1]
exceptExceptionase:
print(f”Error parsing data: {e}”)
 
returnjsonify({‘heartbeat’: heartbeat_value, ‘temp’: temp_value})

if __name__ == ‘__main__’:
app.run(host=’0.0.0.0′, port=5000, debug=True)

HTML,CSS,JAVAscrip

<!DOCTYPE html>
<html lang=”en”>
<head>
<metacharset=”UTF-8″>
<metaname=”viewport”content=”width=device-width, initial-scale=1.0″>
<title>Health Monitoring System</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
.data {
font-size: 24px;
margin: 20px0;
}
</style>
<script>
functionfetchData() {
fetch(‘/data’)
.then(response => response.json())
.then(data => {
document.getElementById(‘heartbeat’).innerText = ‘Heartbeat: ‘ + data.heartbeat;
document.getElementById(‘temperature’).innerText = ‘Temperature: ‘ + data.temp;
});
}
setInterval(fetchData, 1000); // Update every second
</script>
</head>
<body>
<h1>Health Monitoring System</h1>
<divclass=”data”id=”heartbeat”>Heartbeat: </div>
<divclass=”data”id=”temperature”>Temperature: </div>
</body>
</html>
×

Hello!

Click one of our contacts below to chat on WhatsApp

×