241 lines
7.0 KiB
C++
241 lines
7.0 KiB
C++
#include <Arduino.h>
|
|
|
|
// Include library according to your board type (ESP32 or ESP8266)
|
|
#if defined(ESP32)
|
|
#include <WiFi.h>
|
|
#elif defined(ESP8266)
|
|
#include <ESP8266WiFi.h>
|
|
#endif
|
|
|
|
#include <Firebase_ESP_Client.h>
|
|
#include <DHT.h>
|
|
#include <LiquidCrystal_I2C.h>
|
|
|
|
// Define pins for MQ2 sensor
|
|
#define DO_PIN 14 // ESP32's pin GPIO14 connected to DO pin of the MQ2 sensor
|
|
#define AO_PIN 34 // ESP32's pin GPIO34 connected to AO pin of the MQ2 sensor
|
|
|
|
// Define pin for DHT22 sensor
|
|
#define DHT22_PIN 19 // ESP32 pin GPIO19 connected to DHT22 sensor
|
|
|
|
// Define pins for relay modules
|
|
#define RELAY1_PIN 25 // ESP32 pin GPIO25 connected to IN1 of the relay module
|
|
#define RELAY2_PIN 26 // ESP32 pin GPIO26 connected to IN2 of the relay module
|
|
|
|
// Define threshold for gas detection
|
|
#define GAS_THRESHOLD 500
|
|
|
|
// Firebase Configuration
|
|
#define API_KEY "AIzaSyBpjI0g0YeWkaLW4IfOmcpv3xOshEneF_A"
|
|
#define DATABASE_URL "https://realtimeultrasonik-default-rtdb.firebaseio.com/"
|
|
|
|
// WiFi Credentials
|
|
const char *ssid = "MBKM2023";
|
|
const char *password = "MBKM2023";
|
|
|
|
// Firebase Data Objects
|
|
FirebaseData fbdo;
|
|
FirebaseAuth auth;
|
|
FirebaseConfig config;
|
|
|
|
DHT dht22(DHT22_PIN, DHT22);
|
|
|
|
// set the LCD number of columns and rows
|
|
int lcdColumns = 16;
|
|
int lcdRows = 2;
|
|
|
|
// set LCD address, number of columns and rows
|
|
// if you don't know your display address, run an I2C scanner sketch
|
|
LiquidCrystal_I2C lcd(0x3F, lcdColumns, lcdRows);
|
|
|
|
// Timer variables for switching messages
|
|
unsigned long previousMillis = 0;
|
|
const long interval = 5000; // Interval for switching messages (5 seconds)
|
|
int messageIndex = 0;
|
|
|
|
void setup() {
|
|
// Initialize serial communication
|
|
Serial.begin(9600);
|
|
|
|
// Initialize MQ2 sensor
|
|
pinMode(DO_PIN, INPUT);
|
|
pinMode(AO_PIN, INPUT);
|
|
|
|
// Initialize DHT22 sensor
|
|
dht22.begin();
|
|
|
|
// Initialize relay pins
|
|
pinMode(RELAY1_PIN, OUTPUT);
|
|
pinMode(RELAY2_PIN, OUTPUT);
|
|
|
|
// Initialize LCD
|
|
lcd.init();
|
|
// Turn on LCD backlight
|
|
lcd.backlight();
|
|
|
|
Serial.println("Warming up the MQ2 sensor");
|
|
delay(20000); // Wait for the MQ2 to warm up
|
|
|
|
// Connect to WiFi
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(1000);
|
|
Serial.println("Connecting to WiFi...");
|
|
}
|
|
Serial.println("Connected to WiFi");
|
|
|
|
// Configure Firebase
|
|
config.api_key = API_KEY;
|
|
config.database_url = DATABASE_URL;
|
|
|
|
if (Firebase.signUp(&config, &auth, "", "")) {
|
|
Serial.println("Firebase signup successful");
|
|
} else {
|
|
Serial.printf("Firebase signup failed: %s\n", config.signer.signupError.message.c_str());
|
|
}
|
|
|
|
Firebase.begin(&config, &auth);
|
|
Firebase.reconnectWiFi(true);
|
|
}
|
|
|
|
void loop() {
|
|
// Read MQ2 sensor digital output
|
|
int gasState = digitalRead(DO_PIN);
|
|
// Read MQ2 sensor analog output
|
|
int gasValue = analogRead(AO_PIN);
|
|
|
|
// Read DHT22 sensor values
|
|
float humi = dht22.readHumidity();
|
|
float tempC = dht22.readTemperature();
|
|
|
|
// Check if DHT22 readings are successful
|
|
if (isnan(tempC) || isnan(humi)) {
|
|
Serial.println("Failed to read from DHT22 sensor!");
|
|
} else {
|
|
// Print DHT22 sensor values
|
|
Serial.print("Humidity: ");
|
|
Serial.print(humi);
|
|
Serial.print("% | ");
|
|
Serial.print("Temperature: ");
|
|
Serial.print(tempC);
|
|
Serial.println("°C");
|
|
|
|
// Send temperature and humidity data to Firebase
|
|
if (Firebase.RTDB.setFloat(&fbdo, "kandang/suhu", tempC)) {
|
|
Serial.println("Temperature data sent to Firebase");
|
|
} else {
|
|
Serial.println("Failed to send temperature data to Firebase");
|
|
Serial.println("Error: " + fbdo.errorReason());
|
|
}
|
|
|
|
if (Firebase.RTDB.setFloat(&fbdo, "kandang/kelembaban", humi)) {
|
|
Serial.println("Humidity data sent to Firebase");
|
|
} else {
|
|
Serial.println("Failed to send humidity data to Firebase");
|
|
Serial.println("Error: " + fbdo.errorReason());
|
|
}
|
|
}
|
|
|
|
// Print MQ2 sensor values
|
|
if (gasState == HIGH) {
|
|
Serial.println("The gas is NOT present");
|
|
} else {
|
|
Serial.println("The gas is present");
|
|
}
|
|
|
|
Serial.print("Gas value (analog): ");
|
|
Serial.println(gasValue);
|
|
|
|
// Send gas data to Firebase
|
|
if (Firebase.RTDB.setInt(&fbdo, "kandang/amonia", gasValue)) {
|
|
Serial.println("Gas data sent to Firebase");
|
|
} else {
|
|
Serial.println("Failed to send gas data to Firebase");
|
|
Serial.println("Error: " + fbdo.errorReason());
|
|
}
|
|
|
|
// Control relay modules based on gas value
|
|
if (gasValue > GAS_THRESHOLD) { // Gas is detected
|
|
digitalWrite(RELAY1_PIN, LOW); // Activate relay 1
|
|
Serial.println("Relay 1 activated: Current Flowing");
|
|
if (Firebase.RTDB.setBool(&fbdo, "kandang/sprayer", true)) {
|
|
Serial.println("Sprayer status set to true in Firebase");
|
|
} else {
|
|
Serial.println("Failed to set sprayer status in Firebase");
|
|
Serial.println("Error: " + fbdo.errorReason());
|
|
}
|
|
} else {
|
|
digitalWrite(RELAY1_PIN, HIGH); // Deactivate relay 1
|
|
Serial.println("Relay 1 deactivated: Current not Flowing");
|
|
if (Firebase.RTDB.setBool(&fbdo, "kandang/sprayer", false)) {
|
|
Serial.println("Sprayer status set to false in Firebase");
|
|
} else {
|
|
Serial.println("Failed to set sprayer status in Firebase");
|
|
Serial.println("Error: " + fbdo.errorReason());
|
|
}
|
|
}
|
|
|
|
// Example condition to control relay 2
|
|
if (tempC > 35) { // Temperature is higher than 35°C
|
|
digitalWrite(RELAY2_PIN, LOW); // Activate relay 2
|
|
Serial.println("Relay 2 activated: Current Flowing");
|
|
if (Firebase.RTDB.setBool(&fbdo, "kandang/sprayer2", true)) {
|
|
Serial.println("Sprayer 2 status set to true in Firebase");
|
|
} else {
|
|
Serial.println("Failed to set sprayer 2 status in Firebase");
|
|
Serial.println("Error: " + fbdo.errorReason());
|
|
}
|
|
} else {
|
|
digitalWrite(RELAY2_PIN, HIGH); // Deactivate relay 2
|
|
Serial.println("Relay 2 deactivated: Current not Flowing");
|
|
if (Firebase.RTDB.setBool(&fbdo, "kandang/sprayer2", false)) {
|
|
Serial.println("Sprayer 2 status set to false in Firebase");
|
|
Serial.println("Error: " + fbdo.errorReason());
|
|
}
|
|
}
|
|
|
|
// Timer for switching messages on LCD
|
|
unsigned long currentMillis = millis();
|
|
if (currentMillis - previousMillis >= interval) {
|
|
previousMillis = currentMillis;
|
|
messageIndex = (messageIndex + 1) % 3;
|
|
|
|
lcd.clear();
|
|
switch (messageIndex) {
|
|
case 0:
|
|
lcd.setCursor(0, 0);
|
|
if (tempC < 28) {
|
|
lcd.print("Kondisi Normal");
|
|
} else {
|
|
lcd.print("Kondisi Panas");
|
|
}
|
|
break;
|
|
case 1:
|
|
// Display message 2
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("TEMP: ");
|
|
lcd.print(tempC);
|
|
lcd.print("C");
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("HUM: ");
|
|
lcd.print(humi);
|
|
lcd.print("%");
|
|
break;
|
|
case 2:
|
|
lcd.setCursor(0, 0);
|
|
if (gasValue <= GAS_THRESHOLD) {
|
|
lcd.print("Gas Normal");
|
|
} else {
|
|
lcd.print("Gas Tinggi");
|
|
}
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("Amonia: ");
|
|
lcd.print(gasValue);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Wait for 2 seconds before next reading
|
|
delay(2000);
|
|
}
|