Rickth

Rain Add

May 24th, 2020
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********
  2.   Original code by Rui Santos
  3.   https://randomnerdtutorials.com/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide/
  4.   modified/externded by Rick Hodges
  5. *********/
  6.  
  7. // Import required libraries
  8. #include <Arduino.h>
  9. #include <ESP8266WiFi.h>
  10. #include <Hash.h>
  11. #include <ESPAsyncTCP.h>
  12. #include <ESPAsyncWebServer.h>
  13. #include <Adafruit_Sensor.h>
  14. #include <DHT.h>
  15.  
  16. // Replace with your network credentials
  17. const char* ssid = "********";
  18. const char* password = "**********";
  19.  
  20. #define DHTPIN 5     // Digital pin connected to the DHT sensor
  21. #define RAINPIN 14  // Digital pin connected to the Rain Gauge
  22.  
  23. // Uncomment the type of sensor in use:
  24. //#define DHTTYPE    DHT11     // DHT 11
  25. #define DHTTYPE    DHT22     // DHT 22 (AM2302)
  26. //#define DHTTYPE    DHT21     // DHT 21 (AM2301)
  27.  
  28. DHT dht(DHTPIN, DHTTYPE);
  29.  
  30. // current temperature & humidity, updated in loop()
  31. float t = 0.0;
  32. float h = 0.0;
  33. float rday = 0.0;
  34. float rmon = 0.0;
  35.  
  36. volatile boolean rained = false;  //interrupt flag;
  37.  
  38. // Create AsyncWebServer object on port 80
  39. AsyncWebServer server(80);
  40.  
  41. // Generally, you should use "unsigned long" for variables that hold time
  42. // The value will quickly become too large for an int to store
  43. unsigned long previousMillis = 0;    // will store last time DHT was updated
  44.  
  45. // Updates DHT readings every 10 seconds
  46. const long interval = 10000;  
  47.  
  48. const char index_html[] PROGMEM = R"rawliteral(
  49. <!DOCTYPE HTML><html>
  50. <head>
  51.  <meta name="viewport" content="width=device-width, initial-scale=1">
  52.  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  53.   <style>
  54.     html {
  55.      font-family: Arial;
  56.      display: inline-block;
  57.      margin: 0px auto;
  58.      text-align: center;
  59.     }
  60.     h2 { font-size: 3.0rem; }
  61.     p { font-size: 3.0rem; }
  62.     .units { font-size: 1.2rem; }
  63.     .dht-labels{
  64.       font-size: 1.5rem;
  65.       vertical-align:middle;
  66.       padding-bottom: 15px;
  67.     }
  68.   </style>
  69. </head>
  70. <body>
  71.   <h2>McKenzie Rd - Weather</h2>
  72.   <p>
  73.     <i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
  74.     <span class="dht-labels">Temperature</span>
  75.     <span id="temperature">%TEMPERATURE%</span>
  76.     <sup class="units">&deg;F</sup>
  77.   </p>
  78.   <p>
  79.     <i class="fas fa-tint" style="color:#00add6;"></i>
  80.     <span class="dht-labels">Humidity</span>
  81.     <span id="humidity">%HUMIDITY%</span>
  82.     <sup class="units">&#37;</sup>
  83.   </p>
  84.   <p>
  85.     <i class="fas fa-cloud-showers-heavy" style="color:#99e6ff;"></i>
  86.     <span class="dht-labels">Rainfall</span>
  87.     <span id="Rainfall">%RAINFALL%</span>
  88.     <sup class="units">In.</sup>
  89.   </p>
  90. </body>
  91. <script>
  92. setInterval(function ( ) {
  93.   var xhttp = new XMLHttpRequest();
  94.   xhttp.onreadystatechange = function() {
  95.     if (this.readyState == 4 && this.status == 200) {
  96.       document.getElementById("temperature").innerHTML = this.responseText;
  97.     }
  98.   };
  99.   xhttp.open("GET", "/temperature", true);
  100.   xhttp.send();
  101. }, 10000 ) ;
  102.  
  103. setInterval(function ( ) {
  104.   var xhttp = new XMLHttpRequest();
  105.   xhttp.onreadystatechange = function() {
  106.     if (this.readyState == 4 && this.status == 200) {
  107.       document.getElementById("humidity").innerHTML = this.responseText;
  108.     }
  109.   };
  110.   xhttp.open("GET", "/humidity", true);
  111.   xhttp.send();
  112. }, 10000 ) ;
  113.  
  114. setInterval(function ( ) {
  115.   var xhttp = new XMLHttpRequest();
  116.   xhttp.onreadystatechange = function() {
  117.     if (this.readyState == 4 && this.status == 200) {
  118.       document.getElementById("Rainfall").innerHTML = this.responseText;
  119.     }
  120.   };
  121.   xhttp.open("GET", "/Rainfall", true);
  122.   xhttp.send();
  123. }, 10000 ) ;
  124. </script>
  125. </html>)rawliteral";
  126.  
  127. // Replaces placeholder with DHT values
  128. String processor(const String& var){
  129.  //Serial.println(var);
  130.  if(var == "TEMPERATURE"){
  131.    return String(t);
  132.  }
  133.  else if(var == "HUMIDITY"){
  134.    return String(h);
  135.  }
  136.  else if(var == "RAINFALL"){
  137.    return String(rday);
  138.  }
  139.  return String();
  140. }
  141.  
  142. ICACHE_RAM_ATTR void measurerain(){
  143.  rained = true;
  144. }
  145.  
  146.  
  147. void setup(){
  148.  // create interrupt
  149.  pinMode (RAINPIN ,INPUT);
  150.  attachInterrupt(digitalPinToInterrupt(RAINPIN), measurerain, RISING);
  151.  
  152.  // Serial port for debugging purposes
  153.  Serial.begin(115200);
  154.  dht.begin();
  155.  
  156.  // Connect to Wi-Fi
  157.  WiFi.begin(ssid, password);
  158.  Serial.println("Connecting to WiFi");
  159.  while (WiFi.status() != WL_CONNECTED) {
  160.    delay(1000);
  161.    Serial.println(".");
  162.  }
  163.  
  164.  // Print ESP8266 Local IP Address
  165.  Serial.println(WiFi.localIP());
  166.  
  167.  // Route for root / web page
  168.  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  169.    request->send_P(200, "text/html", index_html, processor);
  170.  });
  171.  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
  172.    request->send_P(200, "text/plain", String(t).c_str());
  173.  });
  174.  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
  175.    request->send_P(200, "text/plain", String(h).c_str());
  176.  });
  177.  server.on("/Rainfall", HTTP_GET, [](AsyncWebServerRequest *request){
  178.    request->send_P(200, "text/plain", String(rday).c_str());
  179.  });
  180.  
  181.  // Start server
  182.  server.begin();
  183.  
  184.  
  185. }
  186.  
  187. void loop(){  
  188.  unsigned long currentMillis = millis();
  189.  if (currentMillis - previousMillis >= interval) {
  190.    // save the last time you updated the DHT values
  191.    previousMillis = currentMillis;
  192.    // Read temperature as Celsius (the default)
  193.    //float newT = dht.readTemperature();
  194.    // Read temperature as Fahrenheit (isFahrenheit = true)
  195.    float newT = dht.readTemperature(true);
  196.    // if temperature read failed, don't change t value
  197.    if (isnan(newT)) {
  198.      Serial.println("Failed to read from DHT sensor!");
  199.    }
  200.    else {
  201.      t = newT;
  202.      Serial.println(t);
  203.    }
  204.    // Read Humidity
  205.    float newH = dht.readHumidity();
  206.    // if humidity read failed, don't change h value
  207.    if (isnan(newH)) {
  208.      Serial.println("Failed to read from DHT sensor!");
  209.    }
  210.    else {
  211.      h = newH;
  212.      Serial.println(h);
  213.    }
  214.    if (rained) {
  215.      rained = false;
  216.      rday += 0.01;
  217.      rmon += 0.01;
  218.      Serial.print("Rain today = ");
  219.      Serial.print(rday, 2);
  220.      Serial.println(" inches.");
  221.    }
  222.  }
  223. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×