#define BLYNK_TEMPLATE_ID "TMPL3grG9nPT7"
#define BLYNK_TEMPLATE_NAME "soil moisture and npk"
#define BLYNK_AUTH_TOKEN "gFxZ4mVCpGZFPmfDFYFFeOtDOb9_5hxY"
#define ledpin 26
#define BUZZER_PIN 27
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// 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] = {"Potassium value is: ", "Nitrogen value is: ", "Phosphorous value is: "};
uint8_t rtValue[ncom]; // Store the return values from the custom chip in here
LiquidCrystal_I2C lcd(0x27, 20, 4);
void beepBuzzer(int frequency, int duration, int repeat) {
// Function to produce beeping sounds
for (int i = 0; i < repeat; i++) {
tone(BUZZER_PIN, frequency, duration);
delay(300); // Delay between consecutive beeps
}
delay(2000); // Delay after the entire sequence
}
void setup() {
Serial.begin(115200);
Serial2.begin(15200, SERIAL_8N1, 16, 17); // Initialize the custom chip communication line
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("Hello, ESP32!");
pinMode(ledpin, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn on the LCD screen backlight
}
void loop() {
// Reading potentiometer value
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 commar array through serial2
if (Serial2.available()) { // If serial2 data is there
rtValue[i] = Serial2.read(); // Read serial2
Serial2.flush(); // Flush serial2, very important to avoid extra bits interference
Serial.print(respar[i]); // Print the response array to the console
Serial.println(rtValue[i]); // Print the return value with newline at console
}
delay(3000);
}
if ((potValue < 20) && (rtValue[0] < 10 || rtValue[1] < 10 || rtValue[2] < 10)){
digitalWrite(ledpin, HIGH);
beepBuzzer(3000, 1000, 3);
Blynk.virtualWrite(V4, HIGH);
}
else{
digitalWrite(ledpin, LOW);
beepBuzzer(0,0,0);
Blynk.virtualWrite(V4, LOW);
}
lcd.setCursor(0,0);
lcd.print("Moisture : ");
lcd.print(potValue);
lcd.setCursor(0,1);
lcd.print("Nitrogen : ");
lcd.print(rtValue[1]);
lcd.setCursor(0,2);
lcd.print("Phosphorous : ");
lcd.print(rtValue[2]);
lcd.setCursor(0,3);
lcd.print("Potassium : ");
lcd.print(rtValue[0]);
Blynk.virtualWrite(V0, potValue); // Moisture value
Blynk.virtualWrite(V1, rtValue[1]); // Nitrogen value
Blynk.virtualWrite(V2, rtValue[2]); // Phosphorous value
Blynk.virtualWrite(V3, rtValue[0]); // Potassium value
Blynk.run();
}