webDht11ThreeLEDvarableRegistance
內網上傳溫濕度到三色小夜燈unknown
plain_text
2 years ago
4.6 kB
6
Indexable
Never
//webDht11ThreeLEDvarableRegistance.ino #include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include "DHT.h" #define led_red D1 //D0 連接上 led的紅色引腳 #define led_green D2 #define led_blue D3 #define led_red_brightness 255 //紅色通道數值 0~255 #define led_green_brightness 255 #define led_blue_brightness 255 float multipleNumber = 1.0;//等比例調整亮度 int sensorPin = A0; //類比輸入的PIN腳 電阻中間腳位->A0 float sensorValue = 0; // 類比輸入得到的數值 // Uncomment one of the lines below for whatever DHT sensor type you're using! #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT21 // DHT 21 (AM2301) //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 /*Put your SSID & Password*/ const char* ssid = "aaa"; // Enter SSID here const char* password = "bbb"; //Enter Password here ESP8266WebServer server(80); // DHT Sensor uint8_t DHTPin = D6; // Initialize DHT sensor. DHT dht(DHTPin, DHTTYPE); float Temperature; float Humidity; void setup() { Serial.begin(9600); delay(100); pinMode(DHTPin, INPUT); dht.begin(); Serial.println("Connecting to "); Serial.println(ssid); //connect to your local wi-fi network WiFi.begin(ssid, password); //check wi-fi is connected to wi-fi network while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected..!"); Serial.print("Got IP: "); Serial.println(WiFi.localIP()); server.on("/", handle_OnConnect); server.onNotFound(handle_NotFound); server.begin(); Serial.println("HTTP server started"); pinMode(led_red,OUTPUT); //設置爲輸出模式 pinMode(led_green,OUTPUT); pinMode(led_blue,OUTPUT); pinMode(D4, OUTPUT);//多餘的內建LED analogWrite(led_red,0); //首先,先把所有的燈都關閉 analogWrite(led_green,0); //可以隨意設置開啓關閉哪些燈,搭配出不同組合有不同顏色 analogWrite(led_blue,255); delay(5000); } void loop() { server.handleClient(); // digitalWrite(D0, HIGH); //設定D0腳位高電壓(就是通電) digitalWrite(D4, HIGH); //設定D4腳位高電壓(就是通電) // analogWrite(led_red,0); // analogWrite(led_green,200); // analogWrite(led_blue,0); // delay(2000); // analogWrite(led_red,0); // analogWrite(led_green,0); // analogWrite(led_blue,200); // delay(2000); sensorValue = analogRead(sensorPin); //讀取類比輸入的值會得到0~1023 Serial.print("可變電阻類比輸入:"); Serial.println(sensorValue); sensorValue = map(sensorValue,0,1023,0,255); //將0~1023轉化成0~255 Serial.print("可變電阻類比輸入轉化區間:"); Serial.println(sensorValue); multipleNumber = sensorValue/255; Serial.print("亮度調整值:"); Serial.println(multipleNumber); analogWrite(led_red,led_red_brightness*multipleNumber); //將結果用PWM的方式輸出給LED,改變亮度 analogWrite(led_green,led_green_brightness*multipleNumber); analogWrite(led_blue,led_blue_brightness*multipleNumber); delay(2000); } void handle_OnConnect() { Temperature = dht.readTemperature(); // Gets the values of the temperature Humidity = dht.readHumidity(); // Gets the values of the humidity server.send(200, "text/html", SendHTML(Temperature,Humidity)); } void handle_NotFound(){ server.send(404, "text/plain", "Not found"); } String SendHTML(float Temperaturestat,float Humiditystat){ String ptr = "<!DOCTYPE html> <html>\n"; ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n"; ptr +="<meta charset='utf-8'>\n"; ptr +="<meta http-equiv = 'refresh' content = '1'>\n"; ptr +="<title>ESP8266 Weather Report</title>\n"; ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n"; ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n"; ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n"; ptr +="</style>\n"; ptr +="</head>\n"; ptr +="<body>\n"; ptr +="<div id=\"webpage\">\n"; ptr +="<h1>ESP8266 NodeMCU Weather Report</h1>\n"; ptr +="<p>Temperature: "; ptr +=Temperaturestat; ptr +="°C</p>"; ptr +="<p>Humidity: "; ptr +=Humiditystat; ptr +="%</p>"; ptr +="</div>\n"; ptr +="</body>\n"; ptr +="</html>\n"; return ptr; }