/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#include <ESP32Servo.h>
#define OLED_RESET 16
Adafruit_SSD1306 display(OLED_RESET);
#define DHTPIN 12
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
Servo myservo;
#define lx 13
const float GAMMA = 0.7;
const float RL10 = 50;
#define but1 14
#define but2 27
#define but3 26
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(lx, INPUT);
myservo.attach(18);
myservo.write(0);
display.begin(SSD1306_SWITCHCAPVCC, 0x3c); //สั่งให้จอ OLED เริ่มทำงานที่ Address 0x3C
display.clearDisplay(); // ลบภาพในหน้าจอทั้งหมด
display.setTextSize(1); // กำหนดขนาดตัวอักษร
display.setTextColor(WHITE);
pinMode(but1 , INPUT);
pinMode(but2 , INPUT);
pinMode(but3 , INPUT);
}
void loop() {
int lux = analogRead(lx);
lux = map(lux , 0 , 4095 , 181 , -2);
myservo.write(lux);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if(digitalRead(but1) == 0)
{
while(digitalRead(but1) == 0)delay(100);
{
but1 == 0;
but2 == 1;
but3 == 1;
//Serial.println("Temp: " + String(t) + "C");
display.setCursor(0,0); // กำหนดตำแหน่ง x,y ที่จะแสดงผล
display.println("Temp: " + String(t) + "C");
display.display();
display.clearDisplay();
}
}
if(digitalRead(but2) == 0)
{
while(digitalRead(but2) == 0)delay(100);
{
but1 == 1;
but2 == 0;
but3 == 1;
//Serial.println("Humidity: " + String(h) + "%");
display.setCursor(0,13); // กำหนดตำแหน่ง x,y ที่จะแสดงผล
display.println("Humidity: " + String(h) + "%");
display.display();
display.clearDisplay();
}
}
if(digitalRead(but3) == 0)
{
while(digitalRead(but3) == 0)delay(100);
{
but1 == 1;
but2 == 1;
but3 == 0;
//Serial.println("Lux: " + String(lux) + "%");
display.setCursor(0,25);
display.println("Lux: " + String(lux) + "%");
display.display();
display.clearDisplay();
}
}
}