/*************************************************************
This example shows how value can be pushed from Arduino to
the Blynk App.
NOTE:
BlynkTimer provides SimpleTimer functionality:
http://playground.arduino.cc/Code/SimpleTimer
App dashboard setup:
Value Display widget attached to Virtual Pin V5
*************************************************************/
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6sSTSU4i5"
#define BLYNK_TEMPLATE_NAME "Home Automation System"
#define BLYNK_AUTH_TOKEN "AWqFckllrZYAx4vgFjEW3oefqi212ERs"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define sensor 26
int value;
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32_SSL.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const int LDR_PIN =35;
const int relayPin =32;
const int relayPin1 =33;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define DHTPIN 15
#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V6, millis() / 1000);
int potValue = analogRead(34);
Blynk.virtualWrite(V2, potValue);
}
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(V1, h);
Blynk.virtualWrite(V0, t);
}
BLYNK_WRITE(V3)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("V3 Motor status: ");
Serial.println(pinValue);
digitalWrite(2, pinValue);
if (pinValue= 0)
{
digitalWrite(relayPin1,LOW);
delay(500);
}
else if(pinValue= 1)
{
digitalWrite(relayPin1,HIGH);
delay(500);
}
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(1, INPUT);
pinMode(LDR_PIN,INPUT);
pinMode(2, INPUT);
pinMode(34, INPUT);
pinMode(26, INPUT);
Serial.begin(115200);
Wire.begin(23, 22);
lcd.init();
lcd.backlight();
pinMode(relayPin, OUTPUT);
pinMode(relayPin1, OUTPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 443);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8443);
dht.begin();
Wire.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
timer.setInterval(1000L, myTimerEvent);
}
void CO2Level() {
int16_t j = analogRead(2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CO2: ");
lcd.print(String(j));
lcd.setCursor(9, 0);
lcd.print("ppm");
delay(500); // print message at (0, 0)
Serial.print("Analog: ");
Serial.println(analogRead(2));
delay(100);
}
void loop()
{
int16_t i = analogRead(34);
String msg = i < 2165 ? "WET" : i > 3135 ? "DRY" : "OK";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Soil: ");
lcd.print(msg);
delay(500);
if (i< 3135)
{
digitalWrite(relayPin1,LOW);
delay(500);
}
else if(i>2500)
{
digitalWrite(relayPin1,HIGH);
delay(500);
}
int sensorValue=analogRead(LDR_PIN); // Read the LDR value (0-4095)
int brightness =map(sensorValue,0,4095,0,255);// Map to LED brightness (0-255)
if (sensorValue<1000)
{
digitalWrite(relayPin,LOW);
delay(500);
}
else if(sensorValue>2000)
{
digitalWrite(relayPin,HIGH);
delay(500);
}
Serial.println(analogRead(26));
CO2Level();
Blynk.run();
timer.run(); // Initiates BlynkTimer
}