1. 一個 DHT22 。
2. 加多一個 LED 。
3. 更改固定 IP 地圵。
4. 更攺按鍵顏色。
/*************************************************************
從這個網更改的,詳細請看這網
Completeproject details at http://randomnerdtutorials點com
****************************************************************/
// Load Wi-Fi library
#include <ESP8266WiFi.h>
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
const long interval = 4100;
float temp;
float humi;
float temp1;
float humi1;
#include <Wire.h>
#include <DHT.h>
#define DHTPin 2
#define DHTTYPE DHT
DHT dht(DHTPin, DHT22);
// Replace with your network credentials
const char* ssid = "xxxxxxxx"; //????更攺自巳的wifi名稱
const char* password ="xxxxxxxx"; //????更攺自巳的密碼
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the currentoutput state
String output0State = "OFF";
String output1State = "OFF";
String output2State = "OFF";
// Assign output variables to GPIO pins
const int output0 = 14;
const int output1 = 4;
const int output3 = 5;
// Set your Static IP address
IPAddress local_IP(192, 168, 1, 167);//????更改自已IP,這里是固定IP地址,每次開機都是這個地圵.
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional 可不用更改
IPAddress secondaryDNS(8, 8, 4, 4);//optional 可不用更改
//----------------------------------------------------------------------------------
void setup() {
//Initialize the output variables as outputs
pinMode(output0, OUTPUT);
pinMode(output1, OUTPUT);
pinMode(output3, OUTPUT);
//Set outputs to LOW
digitalWrite(output0, LOW);
digitalWrite(output1, LOW);
digitalWrite(output3, LOW);
//////////Serial.begin(9600); //要不要都可以,試機時加入
//--------------------------------------------------------------------------------
dht.begin();
// Configures static IP address
if(!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
/////////// Serial.println("STA Failed to configure");
}
//Connect to Wi-Fi network with SSID and password
///////////Serial.print("Connecting to ");
///////////Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
////////////Serial.print(".");
}
//Print local IP address and start web server
/////////////Serial.println("");
////////////Serial.println("WiFi connected.");
////////////Serial.println("IP address: ");
////////////Serial.println(WiFi.localIP());
server.begin();
}
//-------------------------------------------------------------------------------
void loop(){
//4sec read dhl22 one time
currentMillis = millis();
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
temp1 = dht.readTemperature();
humi1 = dht.readHumidity();
if (!(isnan(humi1) ||isnan(temp1)))
{
temp=temp1;
humi=humi1;
}
}
//-----------------------------------------------------------------------------------------
WiFiClient client = server.available(); // Listen for incomingclients
if(client)
{ // If a new client connects,
//////////////Serial.println("New ClientX1."); // print a message out in the serialport
String currentLine =""; // make aString to hold incoming data from the client
while (client.connected())
{ // loop while the client's connected
// Serial.println("New ClientX2.");
if (client.available())
{ // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
///////////////Serial.write(c); // print it out the serialmonitor
header += c;
if (c == '\n')
{ // if the byte is anewline character
// if the current line isblank, you got two newline characters in a row.
// that's the end of the clientHTTP request, so send a response:
if (currentLine.length() == 0)
{
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blankline:
//-----------------------------------------------------------------------------------------------
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println("Refresh: 10");
client.println();
//---------------------------------------------------------------------------------------
// turns the GPIOs on and off
if (header.indexOf("GET/0/on") >= 0) {
//////////// Serial.println("GPIO 0 on");
output0State ="ON";
digitalWrite(output0, HIGH);
}
else if (header.indexOf("GET /0/off") >= 0){
////////////Serial.println("GPIO 0 off");
output0State ="OFF";
digitalWrite(output0, LOW);
}
else if (header.indexOf("GET /1/on") >= 0) {
//////////// Serial.println("GPIO 1 on");
output1State ="ON";
digitalWrite(output1, HIGH);
}
else if (header.indexOf("GET /1/off") >= 0) {
//////////// Serial.println("GPIO 1 off");
output1State ="OFF";
digitalWrite(output1, LOW);
}
else if (header.indexOf("GET /2/on") >= 0) {
////////////Serial.println("GPIO 3 on");
output2State ="ON";
digitalWrite(output3, HIGH);
}
else if (header.indexOf("GET /2/off") >= 0) {
////////////// Serial.println("GPIO 3 off");
output2State ="OFF";
digitalWrite(output3, LOW);
}
//------------------------------------------------------------------------------
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\"content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\"href=\"data:,\">");
// CSS to style the on/offbuttons
// Feel free to change thebackground-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica;display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #195B6A; border-radius:8px; color: white; padding:16px 40px;");
client.println("text-decoration: none; font-size: 14px; margin:2px; cursor: pointer;}");
client.println(".button2 {background-color:#c3352b;}</style></head>");
// Web Page Heading
client.println("<body><h4>CONTROL 1 DHT 3 LED</h4>");
client.println("Temp: ");
client.println(temp);
client.println("C");
client.println("<br>");
client.println("Humi: ");
client.println(humi);
client.println("%");
//--------------------------------------------------------------------------------------------------------------------
// Display current state, and ON/OFF buttons
// If the output0State is off, it displays the ON button
if (output0State=="OFF")
{
client.println("<p><ahref=\"/0/on\"><button class=\"button\">LED0_OFF</button></a></p>");
}
else
{
client.println("<p><ahref=\"/0/off\"><button class=\"buttonbutton2\">LED0_ON</button></a></p>");
}
//-----------------------------------------------------------------------------------------------------------------------
// Display current state, and ON/OFF buttons
// If the output1State is off, it displays the ON button
if (output1State=="OFF")
{
client.println("<p><ahref=\"/1/on\"><buttonclass=\"button\">LED1_OFF</button></a></p>");
}
else
{
client.println("<p><a href=\"/1/off\"><buttonclass=\"buttonbutton2\">LED1_ON</button></a></p>");
}
//---------------------------------------------------------------------------------------------------------------------
// Display current state, and ON/OFF buttons
// If the output1State is off, it displays the ON button
if (output2State=="OFF")
{
client.println("<p><ahref=\"/2/on\"><buttonclass=\"button\">LED2_OFF</button></a></p>");
}
else
{
client.println("<p><ahref=\"/2/off\"><button class=\"buttonbutton2\">LED2_ON</button></a></p>");
}
//------------------------------------------------------------------------------------------------------------
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
}
else
{ // if you got a newline, thenclear currentLine
currentLine = "";
}
}
else if (c != '\r')
{ // if you got anything elsebut a carriage return character,
currentLine += c; // add it tothe end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
//////////////Serial.println("Client disconnected.");
}
}