#define BLYNK_TEMPLATE_ID "TMPL3YrYLH1QV"
#define BLYNK_TEMPLATE_NAME "Soil Monitor"
#define BLYNK_AUTH_TOKEN "MxCr0-Ja78OibbjUVwN-dwmDi3REo7gi"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#include "DHTesp.h"
#include <HardwareSerial.h>
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 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, sendDHTData); // Send DHT data every 2 seconds
Serial.println("Hello, ESP32!");
}
void sendDHTData() {
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 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
}
}
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, h);
Blynk.virtualWrite(V2, potValue);//pot
Blynk.virtualWrite(V4, rtValue[0]);//phro
Blynk.virtualWrite(V3, rtValue[2]);//nit
Blynk.virtualWrite(V2, rtValue[1]);//pot
}
void loop() {
// put your main code here, to run repeatedly:
// TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Serial.println("Temp: " + String(data.temperature, 2) + "°C");
// Serial.println("Humidity: " + String(data.humidity, 1) + "%");
//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 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
}
}
delay(500); // this speeds up the simulation*/
Blynk.run();
timer.run();
}