#include <WiFi.h>
#include <HTTPClient.h>
// Wi-Fi Credentials
const char* ssid = "ENTC2"; // Replace with your Wi-Fi SSID
const char* password = "123456789"; // Replace with your Wi-Fi Password
// ThingSpeak API Key and URL
const char* server = "http://api.thingspeak.com/update";
const char* apiKey = "6XXOWM4JU93IB65Q"; // Replace with your ThingSpeak API Key
// MQ-2 Sensor Pin
const int gasSensorPin = 34; // Connect MQ-2 Analog output to GPIO 34
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
}
void loop() {
int gasValue = analogRead(gasSensorPin); // Read gas sensor value
Serial.print("Gas Level: ");
Serial.println(gasValue);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "?api_key=" + apiKey + "&field1=" + String(gasValue);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("ThingSpeak Response: ");
Serial.println(httpResponseCode);
} else {
Serial.println("Error in HTTP Request");
}
http.end();
}
delay(15000); // Send data every 15 seconds
}