#define BLYNK_AUTH_TOKEN "rctDfVwCBZ7PERy8wMtJPRZivww5Zx7W"
#define BLYNK_TEMPLATE_ID "TMPL3tEgZbd6z"
#define BLYNK_TEMPLATE_NAME "SOIL NUTRITION MONITORING IOT"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHTesp.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const int DHT_PIN = 27;
DHTesp dhtSensor;
const int pHSensorPin = 32;
// Calibration parameters (you need to calibrate your sensor)
const float acidVoltage = 2032.44; // voltage at pH 4.0
const float neutralVoltage = 1500.0; // voltage at pH 7.0
// 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] = {"Phosphorous value is: ", "Potassium value is: ", "Nitrogen 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 displayTitle();
void displayTeamMembers();
void displayAllSensorReadings(unsigned long duration);
void displayThankYou();
void sendData();
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.
// Initialize Blynk
Serial.println("Connecting to Blynk...");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
while (!Blynk.connected()) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("Blynk connected.");
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
timer.setInterval(200L, sendData); // Send data every 2 seconds
Serial.println("Hello, ESP32!");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize with the I2C address 0x3C
display.clearDisplay(); // Clear the buffer
display.display(); // Display
// Display title for 5 seconds
displayTitle();
delay(5000);
// Display team members for 5 seconds
displayTeamMembers();
delay(5000);
// Display all sensor readings for 10 seconds
displayAllSensorReadings(10000);
// Display "Thank you" message
displayThankYou();
}
void displayTitle() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println("SOIL NUTRITION MONITORING IOT");
display.println("System Using IoT");
display.display();
}
void displayTeamMembers() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println("Team Members:");
display.println("Subhageetha");
display.println("Mhageshwari Meena");
display.println("Yukthanidhi");
display.println("Jothi sri");
display.display();
}
void displayAllSensorReadings(unsigned long duration) {
float h = dhtSensor.getHumidity();
float t = dhtSensor.getTemperature();
int potValue = analogRead(potPin);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println("All Sensor Readings:");
display.print("Temperature: ");
display.println(t, 2);
display.print("Humidity: ");
display.println(h, 2);
display.print("Soil Moisture: ");
display.println(potValue);
display.display();
delay(duration);
}
void displayThankYou() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 20);
display.println("Thank you!");
display.display();
}
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");
int analogValue = analogRead(pHSensorPin);
// Convert the analog value to voltage
float voltage = analogValue * (3.3 / 4095.0); // 3.3V reference, 12-bit ADC
// Convert the voltage to pH value and moisture
float pHValue = (voltage * 14.0) / 3.3; // Declare pHValue here
potValue = analogRead(potPin);
Serial.println("Moisture: " + String(potValue));
Serial.print("pH Value: ");
Serial.println(pHValue, 1);
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();
}