0% found this document useful (0 votes)
5 views

Code

The document contains code for a Factory Gas Monitor using an ESP32 microcontroller, which connects to WiFi and utilizes Blynk for remote monitoring. It reads data from DHT11, dust, and CO sensors, checks against defined thresholds, and logs the data to an SD card. Alerts are indicated through LEDs and data is sent to the Blynk app for visualization.

Uploaded by

mariam119129
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Code

The document contains code for a Factory Gas Monitor using an ESP32 microcontroller, which connects to WiFi and utilizes Blynk for remote monitoring. It reads data from DHT11, dust, and CO sensors, checks against defined thresholds, and logs the data to an SD card. Alerts are indicated through LEDs and data is sent to the Blynk app for visualization.

Uploaded by

mariam119129
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

#define BLYNK_TEMPLATE_ID "TMPL2kpbp2ZAH"

#define BLYNK_TEMPLATE_NAME "Factory Gas Monitor"

#define BLYNK_AUTH_TOKEN "9i0tMBRETkqJNSPV9LbmeCaOSGDsB6cs"

#include <WiFi.h>

#include <BlynkSimpleEsp32.h>

#include <DHT.h>

#include <SD.h>

#include <SPI.h>

// Define the Chip Select (CS) pin

#define SD_CS 5

#define SD_SCK 18

#define SD_MOSI 23

#define SD_MISO 19

// WiFi credentials

char ssid[] = "MARV";

char pass[] = "marvyyyy";

// DHT11 Sensor Settings

#define DHTPIN 4 // Pin connected to DHT11 data pin

#define DHTTYPE DHT11 // DHT 11

#define TEMP_THRESHOLD 15 // Temperature threshold in Celsius

#define TEMP_LED_PIN 25 // LED for temperature alert

// Dust Sensor Settings

#define DUST_SENSOR_PIN 34 // Dust sensor connected to GPIO34

#define DUST_THRESHOLD 300 // Dust threshold


#define DUST_LED_PIN 17 // LED for dust alert

// CO Sensor Settings

#define CO_SENSOR_PIN 35 // CO sensor connected to GPIO35

#define CO_THRESHOLD 50 // CO threshold

#define CO_LED_PIN 26 // LED for CO alert

DHT dht(DHTPIN, DHTTYPE);

// Timing Variables

unsigned long lastUpdateTime = 0; // Timing for sensor updates

const unsigned long updateInterval = 20000; // 20-second interval for sensor readings
and LED blinking

bool tempAlert = false; // Flags for alert conditions

bool dustAlert = false;

bool coAlert = false;

void setup() {

Serial.begin(9600);

// WiFi setup

WiFi.begin(ssid, pass);

Serial.print("Connecting to WiFi...");

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}
Serial.println("\nConnected to WiFi!");

Serial.print("IP address: ");

Serial.println(WiFi.localIP());

// Blynk setup

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

// Sensor and LED setup

dht.begin();

pinMode(TEMP_LED_PIN, OUTPUT);

pinMode(DUST_LED_PIN, OUTPUT);

pinMode(CO_LED_PIN, OUTPUT);

// Initialize the SD card

if (!SD.begin(SD_CS)) {

Serial.println("SD card initialization failed!");

return;

Serial.println("SD card initialized.");

// Write a file

// File file = SD.open("/example.txt", FILE_WRITE);

// if (file) {

// file.println("Hello, ESP32 with SD card!");

// file.close();

// Serial.println("Data written to SD card.");

// } else {

// Serial.println("Failed to open file for writing.");


// }

// // Read the file

// file = SD.open("/example.txt");

// if (file) {

// Serial.println("Reading from SD card:");

// while (file.available()) {

// Serial.write(file.read());

// }

// file.close();

// } else {

// Serial.println("Failed to open file for reading.");

// }

// Ensure all LEDs start OFF

digitalWrite(TEMP_LED_PIN, LOW);

digitalWrite(DUST_LED_PIN, LOW);

digitalWrite(CO_LED_PIN, LOW);

void loop() {

Blynk.run(); // Run Blynk

unsigned long currentTime = millis();

// Sensor Readings Update and Threshold Checking

if (currentTime - lastUpdateTime >= updateInterval) {

lastUpdateTime = currentTime;
// Read sensors

float temperature = dht.readTemperature();

int dustLevel = analogRead(DUST_SENSOR_PIN);

int coLevel = analogRead(CO_SENSOR_PIN);

// Print sensor values

Serial.print("Temperature: ");

Serial.print(temperature);

Serial.println(" °C");

Serial.print("Dust Level: ");

Serial.println(dustLevel);

Serial.print("CO Level: ");

Serial.println(coLevel);

File file = SD.open("/example.txt", FILE_APPEND);

// File file = SD.open("/example.txt");

if (file) {

String temp = "Temperature: " + String(temperature) + " °C";

file.println(temp);

// file.print(temperature);

// file.println(" °C");

file.print("Dust Level: ");

file.println(dustLevel);

file.print("CO Level: ");


file.println(coLevel);

file.close();

Serial.println("Data written to SD card.");

} else {

Serial.println("Failed to open file for writing.");

// file = SD.open("/example.txt");

// if (file) {

// Serial.println("Reading from SD card:");

// while (file.available()) {

// Serial.write(file.read());

// }

// file.close();

// } else {

// Serial.println("Failed to open file for reading.");

// }

// Set alert flags based on thresholds

tempAlert = (temperature > TEMP_THRESHOLD);

dustAlert = (dustLevel > DUST_THRESHOLD);

coAlert = (coLevel > CO_THRESHOLD);

// Send data to Blynk

Blynk.virtualWrite(V0, temperature); // Send temperature to V0

Blynk.virtualWrite(V1, dustLevel); // Send dust level to V1

Blynk.virtualWrite(V3, coLevel); // Send CO level to V3


// Update Blynk LEDs

Blynk.virtualWrite(V4, tempAlert ? 255 : 0); // Temperature LED

Blynk.virtualWrite(V5, coAlert ? 255 : 0); // CO LED

// Blink LEDs every 20 seconds if alerts are active

if (tempAlert && currentTime - lastUpdateTime < 1000) { // Blink TEMP LED

digitalWrite(TEMP_LED_PIN, HIGH);

delay(500); // Keep LED ON for 500ms

digitalWrite(TEMP_LED_PIN, LOW);

if (dustAlert && currentTime - lastUpdateTime < 1000) { // Blink DUST LED

digitalWrite(DUST_LED_PIN, HIGH);

delay(500); // Keep LED ON for 500ms

digitalWrite(DUST_LED_PIN, LOW);

if (coAlert && currentTime - lastUpdateTime < 1000) { // Blink CO LED

digitalWrite(CO_LED_PIN, HIGH);

delay(500); // Keep LED ON for 500ms

digitalWrite(CO_LED_PIN, LOW);

You might also like