#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* scriptUrl = "https://script.google.com/macros/s/AKfycby7tcLfoGlLvpwcCdRFu8Z6bvTiyf-PSpNivow_eDQS6kZXsLb93I80cUgAirFhdLNU/exec"; // Ganti dengan URL Apps Script
#define PPM_PIN 34 // Pin analog MQ-135
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void loop() {
// Baca nilai dari sensor MQ-135
int16_t ppmValue = analogRead(PPM_PIN);
int mappedppmValue = (ppmValue / 4.095); // Koreksi pembacaan nilai
Serial.print("PPM: ");
Serial.println(mappedppmValue);
// Kirim data ke Google Sheets
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(scriptUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Format data yang dikirim
String postData = "ppm=" + String(mappedppmValue);
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
Serial.println("Data sent to Google Sheets");
} else {
Serial.print("Error sending data: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
delay(5000); // Kirim data setiap 5 detik
}