SHARE
TWEET

Untitled

a guest Feb 18th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "DHT.h"
  2. #include <Arduino.h>
  3. #include <U8g2lib.h>
  4.  
  5. #ifdef U8X8_HAVE_HW_SPI
  6. #include <SPI.h>
  7. #endif
  8. #ifdef U8X8_HAVE_HW_I2C
  9. #include <Wire.h>
  10. #endif
  11.  
  12. #define DHTPIN 4     // what digital pin the DHT22 is conected to
  13. #define DHTTYPE DHT22   // there are multiple kinds of DHT sensors
  14.  
  15. DHT dht(DHTPIN, DHTTYPE);
  16. U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
  17.  
  18. void setup() {
  19.   u8g2.begin();
  20.   Serial.begin(9600);
  21.   Serial.setTimeout(2000);
  22.  
  23.   // Wait for serial to initialize.
  24.   while(!Serial) { }
  25.  
  26.   Serial.println("Device Started");
  27.   Serial.println("-------------------------------------");
  28.   Serial.println("Running DHT!");
  29.   Serial.println("-------------------------------------");
  30.  
  31. }
  32.  
  33. int timeSinceLastRead = 0;
  34. void loop() {
  35.  
  36.   u8g2.clearBuffer();          // clear the internal memory
  37.   u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
  38.  
  39.   u8g2.drawStr(0,10,"Luftfeuchte: ");  // write something to the internal memory
  40.  
  41.   u8g2.sendBuffer();          // transfer internal memory to the display
  42.  
  43.   // Report every 2 seconds.
  44.   if(timeSinceLastRead > 2000) {
  45.     // Reading temperature or humidity takes about 250 milliseconds!
  46.     // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  47.     float h = dht.readHumidity();
  48.     // Read temperature as Celsius (the default)
  49.     float t = dht.readTemperature();
  50.     // Read temperature as Fahrenheit (isFahrenheit = true)
  51.     float f = dht.readTemperature(true);
  52.  
  53.     // Check if any reads failed and exit early (to try again).
  54.     if (isnan(h) || isnan(t) || isnan(f)) {
  55.       Serial.println("Failed to read from DHT sensor!");
  56.       timeSinceLastRead = 0;
  57.       return;
  58.     }
  59.  
  60.     // Compute heat index in Fahrenheit (the default)
  61.     float hif = dht.computeHeatIndex(f, h);
  62.     // Compute heat index in Celsius (isFahreheit = false)
  63.     float hic = dht.computeHeatIndex(t, h, false);
  64.  
  65.     Serial.print("Humidity: ");
  66.     Serial.print(h);
  67.     Serial.print(" %t");
  68.     Serial.print("Temperature: ");
  69.     Serial.print(t);
  70.     Serial.print(" *C ");
  71.     Serial.print(f);
  72.     Serial.print(" *Ft");
  73.     Serial.print("Heat index: ");
  74.     Serial.print(hic);
  75.     Serial.print(" *C ");
  76.     Serial.print(hif);
  77.     Serial.println(" *F");
  78.  
  79.     timeSinceLastRead = 0;
  80.   }
  81.   delay(100);
  82.   timeSinceLastRead += 100;
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top