#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";
const char* password = "";
//const char* ssid = "RINDA 2.4G"; // Wifi name
//const char* password = "0910318833"; // Wifi password
const char* webAppUrl = "https://script.google.com/macros/s/AKfycby966TkrZB75meZBuAQ2SBgO0I_vNt6YsWRMCeip4ybwAHjbk8gyD6tl1ZmA0Ej0STD/exec"; // Replace with your actual web app URL
const char* lineNotifyToken = "uXsXNkWoGpSRTL4cCMteQ8eqSZDp6LQF4qySqq8ZBEn"; // Line Notify token
int flagTempSendNotify = 0;
int flagHumiSendNotify = 0;
int flagADCSendNotify = 0;
int flagVoltSendNotify = 0;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4); // Address 0x27 for 20x4 LCD
void setup() {
Serial.begin(115200);
delay(100);
dht.begin();
lcd.begin();
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");
lcd.setCursor(0, 0);
lcd.print("Connected to WiFi ");
// you must be comment lcd.init() because use within wokwi only
//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.print(" ");
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);
// Check if values exceed threshold and send Line Notify
checkThresholdAndNotify(temperature, humidity, adcValue, voltage);
delay(5000); // 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 ");
delay(5000);
} 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 ");
}
}
void checkThresholdAndNotify(float temperature, float humidity, int adcValue, float voltage) {
if (temperature > 70.0 && flagTempSendNotify == 0) { // Threshold for temperature
String message = "Alert: Temperature exceeded: " + String(temperature) + " C ";
flagTempSendNotify = 1;
sendLineNotify(message);
}
else if (temperature <= 70.0)
{
flagTempSendNotify = 0;
}
if (humidity > 90.0 && flagHumiSendNotify == 0) { // Threshold for humidity
String message = "Alert: Humidity exceeded: " + String(humidity) + " ";
flagHumiSendNotify = 1;
sendLineNotify(message);
}
else if (humidity <= 90.0)
{
flagHumiSendNotify = 0;
}
if (adcValue > 3000 && flagADCSendNotify == 0) { // Threshold for ADC value
String message = "Alert: ADC value exceeded: " + String(adcValue) + " ";
flagADCSendNotify = 1;
sendLineNotify(message);
}
else if (adcValue <= 3000)
{
flagADCSendNotify = 0;
}
if (voltage > 2.5 && flagVoltSendNotify == 0) { // Threshold for voltage
String message = "Alert: Voltage exceeded: " + String(voltage) + " V";
flagVoltSendNotify = 1;
sendLineNotify(message);
}
else if (voltage <= 2.5)
{
flagVoltSendNotify = 0;
}
}
void sendLineNotify(String message) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Bearer " + String(lineNotifyToken));
String payload = "message=" + message;
int httpCode = http.POST(payload);
if (httpCode > 0) {
Serial.print("Line Notify response code: ");
Serial.println(httpCode);
} else {
Serial.print("Line Notify request failed with error code: ");
Serial.println(httpCode);
}
http.end();
}