Advertisement
LeventeDaradici

AHT10 - ESP8266 - Digital Temperature and Humidity Sensor with webserver - with auto reconnect

Jan 16th, 2022
3,736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. #include <Adafruit_AHTX0.h>
  2. Adafruit_AHTX0 aht;
  3. #include <Arduino.h>
  4. #include <ESP8266WiFi.h>
  5. #include <Hash.h>
  6. #include <ESPAsyncTCP.h>
  7. #include <ESPAsyncWebServer.h>
  8. #include <Adafruit_Sensor.h>
  9. #include <GyverOLED.h>
  10. GyverOLED<SSH1106_128x64> oled;
  11.  
  12. const char* ssid = "YOUR WIFI SSID";
  13. const char* password = "YOUR WIFI PASSWORD";
  14.  
  15. float t = 0.0;
  16. float h = 0.0;
  17.  
  18. AsyncWebServer server(80);
  19.  
  20. unsigned long previousMillis = 0;
  21. const long interval = 10000;  
  22.  
  23. const char index_html[] PROGMEM = R"rawliteral(
  24. <!DOCTYPE HTML><html>
  25. <head>
  26.  <meta charset=utf-8>
  27.  <meta name="viewport" content="width=device-width, initial-scale=1">
  28.  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  29.  
  30.   <style>
  31.     html {
  32.      font-family: Arial;
  33.      display: inline-block;
  34.      margin: 0px auto;
  35.      text-align: center;
  36.     }
  37.     h2 { font-size: 3.0rem; }
  38.     p { font-size: 3.0rem; }
  39.     .units { font-size: 1.2rem; }
  40.     .dht-labels{
  41.       font-size: 1.5rem;
  42.       vertical-align:middle;
  43.       padding-bottom: 15px;
  44.     }
  45.    
  46.   </style>
  47.  
  48. </head>
  49. <body>
  50. <style>
  51. body {
  52.   background-image: url('https://i.imgur.com/U6G3O4C.jpg');
  53.   background-repeat: no-repeat;
  54.   background-attachment: fixed;
  55.   background-size: cover;
  56.   color: white;
  57.   text-shadow: 0 0 18px #000000, 0 0 18px #000000;
  58. }
  59. </style>
  60. <h1>
  61.   <h2>Cameră mare</h2>
  62.   <p>
  63.     <i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
  64.     <span class="dht-labels">Temperatură</span>
  65.     <span id="temperature">%TEMPERATURE%</span>
  66.     <sup class="units">&deg;C</sup>
  67.   </p>
  68.   <p>
  69.     <i class="fas fa-tint" style="color:#00add6;"></i>
  70.     <span class="dht-labels">Umiditate</span>
  71.     <span id="humidity">%HUMIDITY%</span>
  72.     <sup class="units">%</sup>
  73.   </p>
  74. </body>
  75. <script>
  76. setInterval(function ( ) {
  77.   var xhttp = new XMLHttpRequest();
  78.   xhttp.onreadystatechange = function() {
  79.     if (this.readyState == 4 && this.status == 200) {
  80.       document.getElementById("temperature").innerHTML = this.responseText;
  81.     }
  82.   };
  83.   xhttp.open("GET", "/temperature", true);
  84.   xhttp.send();
  85. }, 10000 ) ;
  86.  
  87. setInterval(function ( ) {
  88.   var xhttp = new XMLHttpRequest();
  89.   xhttp.onreadystatechange = function() {
  90.     if (this.readyState == 4 && this.status == 200) {
  91.       document.getElementById("humidity").innerHTML = this.responseText;
  92.     }
  93.   };
  94.   xhttp.open("GET", "/humidity", true);
  95.   xhttp.send();
  96. }, 10000 ) ;
  97. </script>
  98. </html>)rawliteral";
  99.  
  100. String processor(const String& var){
  101.  if(var == "TEMPERATURE"){
  102.    return String(t);
  103.  }
  104.  else if(var == "HUMIDITY"){
  105.    return String(h);
  106.  }
  107.  return String();
  108. }
  109.  
  110. IPAddress local_IP(192, 168, 0, 202);
  111. IPAddress gateway(192, 168, 0, 1);
  112. IPAddress subnet(255, 255, 255, 0);
  113. IPAddress primaryDNS(193, 231, 236, 25);  
  114. IPAddress secondaryDNS(193, 231, 236, 30);
  115.  
  116. void setup() {
  117.  delay(2000);
  118.  Serial.begin(115200);
  119.  oled.init();
  120.  oled.clear();
  121.  oled.update();
  122.  aht.begin();
  123.  
  124.  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
  125.    Serial.println("STA Failed to configure");
  126.  }
  127.  Serial.print("Connecting to ");
  128.  oled.clear();
  129.  oled.setScale(1);
  130.  oled.setCursor(10, 1);  
  131.  oled.print("connecting to wifi");
  132.  oled.update();
  133.  oled.setCursor(1, 4);  
  134.  oled.setScale(2);
  135.  Serial.println(ssid);
  136.  WiFi.begin(ssid, password);
  137.  while (WiFi.status() != WL_CONNECTED) {
  138.    Serial.print(".");
  139.    oled.print(">");
  140.    oled.update();
  141.    delay(500);
  142.  }
  143.  
  144.  Serial.println(WiFi.localIP());
  145.  oled.clear();
  146.  oled.setScale(1);
  147.  oled.setCursor(28, 3);  
  148.  oled.print("CONNECTED TO");
  149.  oled.setCursor(25, 5);  
  150.  oled.print(WiFi.localIP());
  151.  oled.update();
  152.  delay(2000);
  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", String(t).c_str());
  158.  });
  159.  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
  160.    request->send_P(200, "text/plain", String(h).c_str());
  161.  });
  162.  
  163.  server.begin();
  164.  
  165. }
  166.  
  167. void loop() {
  168.  oled.home();
  169.  sensors_event_t humidity, temp;
  170.  aht.getEvent(&humidity, &temp);
  171.  
  172. unsigned long currentMillis = millis();
  173.  if (currentMillis - previousMillis >= interval) {
  174.    previousMillis = currentMillis;
  175.    if (WiFi.status() != WL_CONNECTED)
  176.          {
  177.            WiFi.disconnect();
  178.            oled.clear();
  179.            oled.setScale(1);
  180.            oled.setCursor(10, 1);  
  181.            oled.print("connecting to wifi");
  182.            oled.update();
  183.            oled.setCursor(1, 4);  
  184.            oled.setScale(2);
  185.            WiFi.begin(ssid, password);
  186.                 while (WiFi.status() != WL_CONNECTED)
  187.                 {
  188.                    Serial.print(".");
  189.                    oled.print(">");
  190.                    oled.update();
  191.                    delay(500);
  192.                 }
  193.  
  194.            Serial.println(WiFi.localIP());
  195.            oled.clear();
  196.            oled.setScale(1);
  197.            oled.setCursor(28, 3);  
  198.            oled.print("CONNECTED TO");
  199.            oled.setCursor(25, 5);  
  200.            oled.print(WiFi.localIP());
  201.            oled.update();
  202.            delay(2000);
  203.          }
  204.    float newT = temp.temperature;
  205.    if (isnan(newT)) {
  206.      Serial.println("Failed to read from AHT sensor!");
  207.    }
  208.    else {
  209.      t = newT;
  210.      Serial.println(t);
  211.      oled.clear();
  212.      oled.setScale(3);
  213.      oled.setCursor(8, 1);  
  214.      oled.print(t);
  215.      oled.setScale(1);
  216.      oled.print("*");
  217.      oled.setScale(3);
  218.      oled.print("C");    
  219.      oled.update();
  220.    }
  221.  
  222.    float newH = humidity.relative_humidity;
  223.    if (isnan(newH)) {
  224.      Serial.println("Failed to read from AHT sensor!");
  225.    }
  226.    else {
  227.      h = newH;
  228.      Serial.println(h);
  229.      oled.setScale(3);
  230.      oled.setCursor(8, 5);  
  231.      oled.print(h);
  232.      oled.setScale(1);
  233.      oled.print(" ");
  234.      oled.setScale(3);
  235.      oled.print("%");
  236.      oled.update();
  237.    }
  238.  }
  239. }
  240.  
  241.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement