arduino,esp8266开发板实现人体感应节能风扇,web页面实时监控风扇运行状态,可通过web页面控制风扇运行状态的代码
时间: 2024-03-05 20:52:53 浏览: 75
以下是一个简单的代码示例,用于使用Arduino和ESP8266开发板实现人体感应节能风扇,并通过Web页面实时监控风扇运行状态,并通过Web页面控制风扇的运行状态。
首先,需要连接Arduino和ESP8266开发板,并将以下代码上传到ESP8266开发板:
```c
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
#define FANPIN 3
#define LEDPIN 4
#define MOTIONPIN 5
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
ESP8266WebServer server(80);
DHT dht(DHTPIN, DHTTYPE);
bool fan_on = false;
void handleRoot() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
String html = "<html><body>";
html += "<h1>Smart Fan</h1>";
html += "<p>Temperature: " + String(temperature) + " °C</p>";
html += "<p>Humidity: " + String(humidity) + " %</p>";
html += "<p>Fan: ";
if (fan_on) {
html += "On</p>";
} else {
html += "Off</p>";
}
html += "<form method=\"post\">";
if (fan_on) {
html += "<button name=\"fan\" value=\"off\">Turn Off Fan</button>";
} else {
html += "<button name=\"fan\" value=\"on\">Turn On Fan</button>";
}
html += "</form>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleFan() {
String value = server.arg("fan");
if (value == "on") {
fan_on = true;
digitalWrite(FANPIN, HIGH);
} else if (value == "off") {
fan_on = false;
digitalWrite(FANPIN, LOW);
}
server.sendHeader("Location", "/");
server.send(302, "text/plain", "");
}
void setup() {
pinMode(FANPIN, OUTPUT);
pinMode(LEDPIN, OUTPUT);
pinMode(MOTIONPIN, INPUT);
digitalWrite(FANPIN, LOW);
digitalWrite(LEDPIN, LOW);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
dht.begin();
server.on("/", handleRoot);
server.on("/fan", handleFan);
server.begin();
Serial.println("Server started");
}
void loop() {
server.handleClient();
int motion = digitalRead(MOTIONPIN);
if (motion == HIGH && !fan_on) {
digitalWrite(FANPIN, HIGH);
digitalWrite(LEDPIN, HIGH);
fan_on = true;
} else if (motion == LOW && fan_on) {
digitalWrite(FANPIN, LOW);
digitalWrite(LEDPIN, LOW);
fan_on = false;
}
delay(2000);
}
```
然后,需要将以下代码上传到Arduino开发板:
```c
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define FANPIN 8
Adafruit_BME280 bme;
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(FANPIN, OUTPUT);
digitalWrite(FANPIN, LOW);
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
void loop() {
float temperature = bme.readTemperature();
float humidity = bme.readHumidity();
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Humidity = ");
Serial.print(humidity);
Serial.println(" %");
if (temperature >= 28 && !digitalRead(FANPIN)) {
digitalWrite(FANPIN, HIGH);
} else if (temperature < 28 && digitalRead(FANPIN)) {
digitalWrite(FANPIN, LOW);
}
delay(5000);
}
```
这些代码将使您的系统能够读取温度和湿度传感器的数据,并根据温度控制风扇的运行状态。此外,它还将提供一个Web页面,您可以使用该页面控制风扇的运行状态并监视传感器数据。
阅读全文