/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "your data"
#define BLYNK_TEMPLATE_NAME "your data"
#define BLYNK_AUTH_TOKEN "your data"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
//นำเข้า library ชื่อ DHT.h และกำหนดค่าที่เกี่ยวข้อง
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "your data";
char pass[] = "your data";
//กำหนดค่าให้ตัวแปรชื่อ timer เป็น object ของคลาส BlynkTimer
//ซึ่ง timer จะถูกนำไปใช้ตอนจับเวลาให้ฟังก์ชันทำงานทุกๆ x มิลลิวินาทีตามต้องการ
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
//ฟังก์ชันนี้จะอ่านค่าอุณหภูมิจาก DHT22 เป็นองศาเซลเซียสเก็บไว้ในตัวแปร t
//แล้วจะแสดงค่าในตัวแปร t ออกทาง serial monitor
//และส่งค่าในตัวแปร t ผ่าน Virtual pin 5 ไปยัง Blynk App
//นั่นคือเป็นการ push data from ESP32 ไปยัง Blynk App dashboard
void myTimerEvent()
{
//ฟังก์ชันนี้อยู่ใน Lab 4 เป็นฟังก์ชันอ่านอุณหภูมิจาก DHT22 เป็นองศาเซลเซียส
float t = dht.readTemperature();
Serial.println(String(t));
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, t);
}
void setup()
{
// Debug console
Serial.begin(115200);
dht.begin();
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
//คำสั่งนี้เป็นคำสั่งที่อยู่ในไลบรารี่ BlynkSimpleEsp32.h
//คำสั่ง setInterval นี้จะเรียกฟังก์ชันชื่อ myTimeEvent ทุกๆ 1000 มิลลิวินาที (หนึ่งนาที)
timer.setInterval(1000L, myTimerEvent);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
}