0% found this document useful (0 votes)
25 views10 pages

Title - Solution To All Types of IoT Based Monitoring Systems

Uploaded by

khanalif879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views10 pages

Title - Solution To All Types of IoT Based Monitoring Systems

Uploaded by

khanalif879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Title: Solution to all types of IoT based Monitoring Systems

Programming Embedded System Lab

Contents:
● IoT based Cloud Server (Thingspeak.com)
● Inserting and Reading Data from Thingspeak
● Circuit Diagram of Real Time Monitoring System
● Programming
● Web Interface
● Conclusion
IoT based Cloud Server (Thingspeak.com)

Inserting and Reading Data from Thingspeak


Circuit Diagram of Real Time Monitoring System
Microcontroller Programming

#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
#include <DHT.h>

// WiFi credentials
const char* ssid = "salehin";
const char* password = "1122334455";

// ThingSpeak credentials
unsigned long myChannelNumber = 2640391;
const char* myWriteAPIKey = "XYNT30MAQLSC5ZXM";

// Define pins
#define DHTPIN1 D2
#define DHTPIN2 D3
#define PIRPIN D4
#define LEDPIN D5
#define BUZZERPIN D6
#define MQPIN A0 // MQ sensor connected to analog pin A0
#define DHTTYPE DHT22

// Initialize DHT sensors


DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);

// Thresholds
float temperatureThreshold = 35.0;
int smokeThreshold = 200; // Adjust this value based on your MQ sensor's calibration

WiFiClient client;

void setup() {
Serial.begin(115200);

// Initialize DHT sensors


dht1.begin();
dht2.begin();

// Set pin modes


pinMode(LEDPIN, OUTPUT);
pinMode(BUZZERPIN, OUTPUT);
pinMode(PIRPIN, INPUT);

// Initialize LED and Buzzer


digitalWrite(LEDPIN, LOW);
digitalWrite(BUZZERPIN, LOW);

// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
// Initialize ThingSpeak
ThingSpeak.begin(client);
}

void loop() {
// Read temperature and humidity
float temp1 = dht1.readTemperature();
float temp2 = dht2.readTemperature();
float humidity1 = dht1.readHumidity();
float humidity2 = dht2.readHumidity();
int smokeValue = analogRead(MQPIN);
int motion = 0;
digitalWrite(LEDPIN, LOW);

// Check if any reading failed and exit early


if (isnan(temp1) || isnan(humidity1) || isnan(temp2) || isnan(humidity2)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print values
Serial.print("Temperature 1: ");
Serial.print(temp1);
Serial.print(" °C, Humidity 1: ");
Serial.print(humidity1);
Serial.println(" %");
Serial.print("Temperature 2: ");
Serial.print(temp2);
Serial.print(" °C, Humidity 2: ");
Serial.print(humidity2);
Serial.println(" %");

Serial.print("Smoke Value: ");


Serial.println(smokeValue);

// Check temperature threshold


if (temp1 > temperatureThreshold || temp2 > temperatureThreshold) {
digitalWrite(LEDPIN, HIGH); // Turn on LED
} else {
digitalWrite(LEDPIN, LOW); // Turn off LED
}

// Check PIR sensor for motion


if (digitalRead(PIRPIN) == HIGH) {
motion = 1;
digitalWrite(BUZZERPIN, HIGH); // Turn on buzzer
Serial.println("Motion detected!");
delay(500);
digitalWrite(BUZZERPIN, LOW);
} else {
motion = 0;
digitalWrite(BUZZERPIN, LOW);
Serial.println("Motion Not detected!"); // Turn off buzzer
}

// Check smoke level


if (smokeValue > smokeThreshold) {
// Blink LED to indicate smoke detected
digitalWrite(LEDPIN, HIGH);
delay(200);
digitalWrite(BUZZERPIN, HIGH);
delay(200);
digitalWrite(BUZZERPIN, LOW);
delay(200);
digitalWrite(BUZZERPIN, HIGH);
delay(200);
digitalWrite(BUZZERPIN, LOW);
}
//////////////////////////////////////////////////////////
// Generate sensor data to push to cloud
float sensor1 = ((temp1+temp2)/2); // Mock sensor 1 value
float sensor2 = ((humidity1+humidity2)/2); // Mock sensor 2 value
int sensor3 = smokeValue; // Mock sensor 3 value
int sensor4 = motion; // Mock sensor 4 value

// Print the sensor values to the Serial Monitor


Serial.print("Temperature: ");
Serial.println(sensor1);
Serial.print("Humidity: ");
Serial.println(sensor2);
Serial.print("Smoke: ");
Serial.println(sensor3);
Serial.print("Motion: ");
Serial.println(sensor4);

// Send data to ThingSpeak


ThingSpeak.setField(1, sensor1); // Field 1
ThingSpeak.setField(2, sensor2); // Field 2
ThingSpeak.setField(3, sensor3); // Field 3
ThingSpeak.setField(4, sensor4); // Field 4
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

// Check response from ThingSpeak


if (responseCode == 200) {
Serial.println("Data sent to ThingSpeak successfully");
} else {
Serial.print("Problem sending data to ThingSpeak. HTTP error code: ");
Serial.println(responseCode);
}
/////////////////////////////////////////////////////////

// Wait for 15 seconds before sending the next set of data


delay(15000);
}

Web Interface

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Real Time Data Center Monitoring</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 10px;
text-align: center;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>

<h1>Last 5 Sensor Readings</h1>

<table>
<thead>
<tr>
<th>Entry ID</th>
<th>Temperature</th>
<th>Humidity</th>
<th>Smoke (Td = 200)</th>
<th>Motion</th>
</tr>
</thead>
<tbody id="data-table">
<!-- Data will be inserted here -->
</tbody>
</table>

<script>
const channelID = "2640391";
const apiKey = "XYNT30MAQLSC5ZXM";
const refreshInterval = 15000; // 15 seconds

function fetchData() {
const url =
`https://api.thingspeak.com/channels/${channelID}/feeds.json?api_key=${apiKey}&results
=5`;

fetch(url)
.then(response => response.json())
.then(data => {
const feeds = data.feeds;
const table = document.getElementById("data-table");

// Clear the existing data


table.innerHTML = "";

feeds.forEach(feed => {
const row = document.createElement("tr");

const entryID = document.createElement("td");


entryID.textContent = feed.entry_id;
row.appendChild(entryID);

const sensor1 = document.createElement("td");


sensor1.textContent = feed.field1;
row.appendChild(sensor1);

const sensor2 = document.createElement("td");


sensor2.textContent = feed.field2;
row.appendChild(sensor2);

const sensor3 = document.createElement("td");


sensor3.textContent = feed.field3;
row.appendChild(sensor3);

const sensor4 = document.createElement("td");


sensor4.textContent = feed.field4;
row.appendChild(sensor4);

table.appendChild(row);
});
})
.catch(error => console.error('Error fetching data:', error));
}

// Fetch data immediately when the page loads


fetchData();

// Set up interval to refresh data every 15 seconds


setInterval(fetchData, refreshInterval);
</script>

</body>
</html>

Conclusion

(-_-) (-_-) (-_-) (-_-) (-_-) (-_-) (-_-)

You might also like