1 | /*********
|
2 | Rui Santos
|
3 | Complete project details at https://randomnerdtutorials.com/esp8266-nodemcu-access-point-ap-web-server/
|
4 |
|
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 | of this software and associated documentation files.
|
7 | The above copyright notice and this permission notice shall be included in all
|
8 | copies or substantial portions of the Software.
|
9 | *********/
|
10 |
|
11 | // Import required libraries
|
12 | #include <Arduino.h>
|
13 | #include <ESP8266WiFi.h>
|
14 | #include <Hash.h>
|
15 | #include <ESPAsyncTCP.h>
|
16 | #include <ESPAsyncWebServer.h>
|
17 | #include <Adafruit_Sensor.h>
|
18 | #include <DHT.h>
|
19 |
|
20 | const char* ssid = "ESP_Temp";
|
21 | const char* password = "0123456789";
|
22 | int channel = 6;
|
23 | int ssid_hidden = 0;
|
24 | int max_connection = 8;
|
25 |
|
26 |
|
27 | #define DHTPIN 2 // Digital pin connected to the DHT sensor
|
28 |
|
29 | // Uncomment the type of sensor in use:
|
30 | #define DHTTYPE DHT11 // DHT 11
|
31 | //#define DHTTYPE DHT22 // DHT 22 (AM2302)
|
32 | //#define DHTTYPE DHT21 // DHT 21 (AM2301)
|
33 |
|
34 | DHT dht(DHTPIN, DHTTYPE);
|
35 |
|
36 | // current temperature & humidity, updated in loop()
|
37 | float t = 0.0;
|
38 | float h = 0.0;
|
39 |
|
40 | // Create AsyncWebServer object on port 80
|
41 | AsyncWebServer server(80);
|
42 |
|
43 | // Generally, you should use "unsigned long" for variables that hold time
|
44 | // The value will quickly become too large for an int to store
|
45 | unsigned long previousMillis = 0; // will store last time DHT was updated
|
46 |
|
47 | // Updates DHT readings every 10 seconds
|
48 | const long interval = 10000;
|
49 |
|
50 | const char index_html[] PROGMEM = R"rawliteral(
|
51 | <!DOCTYPE HTML><html>
|
52 | <head>
|
53 | <meta name="viewport" content="width=device-width, initial-scale=1">
|
54 | <style>
|
55 | html {
|
56 | font-family: Arial;
|
57 | display: inline-block;
|
58 | margin: 0px auto;
|
59 | text-align: center;
|
60 | }
|
61 | h2 { font-size: 3.0rem; }
|
62 | p { font-size: 3.0rem; }
|
63 | .units { font-size: 1.2rem; }
|
64 | .dht-labels{
|
65 | font-size: 1.5rem;
|
66 | vertical-align:middle;
|
67 | padding-bottom: 15px;
|
68 | }
|
69 | </style>
|
70 | </head>
|
71 | <body>
|
72 | <h2>ESP8266 DHT Server</h2>
|
73 | <p>
|
74 | <span class="dht-labels">Temperature</span>
|
75 | <span id="temperature">%TEMPERATURE%</span>
|
76 | <sup class="units">°C</sup>
|
77 | </p>
|
78 | <p>
|
79 | <span class="dht-labels">Humidity</span>
|
80 | <span id="humidity">%HUMIDITY%</span>
|
81 | <sup class="units">%</sup>
|
82 | </p>
|
83 | </body>
|
84 | <script>
|
85 | setInterval(function ( ) {
|
86 | var xhttp = new XMLHttpRequest();
|
87 | xhttp.onreadystatechange = function() {
|
88 | if (this.readyState == 4 && this.status == 200) {
|
89 | document.getElementById("temperature").innerHTML = this.responseText;
|
90 | }
|
91 | };
|
92 | xhttp.open("GET", "/temperature", true);
|
93 | xhttp.send();
|
94 | }, 10000 ) ;
|
95 |
|
96 | setInterval(function ( ) {
|
97 | var xhttp = new XMLHttpRequest();
|
98 | xhttp.onreadystatechange = function() {
|
99 | if (this.readyState == 4 && this.status == 200) {
|
100 | document.getElementById("humidity").innerHTML = this.responseText;
|
101 | }
|
102 | };
|
103 | xhttp.open("GET", "/humidity", true);
|
104 | xhttp.send();
|
105 | }, 10000 ) ;
|
106 | </script>
|
107 | </html>)rawliteral";
|
108 |
|
109 | // Replaces placeholder with DHT values
|
110 | String processor(const String& var){
|
111 | //Serial.println(var);
|
112 | if(var == "TEMPERATURE"){
|
113 | return String(t);
|
114 | }
|
115 | else if(var == "HUMIDITY"){
|
116 | return String(h);
|
117 | }
|
118 | return String();
|
119 | }
|
120 |
|
121 | void setup(){
|
122 | // Serial port for debugging purposes
|
123 | Serial.begin(115200);
|
124 | dht.begin();
|
125 |
|
126 | Serial.print("Setting AP (Access Point)…");
|
127 | // Remove the password parameter, if you want the AP (Access Point) to be open
|
128 | WiFi.softAP(ssid, password, channel, ssid_hidden, max_connection);
|
129 |
|
130 | IPAddress IP = WiFi.softAPIP();
|
131 | Serial.print("AP IP address: ");
|
132 | Serial.println(IP);
|
133 |
|
134 | // Print ESP8266 Local IP Address
|
135 | Serial.println(WiFi.localIP());
|
136 |
|
137 | // Route for root / web page
|
138 | server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
139 | request->send_P(200, "text/html", index_html, processor);
|
140 | });
|
141 | server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
|
142 | request->send_P(200, "text/plain", String(t).c_str());
|
143 | });
|
144 | server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
|
145 | request->send_P(200, "text/plain", String(h).c_str());
|
146 | });
|
147 |
|
148 | // Start server
|
149 | server.begin();
|
150 | }
|
151 |
|
152 | void loop(){
|
153 | unsigned long currentMillis = millis();
|
154 | if (currentMillis - previousMillis >= interval) {
|
155 | // save the last time you updated the DHT values
|
156 | previousMillis = currentMillis;
|
157 | // Read temperature as Celsius (the default)
|
158 | float newT = dht.readTemperature();
|
159 | // Read temperature as Fahrenheit (isFahrenheit = true)
|
160 | //float newT = dht.readTemperature(true);
|
161 | // if temperature read failed, don't change t value
|
162 | if (isnan(newT)) {
|
163 | Serial.println("Failed to read from DHT sensor!");
|
164 | }
|
165 | else {
|
166 | t = newT;
|
167 | Serial.println(t);
|
168 | }
|
169 | // Read Humidity
|
170 | float newH = dht.readHumidity();
|
171 | // if humidity read failed, don't change h value
|
172 | if (isnan(newH)) {
|
173 | Serial.println("Failed to read from DHT sensor!");
|
174 | }
|
175 | else {
|
176 | h = newH;
|
177 | Serial.println(h);
|
178 | }
|
179 | }
|
180 | }
|