#define BLYNK_TEMPLATE_ID "TMPL4_vpPL80E"
#define BLYNK_TEMPLATE_NAME "Smart Greenhouse"
#define BLYNK_AUTH_TOKEN "CEtNtyZzjFe62G7hCzohFU-Sy5bWfj0Y"
#define BLYNK_PRINT Serial
// Internet connection
#include <WiFi.h>
#include <WiFiClient.h>
// Blynk
#include <BlynkSimpleEsp32.h>
// Libraries
#include <DHT.h>
#include <ESP32Servo.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; //name hotspot
char pass[] = ""; //password hotspot
#define BRIGHTNESSPIN 2
#define DOORPIN 32
Servo door_Servo;
#define WATERPUMBPIN 12
#define WINDOWSPIN 13
#define DHTPIN 25
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define TRIG 26
#define ECHO 27
#define LIGHTSPIN 33
BlynkTimer timer;
const float GAMMA = 0.7;
const float RL10 = 50;
void setup() {
Serial.begin(115200); // Starts the serial communication at 115200 baud rate
Blynk.begin(auth, ssid, pass); // Connects to Blynk server using the given authentication token, SSID, and password
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(LIGHTSPIN, OUTPUT);
pinMode(WATERPUMBPIN, OUTPUT);
pinMode(WINDOWSPIN, OUTPUT);
// pinMode(BRIGHTNESSPIN, INPUT);
door_Servo.attach(DOORPIN, 500, 2400);
door_Servo.write(0);
dht.begin(); // Initializes the DHT sensor
timer.setInterval(2500L, getTempHumidity); // Sets the timer interval for reading temperature and humidity sensor
timer.setInterval(2500L, getWaterLevel); // Sets the timer interval for reading the water level sensor
timer.setInterval(2500L, getBrightness); // Sets the timer interval for reading the brightness sensor
}
void loop() {
Blynk.run(); // Runs the Blynk loop to update the virtual pins
timer.run(); // Runs the timer loop to trigger the scheduled tasks
}
void getTempHumidity() {
float h = dht.readHumidity(); // Reads the humidity from the DHT sensor
float t = dht.readTemperature(); // Reads the temperature from the DHT sensor
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
openWindows(h, t);
Blynk.virtualWrite(V0, t); // Writes the temperature value to virtual pin V0
Blynk.virtualWrite(V1, h); // Writes the humidity value to virtual pin V1
}
// Using the HC-SR04 sensor to measure the water level in a tank
void getWaterLevel(){
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
int duration = pulseIn(ECHO, HIGH); // Measures the duration of the echo pulse
float waterLevel = duration * 0.034 / 2; // Calculates the distance in cm using the speed of sound
if (isnan(waterLevel)){
Serial.println("Failed to read from DHHC-SR04 sensor!");
return;
}
// Checks the water level and outputs the corresponding message to Blynk's virtual pin V2
// the sensor is attached at the top part of the water tank
if (waterLevel > 300) {
Blynk.virtualWrite(V2,"Empty");
}
else if (waterLevel >= 100 && waterLevel <= 300) {
Blynk.virtualWrite(V2,"Good");
}
else {
Blynk.virtualWrite(V2,"Full");
}
}
// Opens lights when you tab the button at Blynk
BLYNK_WRITE (V3) {
int lights_State = param.asInt();
if (lights_State == 1){
digitalWrite(LIGHTSPIN, HIGH);
}
else{
digitalWrite(LIGHTSPIN, LOW);
}
}
// Opens door when you tab the button at Blynk
BLYNK_WRITE (V4) {
int door_State = param.asInt();
if (door_State == 1){
door_Servo.write(180);
}
else{
door_Servo.write(0);
}
}
// Opens water pump when you tab the button at Blynk
BLYNK_WRITE (V5) {
int waterPump_State = param.asInt();
if (waterPump_State == 1){
digitalWrite(WATERPUMBPIN, HIGH);
}
else{
digitalWrite(WATERPUMBPIN, LOW);
}
}
// Open windows through Blynk button
BLYNK_WRITE (V6) {
int windows_State = param.asInt();
if (windows_State == 1){
digitalWrite(WINDOWSPIN, HIGH);
}
else{
digitalWrite(WINDOWSPIN, LOW);
}
}
// Opens windows under specific humidity and temperature
void openWindows(float humidity, float temperature){
if( (humidity < 70 ) || (temperature >= 40)){
digitalWrite(WINDOWSPIN, HIGH);
Blynk.virtualWrite(V6, 1); // Change button state
}
// else if((humidity >=70) || (temperature >= 14 && temperature < 40)){
// digitalWrite(WINDOWSPIN, LOW);
// Blynk.virtualWrite(V6, 0);
// }
// else{
// digitalWrite(WINDOWSPIN, LOW);
// // Blynk.virtualWrite(V6, 0);
// }
}
void getBrightness() {
int analogValue = analogRead(A0);
float voltage = float(analogValue) / 1024.0 * 5.0;
float resistance = 2000.0 * voltage / (1.0 - voltage / 5.0);
float lux = pow(RL10 * 1e3 * pow(10.0, GAMMA) / resistance, (1.0 / GAMMA));
Serial.println(lux);
if(isfinite(lux)){
if(lux >= 10000){
Blynk.virtualWrite(V7, "Sunny");
Serial.println("Sunny");
}
else if ((lux >= 1000) && (lux < 10000)){
Blynk.virtualWrite(V7, "Overcast");
Serial.println("Overcast");
}
else if ((lux >= 100) && (lux < 1000)){
Blynk.virtualWrite(V7, "Partly Cloudy");
// Blynk.virtualWrite(V3, 1);
Serial.println("Partly Cloudy");
}
else {
Blynk.virtualWrite(V7, "Night time");
// Blynk.virtualWrite(V3, 1);
Serial.println("Night time");
}
}
else{
Blynk.virtualWrite(V7, "Too bright");
Serial.println("Too bright");
}
}