#define BLYNK_TEMPLATE_ID "TMPL2dzny1zfb"
#define BLYNK_TEMPLATE_NAME "Smart soil management"
#define BLYNK_AUTH_TOKEN "2gf-NYKudpMq2ti_Wu6vZCxuxOURNcr7"
#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 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!");
}
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 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(V, rtValue[1]); //Potassium
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
timer.run();
}