#define BLYNK_AUTH_TOKEN "rctDfVwCBZ7PERy8wMtJPRZivww5Zx7W"
#define BLYNK_TEMPLATE_ID "TMPL3tEgZbd6z"
#define BLYNK_TEMPLATE_NAME "CROP 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
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
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;
const float acidVoltage = 2032.44;
const float neutralVoltage = 1500.0;
const int potPin = 34;
int potValue = 0;
#define ncom 3
char commar[ncom] = {0x1, 0x3, 0x5};
char respar[ncom][30] = {"Phosphorous value is: ", "Potassium value is: ", "Nitrogen value is: "};
uint8_t rtValue[ncom];
BlynkTimer timer;
void displayTitle();
void displayTeamMembers();
void displayAllSensorReadings(unsigned long duration);
void displayThankYou();
void sendData();
void setup() {
Serial.begin(115200);
Serial2.begin(15200, SERIAL_8N1, 16, 17);
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);
Serial.println("Hello, ESP32!");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
displayTitle();
delay(5000);
displayTeamMembers();
delay(5000);
displayAllSensorReadings(10000);
displayThankYou();
}
void displayTitle() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println("CROP 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("Lingeswaran B");
display.println("Krishna M");
display.println("Mukesh D");
display.println("Udhaya Kumar S");
display.println("Venkatesh S");
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);
float voltage = analogValue * (3.3 / 4095.0);
float pHValue = (voltage * 14.0) / 3.3;
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]);
if (Serial2.available()) {
rtValue[i] = Serial2.read();
Serial2.flush();
Serial.print(respar[i]);
Serial.println(rtValue[i]);
}
//send data to blynk
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, h);
Blynk.virtualWrite(V2, potValue);
Blynk.virtualWrite(V4, rtValue[0]);
Blynk.virtualWrite(V3, rtValue[2]);
Blynk.virtualWrite(V5, rtValue[1]);
}
void loop() {
Blynk.run();
timer.run();
}