forked from Viji2511/air_quality_sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdated mq2
More file actions
130 lines (111 loc) · 4.04 KB
/
updated mq2
File metadata and controls
130 lines (111 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define SCREEN_WIDTH 128 // OLED display width
#define SCREEN_HEIGHT 64 // OLED display height
#define OLED_RESET -1 // OLED reset pin (if needed)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define MQ2_PIN 26 // MQ-2 sensor analog pin
#define DUST_SENSOR_PIN 34 // Dust sensor analog output pin
#define LED_PIN 25 // LED control for dust sensor
#define ADC_MAX 4095.0 // ESP32 ADC is 12-bit
#define REF_VOLTAGE 3.3 // ESP32 reference voltage
// WiFi & ThingSpeak Settings
const char* ssid = "Hi";
const char* password = "12345678";
const char* server = "http://api.thingspeak.com/update";
String apiKey = "2RM3FD5H6LCOT8CD";
unsigned long previousMillis = 0;
const long interval = 15000;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C, OLED_RESET)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
display.display();
delay(2000); // Pause for 2 seconds to display the startup logo
// Connect to Wi-Fi
Serial.print("Connecting to WiFi...");
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 10) {
Serial.print(".");
delay(1000);
attempts++;
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi Connection Failed! Check your credentials.");
return;
}
Serial.println("Connected!");
pinMode(LED_PIN, OUTPUT);
}
int readSensor(int pin) {
long sum = 0;
for (int i = 0; i < 10; i++) {
sum += analogRead(pin);
delay(10);
}
return sum / 10;
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Read MQ-2 Gas Sensor
int gasRaw = readSensor(MQ2_PIN);
float gasVoltage = gasRaw * (REF_VOLTAGE / ADC_MAX);
// Read Dust Sensor (GP2Y1010AU0F)
digitalWrite(LED_PIN, LOW); // Turn ON LED for stable reading
delayMicroseconds(280);
int dustRaw = readSensor(DUST_SENSOR_PIN);
delayMicroseconds(40);
digitalWrite(LED_PIN, HIGH); // Turn OFF LED
float dustVoltage = dustRaw * (REF_VOLTAGE / ADC_MAX);
float dustConcentration = (dustVoltage - 0.1) * 500.0; // Approximate conversion
// Print values to Serial Monitor
Serial.print("MQ-2 Sensor: ");
Serial.print(gasRaw);
Serial.print(" | Voltage: ");
Serial.println(gasVoltage, 3);
Serial.print("Dust Sensor: ");
Serial.print(dustRaw);
Serial.print(" | Voltage: ");
Serial.print(dustVoltage, 3);
Serial.print(" | Dust Concentration: ");
Serial.print(dustConcentration);
Serial.println(" µg/m3");
// Display data on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("MQ-2 Gas: ");
display.println(gasVoltage, 3);
display.print("Dust Conc: ");
display.println(dustConcentration);
display.display();
// Send Data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "?api_key=" + apiKey +
"&field1=" + String(gasVoltage) +
"&field2=" + String(dustConcentration);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
Serial.println("Data sent to ThingSpeak");
} else {
Serial.print("Failed to send data. HTTP Response Code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected!");
}
}
}