#define BLYNK_TEMPLATE_ID "TMPL6_ark4qc2"
#define BLYNK_TEMPLATE_NAME "Hydroponic farming"
#define BLYNK_AUTH_TOKEN "STjE-HZuGj3HQicdw-Ip1qE5R2eFhR3N"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#include <BlynkSimpleEsp32.h>
#define LED_PIN 15
const int potPin = 33;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
double dist;
boolean b_pumpworkingFlag;
DHT dht14(14, DHT11);
#include <WiFi.h>
#include <WiFiClient.h>
DHT Sensor(5, DHT11);
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
double fnc_ultrasonic_distance(int _t, int _e) {
unsigned long dur = 0;
digitalWrite(_t, LOW);
delayMicroseconds(5);
digitalWrite(_t, HIGH);
delayMicroseconds(10);
digitalWrite(_t, LOW);
dur = pulseIn(_e, HIGH, 18000);
Serial.print("Echo Duration: ");
Serial.println(dur);
if (dur == 0) return 999.0;
return (dur / 57);
}
void setup() {
pinMode(35, INPUT);
pinMode(32, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, INPUT);
pinMode(18, OUTPUT);
pinMode(14, INPUT);
pinMode(12, OUTPUT);
pinMode(15, OUTPUT);
pinMode(33, INPUT);
Serial.begin(115200);
Serial.flush();
while (Serial.available() > 0) Serial.read();
dht14.begin();
b_pumpworkingFlag = false;
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;) ;
}
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
Blynk.begin(auth, ssid, pass);
Serial.println("Setup complete");
}
void loop() {
Blynk.run();
Serial.println("Loop iteration");
yield();
// Read sensor data
double temperature = dht14.readTemperature();
double humidity = dht14.readHumidity();
// Display temperature and humidity on separate lines
String line1 = "Temp: " + String(temperature, 1) + "C";
String line2 = "Humidity: " + String(humidity, 1) + "%";
String line3;
if ((analogRead(35) < 100)) {
digitalWrite(32, HIGH);
line3 = F("Light: ON ");
} else {
digitalWrite(32, LOW);
line3 = F("Light: OFF");
}
int potValue = analogRead(potPin);
if (potValue > 2000) {
digitalWrite(LED_PIN, HIGH);
b_pumpworkingFlag = true;
line3 = F("Pump: ON ");
} else {
digitalWrite(LED_PIN, LOW);
b_pumpworkingFlag = false;
line3 = F("Pump: OFF");
}
// Adjust these thresholds based on your specific requirements
const int humidity_threshold = 60; // Percentage
const int light_threshold = 10000; // Lux
const int min_water_level = 2; // Minimum water level in cm
const int max_water_level = 40; // Maximum water level in cm
int mappedWaterLevel = map(potValue, 0, 4095, 0, 100);;
// Map potentiometer value to a range suitable for water level
int waterLevel = map(potValue, 0, 4095, 0, 100); // Adjust the mapping range as needed
String line5 = "Water Level: " + String(waterLevel) + "%";
String line4 = "Humidity OK: " + (humidity > humidity_threshold ? String("Yes") : String("No"));
String line6 = "Light for Farming: " + (analogRead(35) > light_threshold ? String("Yes") : String("No"));
display.clearDisplay();
display.setCursor(0, 0);
display.print(line1);
display.setCursor(0, 10);
display.print(line2);
display.setCursor(0, 20);
display.print(line3);
display.setCursor(0, 30);
display.print(line4);
display.setCursor(0, 40);
display.print(line5);
display.setCursor(0, 50);
display.print(line6);
// Send additional data to Blynk using Blynk.virtualWrite
Blynk.virtualWrite(V1, b_pumpworkingFlag ? 1 : 0); // Update Blynk LED Widget for pump
Blynk.virtualWrite(V2, temperature); // Update Blynk Gauge Widget
Blynk.virtualWrite(V3, String(humidity)); // Update Blynk Label Widget
Blynk.virtualWrite(V4, analogRead(35) < 100 ? 1 : 0); // Update Blynk LED Widget for light
Blynk.virtualWrite(V5, mappedWaterLevel); // Update Blynk Label Widget
delay(1000);
display.display();
display.clearDisplay();
}\