-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266.ino
179 lines (156 loc) · 5.89 KB
/
ESP8266.ino
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include <PubSubClient.h>
#include <ArduinoJson.h>
// Update these with your WiFi settings
#define WIFI_SSID "POCO-X3" // Should be changed based on the connected network & The bandwidth should be 2.4 GHz
#define WIFI_PASSWORD "ajsea8055" // Same as above
// MQTT Broker settings
const char* mqtt_server = "192.168.79.254"; // IP Address of The Connected Network
const int mqtt_port = 1883;
const char* mqtt_topic_bme680 = "sensors/BME680";
const char* mqtt_topic_mq135 = "sensors/MQ135";
const char* mqtt_topic_flame = "sensors/Flame"; // New MQTT topic for flame detection
// Define the analog pin connected to the MQ135 sensor
const int MQ135_PIN = A0;
// Define the digital pin connected to the flame detection sensor
const int FLAME_SENSOR_PIN = D5; // Connect D0 pin of the sensor to D5 of ESP8266
// Define the load resistance of the MQ135 sensor (in ohms)
const float RL_MQ135 = 10000.0; // Example resistance value, adjust according to your sensor
Adafruit_BME680 bme; // I2C
#if defined(ESP32)
#include <WiFi.h>
WiFiClient espClient;
#define DEVICE "ESP32"
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
WiFiClient espClient;
#define DEVICE "ESP8266"
#endif
PubSubClient mqttClient(espClient);
void setup() {
Serial.begin(115200);
delay(1000);
// Connect to WiFi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
// Connect to MQTT broker
mqttClient.setServer(mqtt_server, mqtt_port);
while (!mqttClient.connected()) {
Serial.println("Connecting to MQTT broker...");
if (mqttClient.connect(DEVICE)) {
Serial.println("Connected to MQTT broker");
} else {
Serial.print("Failed to connect to MQTT broker, rc=");
Serial.print(mqttClient.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
}
// Initialize BME680 sensor
if (!bme.begin()) {
Serial.println(F("Could not find a valid BME680 sensor, check wiring!"));
while (1);
}
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
// Set flame sensor pin as input
pinMode(FLAME_SENSOR_PIN, INPUT);
}
void loop() {
// Read sensor data
#define SEALEVELPRESSURE_HPA (1013.25)
float temperature = bme.readTemperature();
float pressure = bme.readPressure() / 100.0;
float humidity = bme.readHumidity();
float gasResistance = bme.readGas() / 1000.0;
float altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
// Read the analog value from the MQ135 sensor
int sensorValue = analogRead(MQ135_PIN);
// Convert the analog value to voltage
float voltage = sensorValue * (3.3 / 1023.0);
// Calculate the resistance of the sensor using voltage divider formula
float Rs = (3.3 * RL_MQ135) / voltage - RL_MQ135;
// Use a pre-calibrated equation to estimate the air quality index (AQI)
float ppm = pow(10, (1.2693 * log10(Rs) - 2.4042));
// Read flame sensor value
int flameValue = digitalRead(FLAME_SENSOR_PIN);
// Display sensor data in Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Pressure: ");
Serial.print(pressure);
Serial.println(" hPa");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Gas Resistance: ");
Serial.print(gasResistance);
Serial.println(" kOhms");
Serial.print("Altitude: ");
Serial.print(altitude);
Serial.println(" meters");
Serial.print("Air Quality (ppm): ");
Serial.println(ppm);
Serial.print("Flame Detected: ");
Serial.println(flameValue == LOW ? "Yes" : "No");
Serial.println();
// Publish sensor data to MQTT topics
publishSensorData(temperature, pressure, humidity, gasResistance, altitude, ppm, flameValue);
delay(2000); // Wait before next reading
}
void publishSensorData(float temperature, float pressure, float humidity, float gasResistance, float altitude, float ppm, int flameValue) {
// Create JSON payload with BME680 sensor data
StaticJsonDocument<200> jsonDocBME680;
jsonDocBME680["temperature"] = temperature;
jsonDocBME680["pressure"] = pressure;
jsonDocBME680["humidity"] = humidity;
jsonDocBME680["gasResistance"] = gasResistance;
jsonDocBME680["altitude"] = altitude;
jsonDocBME680["airQuality"] = ppm;
// Serialize JSON to string
char jsonStrBME680[200];
serializeJson(jsonDocBME680, jsonStrBME680);
// Publish BME680 sensor data to MQTT topic
if (mqttClient.publish(mqtt_topic_bme680, jsonStrBME680)) {
Serial.println("Published BME680 sensor data to MQTT topic");
} else {
Serial.println("Failed to publish BME680 sensor data to MQTT topic");
}
// Create JSON payload with MQ135 sensor data
StaticJsonDocument<100> jsonDocMQ135;
jsonDocMQ135["airQuality"] = ppm;
// Serialize JSON to string
char jsonStrMQ135[100];
serializeJson(jsonDocMQ135, jsonStrMQ135);
// Publish MQ135 sensor data to MQTT topic
if (mqttClient.publish(mqtt_topic_mq135, jsonStrMQ135)) {
Serial.println("Published MQ135 sensor data to MQTT topic");
} else {
Serial.println("Failed to publish MQ135 sensor data to MQTT topic");
}
// Create JSON payload with flame sensor data
StaticJsonDocument<100> jsonDocFlame;
jsonDocFlame["flameDetected"] = (flameValue == LOW) ? 1 : 0;
// Serialize JSON to string
char jsonStrFlame[100];
serializeJson(jsonDocFlame, jsonStrFlame);
// Publish flame sensor data to MQTT topic
if (mqttClient.publish(mqtt_topic_flame, jsonStrFlame)) {
Serial.println("Published Flame sensor data to MQTT topic");
} else {
Serial.println("Failed to publish Flame sensor data to MQTT topic");
}
}