Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DHT.h"
- #include <Arduino.h>
- #include <U8g2lib.h>
- #ifdef U8X8_HAVE_HW_SPI
- #include <SPI.h>
- #endif
- #ifdef U8X8_HAVE_HW_I2C
- #include <Wire.h>
- #endif
- #define DHTPIN 4 // what digital pin the DHT22 is conected to
- #define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
- DHT dht(DHTPIN, DHTTYPE);
- U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
- void setup() {
- u8g2.begin();
- Serial.begin(9600);
- Serial.setTimeout(2000);
- // Wait for serial to initialize.
- while(!Serial) { }
- Serial.println("Device Started");
- Serial.println("-------------------------------------");
- Serial.println("Running DHT!");
- Serial.println("-------------------------------------");
- }
- int timeSinceLastRead = 0;
- void loop() {
- u8g2.clearBuffer(); // clear the internal memory
- u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
- u8g2.drawStr(0,10,"Luftfeuchte: "); // write something to the internal memory
- u8g2.sendBuffer(); // transfer internal memory to the display
- // Report every 2 seconds.
- if(timeSinceLastRead > 2000) {
- // Reading temperature or humidity takes about 250 milliseconds!
- // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
- float h = dht.readHumidity();
- // Read temperature as Celsius (the default)
- float t = dht.readTemperature();
- // Read temperature as Fahrenheit (isFahrenheit = true)
- float f = dht.readTemperature(true);
- // Check if any reads failed and exit early (to try again).
- if (isnan(h) || isnan(t) || isnan(f)) {
- Serial.println("Failed to read from DHT sensor!");
- timeSinceLastRead = 0;
- return;
- }
- // Compute heat index in Fahrenheit (the default)
- float hif = dht.computeHeatIndex(f, h);
- // Compute heat index in Celsius (isFahreheit = false)
- float hic = dht.computeHeatIndex(t, h, false);
- Serial.print("Humidity: ");
- Serial.print(h);
- Serial.print(" %t");
- Serial.print("Temperature: ");
- Serial.print(t);
- Serial.print(" *C ");
- Serial.print(f);
- Serial.print(" *Ft");
- Serial.print("Heat index: ");
- Serial.print(hic);
- Serial.print(" *C ");
- Serial.print(hif);
- Serial.println(" *F");
- timeSinceLastRead = 0;
- }
- delay(100);
- timeSinceLastRead += 100;
- }
Add Comment
Please, Sign In to add comment