#define BLYNK_TEMPLATE_ID "TMPL38PHdd0rP"
#define BLYNK_TEMPLATE_NAME "Smart Fertigation"
#define BLYNK_AUTH_TOKEN "LF8qeIU7Qabzyp4R7OuNm-VUqHznfmHL"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
//#include "DHTesp.h"
#include <HardwareSerial.h>
#include <Arduino.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const int slideSwitchPin = 2; // Change this to the digital pin connected to the slide switch
const int moistureSensorPin = A0; // Change this to the analog pin connected to the moisture sensor
// Define the threshold for activating the slide switch
const int moistureThreshold = 600; // Change this threshold as per your requirement
//const int DHT_PIN = 15;
//DHTesp dhtSensor;
// Potentiometer is connected to GPIO 34 (Analog ADC1_CH6)
const int potPin = 34;
// variable for storing the potentiometer value
int potValue = 0;
#define ncom 3 // number of commands.
char commar[ncom] = {0x1,0x3,0x5}; // Actual commands
// Response Strings can be stored like this
char respar[ncom][30]={"Nitrogen value is: ","Phosphorous value is: ","Potassium value is: "};
uint8_t rtValue[ncom]; // Store the return values from the custom chip in here. you can use the same
//values to forward to the IOT part.
BlynkTimer timer;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial2.begin(15200, SERIAL_8N1, 16, 17); //initialize the custom chip communication line.
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
//dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
timer.setInterval(2000L, sendData); // Send data every 2 seconds
Serial.println("Connected to ESP32!");
pinMode(slideSwitchPin, OUTPUT);
// Set up serial communication
Serial.begin(9600);
}
void sendData() {
//float t = dhtSensor.getTemperature();
// float h = dhtSensor.getHumidity();
/*if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("%\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" °C");*/
potValue = analogRead(potPin);
Serial.println("Moisture: " + String(potValue));
for (uint8_t i = 0; i < ncom; i++) {
Serial2.print((char)commar[i]); // send the command stored in ncom array through serial2
if (Serial2.available()) { // if serial2 data is there
rtValue[i] = Serial2.read(); // read serial2
Serial2.flush(); // flush serial2, very important. otherwise extra bits may interfere with communication
Serial.print(respar[i]); // print the response array to the console.
Serial.println(rtValue[i]); // print the return value with newline at console
}
}
// send data to blynk
// Blynk.virtualWrite(V0, t); // Temperature
// Blynk.virtualWrite(V1, h); // Humidity
Blynk.virtualWrite(V2, potValue); // Soil Moisture
Blynk.virtualWrite(V4, rtValue[0]); // Phosphorous
Blynk.virtualWrite(V3, rtValue[2]); // Nitrogen
Blynk.virtualWrite(V5, rtValue[1]); // Potassium
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
timer.run();
int moistureValue = analogRead(moistureSensorPin);
moistureValue = map(moistureValue, 0, 4095, 0, 1000); // Map the sensor value to 0-1000
// Print the moisture value
Serial.print("Moisture Level: ");
Serial.println(moistureValue);
// Check if moisture level is below threshold
if (moistureValue < moistureThreshold) {
// Slide switch is activated
Serial.println("Slide switch activated");
digitalWrite(slideSwitchPin, HIGH); // Turn ON slide switch
} else {
// Slide switch is deactivated
Serial.println("Slide switch deactivated");
digitalWrite(slideSwitchPin, LOW); // Turn OFF slide switch
}
// Add a delay before the next reading
delay(1000);
}