Advertisement
1CM69

AM2302_on_NodeMCU_with_OLED_and_WiFi_Web_Server

Jul 19th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <Adafruit_GFX.h>
  3. #include <Adafruit_SSD1306.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <DHT.h>
  6. #include <Arduino.h>
  7. #include <ESP8266WiFi.h>
  8. #include <Hash.h>
  9. #include <ESPAsyncTCP.h>
  10. #include <ESPAsyncWebServer.h>
  11.  
  12. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  13. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  14.  
  15. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  16. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  17.  
  18. #define DHTPIN 14     // Digital pin connected to the DHT sensor
  19.  
  20. // Uncomment the type of sensor in use:
  21. //#define DHTTYPE    DHT11     // DHT 11
  22. #define DHTTYPE    DHT22     // DHT 22 (AM2302)
  23. //#define DHTTYPE    DHT21     // DHT 21 (AM2301)
  24.  
  25. DHT dht(DHTPIN, DHTTYPE);
  26.  
  27. const char* ssid = "******";
  28. const char* password = "******";
  29.  
  30. AsyncWebServer server(80);
  31.  
  32. String readDHTTemperature() {
  33.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  34.   // Read temperature as Celsius (the default)
  35.   float t = dht.readTemperature();
  36.   // Read temperature as Fahrenheit (isFahrenheit = true)
  37.   //float t = dht.readTemperature(true);
  38.   // Check if any reads failed and exit early (to try again).
  39.   if (isnan(t)) {    
  40.     Serial.println("Failed to read from DHT sensor!");
  41.     return "--";
  42.   }
  43.   else {
  44.     Serial.println(t);
  45.     return String(t);
  46.   }
  47. }
  48.  
  49. String readDHTHumidity() {
  50.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  51.   float h = dht.readHumidity();
  52.   if (isnan(h)) {
  53.     Serial.println("Failed to read from DHT sensor!");
  54.     return "--";
  55.   }
  56.   else {
  57.     Serial.println(h);
  58.     return String(h);
  59.   }
  60. }
  61.  
  62. const char index_html[] PROGMEM = R"rawliteral(
  63. <!DOCTYPE HTML><html>
  64. <head>
  65.  <meta name="viewport" content="width=device-width, initial-scale=1">
  66.  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  67.   <style>
  68.     html {
  69.      font-family: Arial;
  70.      display: inline-block;
  71.      margin: 0px auto;
  72.      text-align: center;
  73.     }
  74.     h2 { font-size: 3.0rem; }
  75.     p { font-size: 3.0rem; }
  76.     .units { font-size: 1.2rem; }
  77.     .dht-labels{
  78.       font-size: 1.5rem;
  79.       vertical-align:middle;
  80.       padding-bottom: 15px;
  81.     }
  82.   </style>
  83. </head>
  84. <body>
  85.   <h2>ESP8266 DHT Server</h2>
  86.   <p>
  87.     <i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
  88.     <span class="dht-labels">Temperature</span>
  89.     <span id="temperature">%TEMPERATURE%</span>
  90.     <sup class="units">&deg;C</sup>
  91.   </p>
  92.   <p>
  93.     <i class="fas fa-tint" style="color:#00add6;"></i>
  94.     <span class="dht-labels">Humidity</span>
  95.     <span id="humidity">%HUMIDITY%</span>
  96.     <sup class="units">%</sup>
  97.   </p>
  98. </body>
  99. <script>
  100. setInterval(function ( ) {
  101.   var xhttp = new XMLHttpRequest();
  102.   xhttp.onreadystatechange = function() {
  103.     if (this.readyState == 4 && this.status == 200) {
  104.       document.getElementById("temperature").innerHTML = this.responseText;
  105.     }
  106.   };
  107.   xhttp.open("GET", "/temperature", true);
  108.   xhttp.send();
  109. }, 10000 ) ;
  110.  
  111. setInterval(function ( ) {
  112.   var xhttp = new XMLHttpRequest();
  113.   xhttp.onreadystatechange = function() {
  114.     if (this.readyState == 4 && this.status == 200) {
  115.       document.getElementById("humidity").innerHTML = this.responseText;
  116.     }
  117.   };
  118.   xhttp.open("GET", "/humidity", true);
  119.   xhttp.send();
  120. }, 10000 ) ;
  121. </script>
  122. </html>)rawliteral";
  123.  
  124. // Replaces placeholder with DHT values
  125. String processor(const String& var){
  126.  //Serial.println(var);
  127.  if(var == "TEMPERATURE"){
  128.    return readDHTTemperature();
  129.  }
  130.  else if(var == "HUMIDITY"){
  131.    return readDHTHumidity();
  132.  }
  133.  return String();
  134. }
  135.  
  136. void setup(){
  137.  // Serial port for debugging purposes
  138.  Serial.begin(115200);
  139.  //Serial.setDebugOutput(true);
  140.  dht.begin();
  141.  
  142.  // Connect to Wi-Fi
  143.  WiFi.begin(ssid, password);
  144.  Serial.println("Connecting to WiFi");
  145.  while (WiFi.status() != WL_CONNECTED) {
  146.    delay(1000);
  147.    Serial.println(".");
  148.  }
  149.  // Print ESP8266 Local IP Address
  150.  Serial.println(WiFi.localIP());
  151.  
  152.  // Route for root / web page
  153.  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  154.    request->send_P(200, "text/html", index_html, processor);
  155.  });
  156.  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
  157.    request->send_P(200, "text/plain", readDHTTemperature().c_str());
  158.  });
  159.  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
  160.    request->send_P(200, "text/plain", readDHTHumidity().c_str());
  161.  });
  162.  
  163.  // Start server
  164.  server.begin();
  165.  
  166.  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  167.    Serial.println(F("SSD1306 allocation failed"));
  168.    for(;;);
  169.  }
  170.  delay(2000);
  171.  display.clearDisplay();
  172.  display.setTextColor(WHITE);
  173.  
  174. }
  175.  
  176. void loop(){
  177.  delay(5000);
  178.  
  179.  //read temperature and humidity
  180.  float OLEDt = dht.readTemperature();
  181.  float OLEDh = dht.readHumidity();
  182.  //if (isnan(h) || isnan(t)) {
  183.    //Serial.println("Failed to read from DHT sensor!");
  184.  //}
  185.  // clear display
  186.  display.clearDisplay();
  187.  
  188.  // display temperature
  189.  display.setTextSize(1);
  190.  display.setCursor(0,0);
  191.  display.print("Temperature: ");
  192.  display.setTextSize(2);
  193.  display.setCursor(0,10);
  194.  display.print(OLEDt);
  195.  display.print(" ");
  196.  display.setTextSize(1);
  197.  display.cp437(true);
  198.  display.write(167);
  199.  display.setTextSize(2);
  200.  display.print("C");
  201.  
  202.  // display humidity
  203.  display.setTextSize(1);
  204.  display.setCursor(0, 35);
  205.  display.print("Humidity: ");
  206.  display.setTextSize(2);
  207.  display.setCursor(0, 45);
  208.  display.print(OLEDh);
  209.  display.print(" %");
  210.  
  211.  display.display();
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement