本次直接分享源码,相关说明视频请参考https://www.bilibili.com/video/BV1UP4y1E7xS/
//Create by F_KUN
//https://fkun.tech/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
//以上部分库请在网上搜索下载
#define OLED_RESET 16 //D0
#define OLED_CLK 5 //D1
#define OLED_MOSI 4 //D2
#define OLED_DC 0 //D3
#define OLED_CS 2 //D4
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define DHTPIN 13
#define DHTTYPE DHT22
#define buzzerPin 3
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const char* ssid = "your SSID";
const char* password = "Wifi passwd";
const char* PARAM_INPUT_1 = "output";
const char* PARAM_INPUT_2 = "state";
DHT dht(DHTPIN, DHTTYPE);
float t = 0.0;
float h = 0.0;
const int relayPin1 = 14; //Dehumidifier
const int relayPin2 = 12;// Light
const int doorpin = 1;
int isauto = 0;
int light = 0;
int Dehumidifier = 0;
int nlight = 0;
int nDehumidifier = 0;
int doorstat = 0;
int dlaststate = 0;
int llaststate = 0;
int humion = 0;
String outputState(int output);
String checkisauto();
AsyncWebServer server(80);
unsigned long previousMillis = 0;
const long interval = 2000;
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<style>
html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; }
h2 { font-size: 3.0rem; }
p { font-size: 3.0rem; }
.units { font-size: 1.2rem; }
.dht-labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px; }
.switch {position: relative; display: inline-block; width: 120px; height: 68px}
.switch input {display: none}
.slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 6px}
.slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 3px}
input:checked+.slider {background-color: #b30000}
input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
</style>
</head>
<body>
<h2>FKUN_Archive_Control</h2>
<p>
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span class="dht-labels">Temperature</span>
<span id="temperature">%TEMPERATURE%</span>
<sup class="units">°C</sup> </p>
<p>
<i class="fas fa-tint" style="color:#00add6;"></i>
<span class="dht-labels">Humidity</span>
<span id="humidity">%HUMIDITY%</span>
<sup class="units">%</sup>
</p>
</body>
<script>
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("temperature").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/temperature", true);
xhttp.send(); }, 5000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200){
document.getElementById("humidity").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/humidity", true);
xhttp.send();
}, 5000 ) ;
</script>
%BUTTONPLACEHOLDER%
<script>
function toggleCheckbox(element) {
var xhr = new XMLHttpRequest();
if(element.checked){ xhr.open("GET", "/update?output="+element.id+"&state=0", true); }
else { xhr.open("GET", "/update?output="+element.id+"&state=1", true); }
xhr.send();
}
</script>
</html>
)rawliteral";
String processor(const String& var){
//Serial.println(var);
if(var == "BUTTONPLACEHOLDER"){
String buttons = "";
buttons += "<h4>Light</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"12\" " + outputState(12) + "><span class=\"slider\"></span></label>";
buttons += "<h4>Dehumidifier</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"14\" " + outputState(14) + "><span class=\"slider\"></span></label>";
buttons += "<h4>AUTO</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"isauto\" " + checkisauto() + "><span class=\"slider\"></span></label>";
return buttons;
}
return String();
//Serial.println(var);
if (var == "TEMPERATURE") {
return String(t);
}
else if (var == "HUMIDITY") {
return String(h);
}
return String();
}
String outputState(int output){
if(digitalRead(output)){
return "";
}
else {
return "checked";
}
}
String checkisauto(){
if(isauto == 1){
return "checked";
}
else {
return "";
}
}
void turnoff(){
tone(buzzerPin, 1200);
delay(120);
noTone(buzzerPin);
delay(30);
tone(buzzerPin, 1000);
delay(110);
noTone(buzzerPin);
delay(30);
tone(buzzerPin, 800);
delay(100);
noTone(buzzerPin);
delay(30);
}
void turnon(){
tone(buzzerPin, 800);
delay(100);
noTone(buzzerPin);
delay(30);
tone(buzzerPin, 1000);
delay(110);
noTone(buzzerPin);
delay(30);
tone(buzzerPin, 1200);
delay(180);
noTone(buzzerPin);
delay(30);
}
void lighton(){
tone(buzzerPin, 1000);
delay(100);
noTone(buzzerPin);
}
void lightoff(){
tone(buzzerPin, 1000);
delay(100);
noTone(buzzerPin);
delay(30);
tone(buzzerPin, 1000);
delay(100);
noTone(buzzerPin);
}
void setup()
{
Serial.begin(9600);//串口波特率
dht.begin();
tone(buzzerPin, 1000);
delay(150);
noTone(buzzerPin);
tone(buzzerPin, 1500);
delay(300);
noTone(buzzerPin);
pinMode(relayPin2, OUTPUT);
digitalWrite(relayPin2, HIGH);
pinMode(relayPin1, OUTPUT);
digitalWrite(relayPin1, HIGH);
pinMode(doorpin, INPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
Serial.println("OLED Ready");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 2);
display.println(F("FKUN Archive"));
display.display();
delay(1000);
display.setCursor(0, 15);
display.println(F("OLED........Ready!"));
display.display();
delay(500);
display.setCursor(0, 25);
display.println(F("SYSTEM......Ready!"));
display.display();
delay(500);
display.setCursor(0, 35);
display.print(F("WiFi info"));
display.display();
delay(100);
display.println(F(":"));
display.display();
delay(500);
display.setCursor(0, 45);
display.print(F("SSID:"));
display.display();
delay(200);
display.println(ssid);
display.display();
delay(500);
display.setCursor(0, 55);
display.print(F("PASSWORD:"));
display.display();
delay(200);
int cnct=10;
while(cnct--)
{
display.print(F("*"));
display.display();
delay(40);
}
delay(800);
Serial.print("Connecting WiFi...");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 21);
display.print(F("Connecting WiFi..."));
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
display.display();
delay(500);
Serial.print(".");
display.print(F("."));
}
Serial.println("");
Serial.print("WiFi connected");
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(5, 25);
display.print(F("Connected!"));
display.display();
delay(300);
display.clearDisplay();
display.display();
delay(100);
display.clearDisplay();
display.setCursor(5, 25);
display.print(F("Connected!"));
display.display();
delay(300);
display.clearDisplay();
display.display();
delay(100);
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(5, 25);
display.print(F("Connected!"));
display.display();
delay(1000);
display.clearDisplay();
display.display();
delay(300);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 2);
display.println(F("IP address:"));
display.display();
delay(500);
display.setCursor(0, 15);
display.println(WiFi.localIP());
display.display();
delay(1000);
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/html", index_html, processor);
});
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/plain", String(t).c_str());
});
server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/plain", String(h).c_str());
});
server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage1;
String inputMessage2;
if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2)) {
inputMessage1 = request->getParam(PARAM_INPUT_1)->value();
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
digitalWrite(inputMessage1.toInt(), inputMessage2.toInt());
}
else {
inputMessage1 = "No message sent";
inputMessage2 = "No message sent";
}
Serial.print("GPIO: ");
Serial.print(inputMessage1);
Serial.print(" - Set to: ");
Serial.println(inputMessage2);
if(inputMessage1=="isauto"){
if(inputMessage2=="0"){
isauto = 1;
Serial.print(" - is AUTO: ");
Serial.println(isauto);
}else{
isauto = 0;
Serial.print(" - is AUTO: ");
Serial.println(isauto);
}
}
request->send(200, "text/plain", "OK");
});
server.begin();
isauto = 1;
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
float newT = dht.readTemperature();
float newH = dht.readHumidity();
if (isnan(newH)) {
display.clearDisplay();
Serial.println("Failed to read from DHT sensor!");
display.setCursor(0, 25);
display.println(F("DHT Sensor Failed!"));
display.display();
}
else {
display.clearDisplay();
h = newH;
t = newT;
Serial.println(t);
Serial.println(h);
display.clearDisplay();
display.setCursor(0, 0);
display.print(F("Temperature:"));
display.setCursor(0, 10);
display.print(t);
display.println(F("C"));
display.setCursor(0, 20);
display.print(F("Humidity:"));
display.setCursor(0, 30);
display.print(h);
display.println(F("%"));
display.display();
}
Dehumidifier = digitalRead(14);
light = digitalRead(12);
if(digitalRead(doorpin)== LOW){
pinMode(relayPin2, OUTPUT);
digitalWrite(relayPin2, HIGH);
display.setCursor(0, 40);
nlight = light;
if(llaststate != nlight){
lightoff();
llaststate = nlight;
}
display.print(F("Light:"));
display.println(F("OFF"));
display.display();
}else{
pinMode(relayPin2, OUTPUT);
digitalWrite(relayPin2, LOW);
Serial.println("Door Open!");
Serial.println("Light ON");
display.setCursor(0, 40);
nlight = light;
if(llaststate != nlight){
lighton();
llaststate = nlight;
}
display.print(F("Light:"));
display.println(F("ON"));
display.display();
}
if(h>50){
humion = 1;
}else if(h < 45){
humion = 0;
}else{
humion = humion;
}
if(isauto==1){
if(humion == 0){
pinMode(relayPin1, OUTPUT);
digitalWrite(relayPin1, HIGH);
nDehumidifier = Dehumidifier;
if(dlaststate != nDehumidifier){
turnoff();
dlaststate = nDehumidifier;
}
display.setCursor(0, 50);
display.print(F("Dehumidifier:"));
display.println(F("OFF (A)"));
display.display();
}else{
pinMode(relayPin1, OUTPUT);
digitalWrite(relayPin1, LOW);
nDehumidifier = Dehumidifier;
if(dlaststate != nDehumidifier){
turnon();
dlaststate = nDehumidifier;
}
display.setCursor(0, 50);
display.print(F("Dehumidifier:"));
display.println(F("ON (A)"));
display.display();
}
}else{
if(light==0){
display.setCursor(0, 40);
nlight = light;
if(llaststate != nlight){
lighton();
llaststate = nlight;
}
display.print(F("Light:"));
display.println(F("ON"));
display.display();
}else{
display.setCursor(0, 40);
nlight = light;
if(llaststate != nlight){
lightoff();
llaststate = nlight;
}
display.print(F("Light:"));
display.println(F("OFF"));
display.display();
}
if(Dehumidifier==0){
nDehumidifier = Dehumidifier;
if(dlaststate != nDehumidifier){
turnon();
dlaststate = nDehumidifier;
}
display.setCursor(0, 50);
display.print(F("Dehumidifier:"));
display.println(F("ON (M)"));
display.display();
}else{
nDehumidifier = Dehumidifier;
if(dlaststate != nDehumidifier){
turnoff();
dlaststate = nDehumidifier;
}
display.setCursor(0, 50);
display.print(F("Dehumidifier:"));
display.println(F("OFF (M)"));
display.display();
}
}
}
}
还有几天就春节了,提前祝福一下|´・ω・)ノ
给您也拜个早年~
感谢分享 赞一个
大佬牛逼!
我有个问题,为啥3D打印机也放进去,打印时味道不是会很大吗
这个打pla的,没有味道。放在柜子里主要是因为风扇声音比较大,也可以少落点灰哈哈。
之前上3D打印课时用的ABS,那味道是真的大