#define BLYNK_TEMPLATE_ID "TMPL3DVdOd7NX"
#define BLYNK_TEMPLATE_NAME "Smart soil nutrition monitoring system"
#define BLYNK_AUTH_TOKEN "yPM0UWhoOYudSWwcQUgW74ip_Q9z7oer"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
#include <HardwareSerial.h>
char ssid[] = "Galaxy 21";
char pass[] = "varshini";
const int DHT_PIN = 15;
DHTesp dhtSensor;
// Potentiometer is connected to GPIO 34 (Analog ADC1_CH6)
const int potPin = 34;
// Buzzer pin
const int buzzerPin = 2;
// 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
BlynkTimer timer;
void setup() {
Serial.begin(115200);
Serial2.begin(15200, SERIAL_8N1, 16, 17); // Custom chip communication line
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(2000L, sendData); // Send data every 2 seconds
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(buzzerPin, OUTPUT); // Initialize buzzer pin as output
Serial.println("Hello, ESP32!");
}
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 command through Serial2
if (Serial2.available()) {
rtValue[i] = Serial2.read(); // Read response from Serial2
Serial2.flush(); // Clear Serial2 buffer
Serial.print(respar[i]); // Print response
Serial.println(rtValue[i]);
}
}
// Send data to Blynk with correct virtual pins
Blynk.virtualWrite(V0, potValue); // Soil moisture
Blynk.virtualWrite(V1, rtValue[0]); // Nitrogen
Blynk.virtualWrite(V2, rtValue[1]); // Phosphorous
Blynk.virtualWrite(V3, rtValue[2]); // Potassium
Blynk.virtualWrite(V4, t); // Temperature
Blynk.virtualWrite(V5, h); // Humidity
// Control the buzzer based on the moisture level
if (potValue < 300) {
digitalWrite(buzzerPin, HIGH); // Turn on buzzer
} else {
digitalWrite(buzzerPin, LOW); // Turn off buzzer
}
}
void loop() {
Blynk.run();
timer.run();
}