#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6qr4vJ4KR"
#define BLYNK_TEMPLATE_NAME "mushroom farm"
#define BLYNK_AUTH_TOKEN "7sXFb5jAcbjgRqyTSHYRYR5ZGKYfFFDf"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <ESP32Servo.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
Servo myservo;
const int lightSensorPin = 34;
const int ledPin1 = 25;
const int ledPin2 = 26;
const int threshold1 = 2000;
const int threshold2 = 2500;
#define DHTPIN 2
#define DHTTYPE DHT22
#define RELAY 13
#define SERVO_PIN 12
#define minPulse 1000
#define maxPulse 2000
int buttonPin = 4;
int buttonState = 0;
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
LCD.init();
LCD.backlight();
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(lightSensorPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, LOW);
myservo.attach(SERVO_PIN, minPulse, maxPulse);
}
BLYNK_WRITE(V2)
{
if(param.asInt() == 1)
{
for (int i = 0; i < 5; i++) {
digitalWrite(RELAY, HIGH);
myservo.write(0);
delay(1000);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(90);
delay(1000);
myservo.write(0);
delay(1000);
}
}else{
digitalWrite(RELAY, LOW);
myservo.write(90);
}
}
void loop()
{
int lightValue = analogRead(lightSensorPin);
int rawValue = analogRead(lightSensorPin);
int mappedValue = map(rawValue, 0, 4095, 100, 0);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
for (int i = 0; i < 5; i++) { // วนลูป 5 รอบ
digitalWrite(RELAY, HIGH); // เปิด Relay
myservo.write(0);
delay(1000);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(90);
delay(1000);
myservo.write(0);
delay(1000);
}
} else {
digitalWrite(RELAY, LOW);
myservo.write(90);
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Mapped Light Value: ");
Serial.println(mappedValue);
if (temperature >= 27) {
digitalWrite(RELAY, HIGH);
myservo.write(0);
delay(1000);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
} else {
digitalWrite(RELAY, LOW);
myservo.write(90);
}
if (humidity <= 60) {
digitalWrite(RELAY, HIGH);
myservo.write(0);
delay(1000);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
} else {
digitalWrite(RELAY, LOW);
myservo.write(90);
}
if (lightValue > threshold1) {
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}
if (lightValue > threshold2) {
digitalWrite(ledPin2, HIGH);
} else {
digitalWrite(ledPin2, LOW);
}
// Update the LCD display, delay, and run Blynk
LCD.setCursor(0, 0);
LCD.print("Humidity: ");
LCD.print(humidity);
LCD.print("%");
LCD.setCursor(0, 1);
LCD.print("Temper: ");
LCD.print(temperature);
LCD.print("*C");
delay(4000);
LCD.clear();
delay(500);
LCD.setCursor(6, 0);
LCD.print("Light");
LCD.setCursor(7, 1);
LCD.print(mappedValue);
LCD.print(" %");
delay(1000);
Blynk.run();
Blynk.virtualWrite(V0, temperature);
Blynk.virtualWrite(V1, humidity);
Blynk.virtualWrite(V2, mappedValue);
}