#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
#include <DHT.h>
#include <DHT_U.h>




// 定义DHT传感器的类型和连接的引脚
#define DHTPIN 2     // 将数据引脚连接到数字2
#define DHTTYPE DHT22   // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE); // 初始化DHT传感器

// 光敏传感器引脚
const int lightSensorPin = A0;

// 继电器引脚
const int lightRelayPin = 4;
const int tempRelayPin = 5;
const int lightPin = 8;

// OLED 显示屏的设置
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// 伺服电机引脚
const int servoPin1 = 6;
const int servoPin2 = 7;
// 温度控制
Servo myServo1;
// 光强控制
Servo myServo2;

const float GAMMA = 0.7;
const float RL10 = 50;

void setup() {
// 初始化串口通信
  Serial.begin(9600);

  

  dht.begin();

  // 设置传感器引脚模式
  pinMode(lightSensorPin, INPUT);
  pinMode(lightRelayPin, OUTPUT);
  pinMode(tempRelayPin, OUTPUT);
  pinMode(lightPin, OUTPUT);


  // 初始化显示屏
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 默认I2C地址为0x3C
  Serial.println(F("SSD1306 allocation failed"));
  for (;;);
  }
  display.display();
  delay(2000); // Pause for 2 seconds
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);

  // 初始化伺服电机
  myServo1.attach(servoPin1);
  myServo2.attach(servoPin2);
  myServo1.write(0); // 初始位置
  myServo2.write(0); // 初始位置
}

void loop() {

  

 
// 读取湿度和温度值
  float h = dht.readHumidity();
  // 读取温度值为摄氏度 (默认设置)
  float t = dht.readTemperature();

  // 检查读数是否失败,并退出循环:
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // 打印温湿度信息到串口
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.println(F("°C "));


  // 读取光敏传感器数据
  int analogValue = analogRead(lightSensorPin);
  float voltage = analogValue / 1024.0 * 5.0;
  float resistance = 2000.0 * voltage / (1.0 - voltage / 5.0);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
  Serial.print("lux: ");
  Serial.println(lux);


  // 更新显示屏
  display.clearDisplay();
  display.setCursor(0, 10);
  display.print("Lux:");
  display.print(lux);
  display.print("Temp:");
  display.println(t);
  display.setCursor(0, 20);
  display.print("Humi: ");
  display.println(h);

if (lux < 300) {
    digitalWrite(lightRelayPin, HIGH);
    myServo2.write(90); // 旋转到0度
    display.setCursor(0, 30);
    display.println("Curtain: open");
    Serial.println("Curtain: open");
  } else {
    digitalWrite(lightRelayPin, LOW); 
    myServo2.write(0); // 旋转到0度
    display.setCursor(0, 30);
    display.println("Curtain: close");
    Serial.println("Curtain: close");
  }
  // 根据光照强度控制灯光
  if (lux < 300) {
    digitalWrite(lightPin, HIGH);
    display.setCursor(0, 40);
    display.println("Light: open");
    Serial.println("Light: open");
  } else{
    digitalWrite(lightPin, LOW); 
    display.setCursor(0, 40);
    display.println("Light: close");
    Serial.println("Light: close");
  }

  // 根据温湿度控通风系统
  if (t > 25.0 or h > 40) {
    digitalWrite(tempRelayPin, HIGH); 
    myServo1.write(90); // 旋转到90度(通风系统打开)
    display.setCursor(0, 50);
    display.println("Ventilator: open");
    Serial.println("Ventilator: open");
  } else {
    digitalWrite(tempRelayPin, LOW); 
    myServo1.write(0); // 旋转到0度(通风系统关闭)
    display.setCursor(0, 50);
    display.println("Ventilator: close");
    Serial.println("Ventilator: close");
  }
    

  
  // 根据光照强度控制遮光帘
  
  display.display();
  // 延迟一段时间再进行下一次读取
  delay(2000);
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module