#define BLYNK_TEMPLATE_ID "TMPL3Yg0xV6Ai"
#define BLYNK_TEMPLATE_NAME "Temperature"
#define BLYNK_AUTH_TOKEN "tFSGlfB3wM9u50JyKz4DWSnc2d7A7hrk"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#define BLYNK_PRINT Serial
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
char auth[] = BLYNK_AUTH_TOKEN;
#define BLYNK_PRINT Serial
#include <Wire.h>
#include <BlynkSimpleEsp32.h> //You need to add it by searching "Blynk" in libraries and install it
#include <DHT.h> //You need to add it by searching "DHT sensor library" in libraries and install it
// DHT sensor settings and configuration
#define DHT_BLYNK_VPIN_TEMPERATURE V0 //Virtual pin on Blynk side
#define DHT_BLYNK_VPIN_HUMIDITY V1 //Virtual pin on Blynk side
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// This function creates the timer object. It's part of Blynk library
BlynkTimer timer;
// DHT related variables
int DHT_ENABLED = 0;
float DHT_HUMIDITY;
float DHT_HUMIDITY_IGNORED_DELTA = 0.01;
float DHT_TEMPERATURE;
float DHT_TEMPERATURE_IGNORED_DELTA = 0.01;
int RUN = 0;
// SETUP BLOCK
// DHT setup
void setupDht() {
Serial.println("DHT startup!");
dht.begin();
DHT_ENABLED = 1;
}
// Sending data from DHT sensor to Blynk
void sendDhtData() {
Serial.println("Sending DHT data");
Blynk.virtualWrite(DHT_BLYNK_VPIN_TEMPERATURE, DHT_TEMPERATURE);
Blynk.virtualWrite(DHT_BLYNK_VPIN_HUMIDITY, DHT_HUMIDITY);
}
// DATA PROCESSING BLOCK
// Reading DHT data
void readAndSendDhtData() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT");
} else {
float humidityDelta = abs(humidity - DHT_HUMIDITY) - DHT_HUMIDITY_IGNORED_DELTA;
float temperatureDelta = abs(temperature - DHT_TEMPERATURE) - DHT_HUMIDITY_IGNORED_DELTA;
if (humidityDelta > 0 || temperatureDelta > 0) {
DHT_HUMIDITY = humidity;
DHT_TEMPERATURE = temperature;
Serial.printf("Humidity: %f%%. Temperature: %f*C.\n", humidity, temperature);
sendDhtData();
}
}
}
void reandAndSendSensorsData() {
readAndSendDhtData();
Serial.println("Sending data from sensors");
}
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V0);
Serial.println("Inside Blynk Connected");
Blynk.syncVirtual(V1);
Serial.println("Inside Blynk Connected");
}
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V0, millis() / 1000);
Blynk.virtualWrite(V1, millis() / 1000);
}
void setup()
{
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN,ssid,pass);
setupDht();
timer.setInterval(5000L, reandAndSendSensorsData);
}
void loop() {
Blynk.run();
timer.run();
}