/*
* STM32 Greenhouse - 蜂鸣器提示版
*
* 任何设备开启/关闭时蜂鸣器响一下
* 报警时连续响
*
* 引脚:
* B6 = DHT
* A0 = 土壤
* A1 = 光照
* A8 = 继电器1 水泵
* B9 = 继电器2 风机
* B10 = 继电器3 灯光
* A11 = 继电器4 发热
* PB3 = 蜂鸣器
*/
#define PIN_DHT PB6
#define PIN_SOIL PA0
#define PIN_LIGHT PA1
#define PIN_PUMP PA8
#define PIN_FAN PB9
#define PIN_LIGHT_R PB10
#define PIN_HEATER PA11
#define PIN_BUZZER PB3
// 阈值
#define TEMP_HEAT_ON 15.0
#define TEMP_HEAT_OFF 35.0
#define TEMP_FAN_ON 30.0
#define TEMP_FAN_OFF 25.0
#define HUMI_PUMP_ON 30.0
#define HUMI_PUMP_OFF 90.0
#define LIGHT_ON 200
#define LIGHT_OFF 400
// 变量
float temperature = 25.0;
float humidity = 60.0;
int soilValue = 500;
int lightValue = 500;
bool pumpState = false;
bool fanState = false;
bool lightState = false;
bool heaterState = false;
unsigned long lastTime = 0;
// ===== 蜂鸣器响一声 =====
void beepOnce() {
tone(PIN_BUZZER, 1500, 150);
delay(200);
}
// ===== 蜂鸣器连续响 =====
void beepAlarm() {
tone(PIN_BUZZER, 2000, 300);
}
// ===== DHT读取 =====
bool readDHT() {
uint8_t bits[40];
uint8_t data[5] = {0};
pinMode(PIN_DHT, OUTPUT);
digitalWrite(PIN_DHT, LOW);
delay(25);
noInterrupts();
digitalWrite(PIN_DHT, HIGH);
delayMicroseconds(25);
pinMode(PIN_DHT, INPUT);
delayMicroseconds(5);
unsigned long t = micros();
while (digitalRead(PIN_DHT) == HIGH) if (micros() - t > 500) { interrupts(); return false; }
t = micros();
while (digitalRead(PIN_DHT) == LOW) if (micros() - t > 500) { interrupts(); return false; }
t = micros();
while (digitalRead(PIN_DHT) == HIGH) if (micros() - t > 500) { interrupts(); return false; }
for (int i = 0; i < 40; i++) {
t = micros();
while (digitalRead(PIN_DHT) == LOW) if (micros() - t > 500) { interrupts(); return false; }
t = micros();
while (digitalRead(PIN_DHT) == HIGH) if (micros() - t > 500) { interrupts(); return false; }
bits[i] = ((micros() - t) > 35) ? 1 : 0;
}
interrupts();
for (int i = 0; i < 40; i++) {
if (bits[i]) data[i / 8] |= (1 << (7 - i % 8));
}
if (data[4] != (data[0] + data[1] + data[2] + data[3])) return false;
humidity = data[0] + data[1] * 0.1;
temperature = data[2] + data[3] * 0.1;
if (humidity > 100 || temperature > 80) return false;
return true;
}
// ===== 继电器控制 =====
void setPump(bool on) {
pumpState = on;
digitalWrite(PIN_PUMP, on ? LOW : HIGH);
Serial.print("[PUMP] ");
Serial.println(on ? "ON" : "OFF");
beepOnce();
}
void setFan(bool on) {
fanState = on;
digitalWrite(PIN_FAN, on ? LOW : HIGH);
Serial.print("[FAN] ");
Serial.println(on ? "ON" : "OFF");
beepOnce();
}
void setLight(bool on) {
lightState = on;
digitalWrite(PIN_LIGHT_R, on ? LOW : HIGH);
Serial.print("[LIGHT] ");
Serial.println(on ? "ON" : "OFF");
beepOnce();
}
void setHeater(bool on) {
heaterState = on;
digitalWrite(PIN_HEATER, on ? LOW : HIGH);
Serial.print("[HEATER] ");
Serial.println(on ? "ON" : "OFF");
beepOnce();
}
// ===== 初始化 =====
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("\n==============================");
Serial.println(" Greenhouse v3.1");
Serial.println("==============================");
pinMode(PIN_PUMP, OUTPUT);
pinMode(PIN_FAN, OUTPUT);
pinMode(PIN_LIGHT_R, OUTPUT);
pinMode(PIN_HEATER, OUTPUT);
pinMode(PIN_BUZZER, OUTPUT);
digitalWrite(PIN_PUMP, HIGH);
digitalWrite(PIN_FAN, HIGH);
digitalWrite(PIN_LIGHT_R, HIGH);
digitalWrite(PIN_HEATER, HIGH);
digitalWrite(PIN_BUZZER, LOW);
pinMode(PIN_DHT, INPUT_PULLUP);
delay(500);
beepOnce();
Serial.println("Ready!\n");
}
// ===== 主循环 =====
void loop() {
unsigned long now = millis();
if (now - lastTime >= 3000) {
lastTime = now;
Serial.println("\n--- Read ---");
// 读DHT
bool ok = readDHT();
if (!ok) {
Serial.println("[DHT] FAIL, sim");
temperature = 20.0 + random(0, 200) / 10.0;
humidity = 40.0 + random(0, 400) / 10.0;
}
// 读电位器
soilValue = analogRead(PIN_SOIL);
lightValue = analogRead(PIN_LIGHT);
Serial.print("T:"); Serial.print(temperature, 1);
Serial.print(" H:"); Serial.print(humidity, 1);
Serial.print(" S:"); Serial.print(soilValue);
Serial.print(" L:"); Serial.println(lightValue);
// ===== 发热控制 =====
if (temperature < TEMP_HEAT_ON && !heaterState) {
setHeater(true);
} else if (temperature > TEMP_HEAT_OFF && heaterState) {
setHeater(false);
}
// ===== 水泵控制 =====
if (humidity < HUMI_PUMP_ON && !pumpState) {
setPump(true);
} else if (humidity > HUMI_PUMP_OFF && pumpState) {
setPump(false);
}
// ===== 风机控制 =====
if (temperature > TEMP_FAN_ON && !fanState) {
setFan(true);
} else if (temperature < TEMP_FAN_OFF && fanState) {
setFan(false);
}
// ===== 灯光控制 =====
if (lightValue < LIGHT_ON && !lightState) {
setLight(true);
} else if (lightValue > LIGHT_OFF && lightState) {
setLight(false);
}
// ===== 报警 =====
if (temperature > 40.0 || humidity < 20.0) {
beepAlarm();
Serial.println("[ALARM]!");
}
// 状态
Serial.print("Status: ");
Serial.print(heaterState ? "HEAT " : "heat ");
Serial.print(pumpState ? "PUMP " : "pump ");
Serial.print(fanState ? "FAN " : "fan ");
Serial.println(lightState ? "LITE" : "lite");
}
delay(100);
}