#define BLYNK_TEMPLATE_NAME "smart soil nutrition management"
#define BLYNK_AUTH_TOKEN "vt-_2hNoylB6XHusBqFFBa-lNwgxXpnM"
#define BLYNK_TEMPLATE_ID "TMPL3ogSnyXXU"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
#include <HardwareSerial.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
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 LED_PIN 2 // Example LED pin
#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("Hello, ESP32!");
pinMode(LED_PIN, OUTPUT); // Initialize LED pin as output
}
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 commar array through Serial2
delay(10); // Small delay to ensure the data has been sent
if (Serial2.available()) { // Check if data is available on Serial2
rtValue[i] = Serial2.read(); // Read from Serial2
Serial.print(respar[i]); // Print the corresponding response message to the console
Serial.println(rtValue[i]); // Print the return value with a newline at the console
} else {
Serial.println("No data available on Serial2"); // Optional: Print message if no data received
}
}
//send data to blynk
Blynk.virtualWrite(V3, potValue); //soil Moisture
Blynk.virtualWrite(V0, rtValue[0]); //Nitrogen
Blynk.virtualWrite(V2, rtValue[2]); //potassium
Blynk.virtualWrite(V1, rtValue[1]); //Phosporous
// Check if soil moisture is below 50 and turn on LED accordingly
if (potValue < 50) {
Serial.println("dry soil is detected");
digitalWrite(LED_PIN, HIGH); // Turn on LED
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED
}
if (rtValue[0] > 50) {
// Print the message to the Wokwi serial console
Serial.println("Nitrogen level is above 50 - Nettle, comfrey and alfalfa plants will grow!");
}
if (rtValue[2] > 50) {
// Print the message to the Wokwi serial console
Serial.println("potassium level is above 50 -potatoes and beets plants will grow!");
}
if (rtValue[1] > 50) {
// Print the message to the Wokwi serial console
Serial.println("phosporous level is above 50 - Beans and lentils plants will grow!");
}
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
timer.run();
}