#include <WiFi.h>
#include "ThingSpeak.h"
// WiFi (Wokwi default)
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak
unsigned long channelID = 3274386; // 🔴 Put your Channel ID
const char* writeAPIKey = "ZAUTKY9R53X6J97H"; // 🔴 Put your Write API Key
WiFiClient client;
// Pins
#define GAS_SENSOR 34
#define BUZZER 25
// Gas threshold (adjust if needed)
int gasThreshold = 1800;
void setup() {
Serial.begin(115200);
pinMode(GAS_SENSOR, INPUT);
pinMode(BUZZER, OUTPUT);
// Connect WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected");
ThingSpeak.begin(client);
}
void loop() {
int gasValue = analogRead(GAS_SENSOR);
Serial.print("Gas Level: ");
Serial.println(gasValue);
// Alert
if (gasValue > gasThreshold) {
digitalWrite(BUZZER, HIGH);
} else {
digitalWrite(BUZZER, LOW);
}
// Send to ThingSpeak (Field 1)
ThingSpeak.writeField(channelID, 1, gasValue, writeAPIKey);
delay(15000); // ThingSpeak update interval
}