#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 4 // DHT22 sensor pin
#define DHTTYPE DHT22 // DHT11/22 sensor type
#define MQ135_PIN 34 // MQ-135 sensor pin
const char* ssid = "Wokwi-GUEST"; // Wifi name
const char* password = ""; // Wifi password
const char* webAppUrl = "https://script.google.com/macros/s/AKfycbwUbDc5MMKbPqmc6o--EY8-qeTMvqVhzEswVwlG7ngjiRfv_Z065nVzjCqvHBWMaonbzA/exec"; // Replace with your actual web app URL
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4); // Address 0x27 for 20x4 LCD
void setup() {
Serial.begin(115200);
delay(100);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
lcd.setCursor(0, 0);
lcd.print("Connecting to WiFi...");
}
Serial.println("Connected to WiFi..ok");
lcd.setCursor(0, 0);
lcd.print("Connected to WiFi...ok ");
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temperature & Humidity");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
int adcValue = analogRead(MQ135_PIN);
float voltage = adcValue * (3.3 / 4095.0);
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor");
lcd.setCursor(0, 2);
lcd.print("Sensor Error ");
delay(2000);
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.print(" %, Gas ADC: ");
Serial.print(adcValue);
Serial.print(", Gas Volt: ");
Serial.print(voltage);
Serial.println(" V");
// Display values on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C ");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" % ");
lcd.setCursor(0, 2);
lcd.print("Gas ADC: ");
lcd.print(adcValue);
lcd.setCursor(0, 3);
lcd.print("Gas Volt: ");
lcd.print(voltage, 2);
lcd.print(" V");
// Send data to Google Apps Script
sendDataToScript(temperature, humidity, adcValue, voltage);
delay(2000); // Delay for 2 seconds
}
void sendDataToScript(float temperature, float humidity, int adcValue, float voltage) {
HTTPClient http;
String serverPath = String(webAppUrl) + "?temperature=" + String(temperature) + "&humidity=" + String(humidity) + "&adcValue=" + String(adcValue) + "&voltage=" + String(voltage);
Serial.print("Connecting to server: ");
Serial.println(serverPath);
lcd.setCursor(0, 1);
lcd.print("Sending data... ");
if (http.begin(serverPath)) {
int httpCode = http.GET();
if (httpCode > 0) {
Serial.print("Server response code: ");
Serial.println(httpCode);
lcd.setCursor(0, 1);
lcd.print("Data sent ");
} else {
Serial.print("HTTP GET request failed with error code: ");
Serial.println(httpCode);
lcd.setCursor(0, 1);
lcd.print("Send failed ");
}
http.end();
} else {
Serial.println("Unable to connect to the server");
lcd.setCursor(0, 1);
lcd.print("Server connect err ");
}
}