#include "DHT.h"
#define BLYNK_TEMPLATE_ID "TMPL60x2SN1ks"
#define BLYNK_TEMPLATE_NAME "ss"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
//#include <NewPing.h>
//จำลองesp32 wifi x Blynk บนWokwi
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "yGCw56mAnYcsRyr1m2WKNhchAxsVCCtW"; // ใส่ Auth Token ของ Blynk ที่ได้รับจากแอปพลิเคชัน Blynk
//char ssid[] = "YOUR NETWORK NAME"; // ใส่ชื่อ WiFi ที่ต้องการเชื่อมต่อ
//char pass[] = "YOUR PASSWORD"; // ใส่รหัสผ่าน WiFi ที่ต้องการเชื่อมต่อ
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
int pinLED = 4; // ใส่หมายเลขของพิน GPIO ที่เชื่อมกับ LED
int pinLED2 = 16; // ใส่หมายเลขของพิน GPIO ที่เชื่อมกับ LED
int pinLED3 = 17; // ใส่หมายเลขของพิน GPIO ที่เชื่อมกับ LED
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
pinMode(pinLED, OUTPUT); // กำหนดหน้าที่ให้ pinLED ขา 4 ทำหน้าที่เป็นเอาต์พุต
pinMode(pinLED2, OUTPUT); // กำหนดหน้าที่ให้ pinLED ขา 6 ทำหน้าที่เป็นเอาต์พุต
pinMode(pinLED3, OUTPUT); // กำหนดหน้าที่ให้ pinLED ขา 6 ทำหน้าที่เป็นเอาต์พุต
Blynk.begin(auth, ssid, pass);
dht.begin();
}
void loop() {
sensor_DHT(); ///เรียกใช้ฟังก์ชัน sensor_DHT
Blynk.run();
digitalWrite(pinLED, HIGH);
delay(1000);
digitalWrite(pinLED, LOW);
delay(1000);
}
BLYNK_WRITE(V2)
{
int ledState = param.asInt();
if (ledState == 1) {
digitalWrite(pinLED2, HIGH);
} else {
digitalWrite(pinLED2, LOW);
}
}
BLYNK_WRITE(V3)
{
int ledState = param.asInt();
if (ledState == 1) {
digitalWrite(pinLED3, HIGH);
} else {
digitalWrite(pinLED3, LOW);
}
}
//////////////////Function///////////////////////////
void sensor_DHT(){
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F(" C "));
Serial.print(f);
Serial.print(F(" F Heat index: "));
Serial.print(hic);
Serial.print(F(" C "));
Serial.print(hif);
Serial.println(F(" F"));
}