Skip to content

Instantly share code, notes, and snippets.

@ahmetus
Created March 23, 2020 14:24
Show Gist options
  • Save ahmetus/a5b1c2022f91ad44ad54295d87abf886 to your computer and use it in GitHub Desktop.
Save ahmetus/a5b1c2022f91ad44ad54295d87abf886 to your computer and use it in GitHub Desktop.

Revisions

  1. ahmetus created this gist Mar 23, 2020.
    63 changes: 63 additions & 0 deletions esp32_dht11.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #include <Adafruit_Sensor.h>
    #include <DHT.h>

    #define DHTPIN 27 // Digital pin connected to the DHT sensor

    #define DHTTYPE DHT11 // DHT 11

    DHT dht(DHTPIN, DHTTYPE);

    String readDHTTemperature() {
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    //float t = dht.readTemperature(true);
    // Check if any reads failed and exit early (to try again).
    if (isnan(t)) {
    Serial.println("DHT sensorden sicaklik bilgisi alinamadi!");
    return "--";
    }
    else {
    Serial.print("Sicaklik : ");
    Serial.print(t);
    Serial.println(" Derece");
    //Serial.print("Derecesi : ");
    return String(t);

    }
    }

    String readDHTHumidity() {
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    if (isnan(h)) {
    Serial.println("DHT sensorden nem bilgisi alinamadi!!");
    return "--";
    }
    else {
    Serial.print("Nem Oranı : ");
    Serial.print(h);
    Serial.println(" Gram/metrekup");
    //Serial.print("Gram/metrekup olcusunde : ");
    return String(h);

    }
    }

    void setup() {
    // Serial port for debugging purposes
    Serial.begin(115200);

    dht.begin();
    Serial.println(readDHTTemperature());
    Serial.println(readDHTHumidity());

    }

    void loop() {
    delay(2000);
    setup();
    delay(2000);

    }