Snippets

David Stacer ESP32 with both DHT12 and DS18B20 Temp sensors

Created by David Stacer

#include <WEMOS_DHT12.h>


// https://github.com/milesburton/Arduino-Temperature-Control-Library
// https://github.com/PaulStoffregen/OneWire

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 23

#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  5        /* Time ESP32 will go to sleep (in seconds) */
#define LED_BUILTIN 2
RTC_DATA_ATTR uint32_t loopcount = 0;

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);


// the setup function runs once when you press reset or power the board
void setup() {
  
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  
  Wire.begin();

  sensors.begin();
  // some debug code for the one wire sensor
//    Serial.print("Found ");
//    Serial.print(sensors.getDeviceCount(), DEC);
//    Serial.println(" devices.");
//    Serial.print("One wire Pin ");

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
}



// the loop function runs over and over again forever
void loop() {
  DHT12 dht12;

  sensors.requestTemperatures();   // get temp from DS18B20  (black wire probe)
  float DS_C, DS_F;
  if (sensors.getDeviceCount() > 0 ) {     // make sure there is a sensor.
    DS_C = sensors.getTempCByIndex(0);
    DS_F = (DS_C * 1.8 ) + 32;        // convert C to F
    Serial.print("External Temp C: "); Serial.print(DS_C);
    Serial.print(" F: ");    Serial.println(DS_F);
  }
  // Get temp from DHT12 sensor (blue one on the board)
  if (dht12.get() == 0) {
    Serial.print("Inside                Temp C: ");
    Serial.print(dht12.cTemp);
    Serial.print(" F: ");
    Serial.print(dht12.fTemp);
    Serial.print("  H: ");
    Serial.println(dht12.humidity);
  }
  else Serial.println("Error reading DH12");

  Serial.print("Sleeping now, Loop count ");
  Serial.println(loopcount++);
  //delay(60000);
  esp_deep_sleep_start();
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.