#define BLYNK_TEMPLATE_NAME "ANBU SIVAM B"
#define BLYNK_TEMPLATE_ID "TMPL3uOVh2tUW"
#define BLYNK_TEMPLATE_NAME "ANBU SIVAM B"
#define BLYNK_AUTH_TOKEN "13cbsUY9aI42UBaYbtZQ5Y5olVBVFJXe"
#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;
const int potPin = 34;
int potValue = 0;
#define ncom 3
char commar[ncom] = {0x1,0x3,0x5};
char respar[ncom][30]={"Nitrogen value is: ","Phosphorous value is: ","Potassium value is: "};
uint8_t rtValue[ncom];
BlynkTimer timer;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial2.begin(15200, SERIAL_8N1, 16, 17);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
timer.setInterval(2000L, sendData);
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]);
if (Serial2.available())
{ //if serial2 data is there
rtValue[i]= Serial2.read();
Serial2.flush();
Serial.print(respar[i]);
Serial.println(rtValue[i]);
}
}
Blynk.virtualWrite(V6, t);
Blynk.virtualWrite(V1, h);
Blynk.virtualWrite(V5, potValue);
Blynk.virtualWrite(V2, rtValue[0]);
Blynk.virtualWrite(V3, rtValue[2]);
Blynk.virtualWrite(V4, rtValue[1]);
}
void loop() {
Blynk.run();
timer.run();
}