#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6IifABHuy"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#include "DHT.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "BJHwRQHRccULSAqC18ML6jodDhJEwB5l";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const int analogPin = 34;
int ledPins[] = {13, 12, 14, 27, 26, 25, 33, 32 ,35 ,34 };
int ledCount = 10; // จำนวน LED
#define DHT22PIN 4
#define DHTTYPE DHT22 // สมมติว่าใช้เซ็นเซอร์ DHT22
DHT dht(DHT22PIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// เริ่มต้นใช้งานเซ็นเซอร์ DHT22
dht.begin();
// กำหนดโหมดของ LED pins
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
// กำหนดโหมดของพิน 15 เป็นเอาต์พุต (เช่น ควบคุมรีเลย์)
pinMode(15, OUTPUT);
// เชื่อมต่อ WiFi และ Blynk
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
float humi = dht.readHumidity(); // Read humidity
float temp = dht.readTemperature(); // Read temperature
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("ºC ");
Serial.print("Humidity: ");
Serial.print(humi);
Serial.println(" %");
// Control relay or any other action based on temperature
if (temp > 30) {
digitalWrite(15, HIGH); // Turn on pin 15 (or perform another action)
} else if (temp < 25) {
digitalWrite(15, LOW); // Turn off pin 15 (or perform another action)
}
// Map temperature to control LEDs
int ledLevel = map(temp, 20, 35, 0, ledCount); // Map temperature range to LED count range
// Adjust LED states based on mapped value
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH); // Turn on LED up to the level of ledLevel
} else {
digitalWrite(ledPins[thisLed], LOW); // Turn off LED beyond ledLevel
}
}
// Send temperature and humidity to Blynk
Blynk.virtualWrite(V1, temp); // V1 is the Virtual Pin used for temperature in Blynk
Blynk.virtualWrite(V2, humi); // V2 is the Virtual Pin used for humidity in Blynk
delay(1000); // Delay between readings
}