#include <WiFi.h>
#include "DHT.h"
#include "Wire.h"
#include <math.h>
#include "ThingSpeak.h"
// Pin Definitions
#define POWER_PIN 32 // ESP32's pin GPIO32 that provides the power to the rain sensor
#define DO_PIN 13 // ESP32's pin GPIO13 connected to DO pin of the rain sensor
#define DHT_SENSOR_PIN 21 // ESP32 pin GPIO21 connected to DHT22 sensor
#define BUZZER_PIN 25 // ESP32 pin GPIO25 connected to Buzzer
#define LIGHT_SENSOR_PIN 32 // ESP32 pin GIOP36 (ADC0)
// WiFi and ThingSpeak Configuration
const char* ssid = "Wokwi-GUEST";
const char* password = "";
unsigned long myChannelNumber = 2766019; // ThingSpeak channel ID
const char* myApiKey = "WKCRSKFYYRZ45DOK"; // ThingSpeak API key
WiFiClient client;
// DHT Sensor Object
DHT dht_sensor(DHT_SENSOR_PIN, DHT22);
float GAMMA = 0.7;
float RL10 = 85;
// Timer for sending data to ThingSpeak
unsigned long lastSendTime = 0; // Track the last time data was sent
const unsigned long sendInterval = 15000; // Minimum interval: 15 seconds
void connectToWiFi() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Reconnecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nReconnected to WiFi");
}
}
void setup() {
// Initialize Serial Communication
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Menghubungkan ke Wifi...");
}
Serial.println("Terhubung ke Wifi >.<");
// Initialize ThingSpeak
ThingSpeak.begin(client);
// Initialize pins
pinMode(POWER_PIN, OUTPUT);
pinMode(DO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Initialize the DHT sensor
dht_sensor.begin();
}
void loop() {
// Reconnect to WiFi if disconnected
connectToWiFi();
// Get current time
unsigned long currentTime = millis();
// Only send data if enough time has passed since the last transmission
if (currentTime - lastSendTime >= sendInterval) {
// Update lastSendTime
lastSendTime = currentTime;
// Read humidity and temperature from DHT sensor
float humi = dht_sensor.readHumidity();
float tempC = dht_sensor.readTemperature();
// Add fluctuation to temperature and humidity for more dynamic graphs
float tempFluctuation = random(-50, 50) / 100.0; // Random fluctuation: -0.5°C to +0.5°C
float humiFluctuation = random(-30, 30) / 100.0; // Random fluctuation: -0.3% to +0.3%
tempC += tempFluctuation;
humi += humiFluctuation;
// Rain Sensor Part
digitalWrite(POWER_PIN, HIGH); // Turn the rain sensor's power ON
delay(10); // Wait 10 milliseconds
int rain_state = digitalRead(DO_PIN); // Read the digital output state
digitalWrite(POWER_PIN, LOW); // Turn the rain sensor's power OFF
// Determine rain status
String weatherStatus = (rain_state == LOW) ? "Cerah" : "Hujan";
// Buzzer control: turn ON if rain is detected, otherwise OFF
if (rain_state == LOW) {
digitalWrite(BUZZER_PIN, LOW); // Buzzer OFF
} else {
digitalWrite(BUZZER_PIN, HIGH); // Buzzer ON
}
// Light Sensor Part
int analog_value = analogRead(LIGHT_SENSOR_PIN);
float voltage = analog_value / 4095.0 * 3.3;
float resistance = 5000 * voltage / (1 - voltage / 3.3);
float lux = pow(RL10 * 1000 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// Add fluctuation to lux
float luxFluctuation = random(-200, 200); // Random fluctuation: -200 to +200
lux += luxFluctuation;
// Print data to Serial Monitor
Serial.println("----");
Serial.println("Temperature (C): " + String(tempC) + "°C");
Serial.println("Humidity: " + String(humi) + "%");
Serial.println("Weather: " + weatherStatus);
Serial.println("Lux: " + String(lux));
// Send data to ThingSpeak
ThingSpeak.setField(1, tempC);
ThingSpeak.setField(2, humi);
ThingSpeak.setField(3, lux);
ThingSpeak.setField(4, rain_state == LOW ? 0 : 1); // 0 = Clear, 1 = Rainy
int response = ThingSpeak.writeFields(myChannelNumber, myApiKey);
if (response == 200) {
Serial.println("Data sudah terkirim ke Thingspeak");
} else {
Serial.println("Data gagal terkirim. Error: " + String(response));
}
}
// Add a small delay to avoid hogging the loop
delay(100);
}