TEXT 131
Untitled Guest on 23rd January 2021 12:36:29 AM
  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     = "ESP8266-Access-Point";
  21. const char* password = "123456789";
  22.  
  23. #define DHTPIN 2     // Digital pin connected to the DHT sensor
  24.  
  25. // Uncomment the type of sensor in use:
  26. #define DHTTYPE    DHT11     // DHT 11
  27. //#define DHTTYPE    DHT22     // DHT 22 (AM2302)
  28. //#define DHTTYPE    DHT21     // DHT 21 (AM2301)
  29.  
  30. DHT dht(DHTPIN, DHTTYPE);
  31.  
  32. // current temperature & humidity, updated in loop()
  33. float t = 0.0;
  34. float h = 0.0;
  35.  
  36. // Create AsyncWebServer object on port 80
  37. AsyncWebServer server(80);
  38.  
  39. // Generally, you should use "unsigned long" for variables that hold time
  40. // The value will quickly become too large for an int to store
  41. unsigned long previousMillis = 0;    // will store last time DHT was updated
  42.  
  43. // Updates DHT readings every 10 seconds
  44. const long interval = 10000;
  45.  
  46. const char index_html[] PROGMEM = R"rawliteral(
  47. <!DOCTYPE HTML><html>
  48. <head>
  49.   <meta name="viewport" content="width=device-width, initial-scale=1">
  50.   <style>
  51.     html {
  52.      font-family: Arial;
  53.      display: inline-block;
  54.      margin: 0px auto;
  55.      text-align: center;
  56.     }
  57.     h2 { font-size: 3.0rem; }
  58.     p { font-size: 3.0rem; }
  59.     .units { font-size: 1.2rem; }
  60.     .dht-labels{
  61.       font-size: 1.5rem;
  62.       vertical-align:middle;
  63.       padding-bottom: 15px;
  64.     }
  65.   </style>
  66. </head>
  67. <body>
  68. <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAABHNCSVQICAgIfAhkiAAAAAFzUkdCAK7OHOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAADypJREFUeF7t3elvXNUZBvD33lm9JXY2Z8MhAQKkqAJBkSjdUBFtVSG1/dLlS/+wfmo/dIGqBdGyiCIBYqcsKdk3EmchcRI78RLPcu+dvs+ZO7YnHh/vvtvzQ/Y4E8chM+e5Z73nOA0lRNSRGz4SUQcMCJEFA0JkwYAQWTAgRBYMCJEFA0JkwYAQWTAgRBYMCJEFA0JkwYAQWTAgRBYMCJEFA0JkwYAQWTAgRBYMCJEFA0JkwYAQWTAgRBYMCJEFA0JkwYAQWTAgRBYMCJEFA0JkwYAQWTAgRBYMCJEFA0JkwYAQWTAgRBYMCJEFA0JkwYAQWfAQzwV0elkcxwm/oqzIdECm6w25OR3ItTuBXNePqi9S8RoyrR81/XpuHJCNct6Rcq752Ft0ZEe3Kzt6XNladhielMpUQIKgIZcmAzk96snFiUD8QGsKfd7Vso0PFHGU82ZRb36e1TDfa77SL/SPiv448zW+s6/oyoEBVw4O5GRTiS3XtMhEQM7f9uXYDU+uTQWilYMUtPzm5pT/1Vz9Wy8fPnuaGoQOtcu9/Tl5bHteyoWV/2yKXmoDUvcbcuSGL4dH6qbg5sNQrHdTCK8m6hoEpaYfO3sceXxnQfb2aduMEieVAfnwcl2O3fTM16gtEIko+gh4adEMQ1D6S458/56iBobNryRJVUC+ul6Xj694pj9hghGTjjNe4UBrlapmdluXI88MFWWgi0FJglQE5FY1kH+fqcq0FsCStmTiOqKEFxoDBahRvrU1L9/dW2j+BsVW4gPy0ZW6/G/Ek3Je/zH4L57ZaIOXHCFBmJ/bXzLD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mBAiBYk8n+IUNVrk0BAXAAAAABJRU5ErkJggg==">
  69.   <h2>ESP8266 DHT Server</h2>
  70.   <p>
  71.     <span class="dht-labels">Temperature</span>
  72.     <span id="temperature">%TEMPERATURE%</span>
  73.     <sup class="units">°C</sup>
  74.   </p>
  75.   <p>
  76.     <span class="dht-labels">Humidity</span>
  77.     <span id="humidity">%HUMIDITY%</span>
  78.     <sup class="units">%</sup>
  79.   </p>
  80. </body>
  81. <script>
  82. setInterval(function ( ) {
  83.   var xhttp = new XMLHttpRequest();
  84.   xhttp.onreadystatechange = function() {
  85.     if (this.readyState == 4 && this.status == 200) {
  86.       document.getElementById("temperature").innerHTML = this.responseText;
  87.     }
  88.   };
  89.   xhttp.open("GET", "/temperature", true);
  90.   xhttp.send();
  91. }, 10000 ) ;
  92.  
  93. setInterval(function ( ) {
  94.   var xhttp = new XMLHttpRequest();
  95.   xhttp.onreadystatechange = function() {
  96.     if (this.readyState == 4 && this.status == 200) {
  97.       document.getElementById("humidity").innerHTML = this.responseText;
  98.     }
  99.   };
  100.   xhttp.open("GET", "/humidity", true);
  101.   xhttp.send();
  102. }, 10000 ) ;
  103. </script>
  104. </html>)rawliteral";
  105.  
  106. // Replaces placeholder with DHT values
  107. String processor(const String& var) {
  108.   //Serial.println(var);
  109.   if (var == "TEMPERATURE") {
  110.     return String(t);
  111.   }
  112.   else if (var == "HUMIDITY") {
  113.     return String(h);
  114.   }
  115.   return String();
  116. }
  117.  
  118. void setup() {
  119.   // Serial port for debugging purposes
  120.   Serial.begin(9600);
  121.   dht.begin();
  122.  
  123.   Serial.print("Setting AP (Access Point)…");
  124.   // Remove the password parameter, if you want the AP (Access Point) to be open
  125.   WiFi.softAP(ssid, password);
  126.  
  127.   IPAddress IP = WiFi.softAPIP();
  128.   Serial.print("AP IP address: ");
  129.   Serial.println(IP);
  130.  
  131.   // Print ESP8266 Local IP Address
  132.   Serial.println(WiFi.localIP());
  133.  
  134.   // Route for root / web page
  135.   server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
  136.     request->send_P(200, "text/html", index_html, processor);
  137.   });
  138.   server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest * request) {
  139.     request->send_P(200, "text/plain", String(t).c_str());
  140.   });
  141.   server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest * request) {
  142.     request->send_P(200, "text/plain", String(h).c_str());
  143.   });
  144.  
  145.  
  146.   //https://www.esp8266.com/viewtopic.php?f=32&t=13496
  147.   WiFi.setOutputPower(0.2); //20.5 max
  148.  
  149.   // Start server
  150.   server.begin();
  151. }
  152.  
  153. void loop() {
  154.   unsigned long currentMillis = millis();
  155.   if (currentMillis - previousMillis >= interval) {
  156.     // save the last time you updated the DHT values
  157.     previousMillis = currentMillis;
  158.     // Read temperature as Celsius (the default)
  159.     float newT = dht.readTemperature();
  160.     // Read temperature as Fahrenheit (isFahrenheit = true)
  161.     //float newT = dht.readTemperature(true);
  162.     // if temperature read failed, don't change t value
  163.     if (isnan(newT)) {
  164.       Serial.println("Failed to read from DHT sensor!");
  165.     }
  166.     else {
  167.       t = newT;
  168.       Serial.println(t);
  169.     }
  170.     // Read Humidity
  171.     float newH = dht.readHumidity();
  172.     // if humidity read failed, don't change h value
  173.     if (isnan(newH)) {
  174.       Serial.println("Failed to read from DHT sensor!");
  175.     }
  176.     else {
  177.       h = newH;
  178.       Serial.println(h);
  179.     }
  180.   }
  181. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.