//ESP32 + DHT-22 + DS18B20 + 74HC595(n) Test //The code in this sketch is explained in detail in the DHT-22, the DS18B20 and the SN74HC595N pages //and just combines the different codes. /*======================================================================================================== * DHT-22 *======================================================================================================== */ //Definitions and Pins #define DHTTYPE DHT22 //define the DHT-xx sensor const int DHTPIN = 15; //define the Data Pin (GPIO 2) //Libraries & Objects #include DHT dht(DHTPIN, DHTTYPE); //Represents sensor //Variables float dht22AirTem = 0; //takes up new readings + final average float dht22AirTemSum = 0; //takes up the sum of readings for averaging String dht22Temperature = ""; //gives back Temperature as a String for MQTT Transmission float dht22RelHum = 0; //takes up new readings + final average float dht22RelHumSum = 0; //takes up the sum of readings for averaging String dht22Humidity = ""; //gives back Humidity as a String for MQTT Transmission const uint8_t AveragingNumberDHT22 = 5; //Number of measurements to be averaged /*======================================================================================================== * DS18B20 *======================================================================================================== */ //Definitions and Pins const int ONE_WIRE_BUS = 14; //=GPIO 14 of the ESP32 as 1-Wire Bus //Libraries and Objects #include //Library for the 1-Wire protocol #include //Library for sending commands and receiving data DeviceAddress bottomSensorAddress = {0x28,0xC4,0xA0,0x51,0x38,0x19,0x01,0xC2}; //Address bottom sensor (sensor 1, Tape) DeviceAddress surfaceSensorAddress = {0x28,0x0B,0xDB,0x60,0x38,0x19,0x01,0xA3}; //Address surface sensor (sensor 2, w/o Tape) OneWire oneWire(ONE_WIRE_BUS); //Object representing the 1-Wire bus DallasTemperature DS18B20(&oneWire); //Object representing the sensors //Variables float bottomTem = 0.00; //for Temperature readings String bottomTemperature = ""; //for the Sring to be sent using MQTT float surfaceTem = 0.00; String surfaceTemperature = ""; const uint8_t AveragingNumberDS18B20 = 5; //Number of measurements to be averaged int RESOLUTION = 12; //Resolution of the sensor int TCONV = 750; //Conversion time for resolution = 12 int delayTime = (TCONV/pow(2, (12-RESOLUTION))) + 10; //Necessary delay time after measuring to get new values, depending on resolution /*======================================================================================================== * Shift Register *======================================================================================================== */ //Shift Register Pins const int LATCHPIN = 2; const int CLOCKPIN = 4; const int DATAPIN = 0; //Shift Register Pin Array byte sensor_numbers[9] = { B00000000, //all off B00000001, //Pin 1 B00000010, //Pin 2 B00000100, //Pin 3 B00001000, //Pin 4 B00010000, //Pin 5 B00100000, //Pin 6 B01000000, //Pin 7 B10000000, //Pin 8 }; /*======================================================================================================== * Setup *======================================================================================================== */ void setup() { pinMode(LATCHPIN ,OUTPUT); pinMode(CLOCKPIN ,OUTPUT); pinMode(DATAPIN ,OUTPUT); Serial.begin(115200); delay(5000); Serial.println("Measurement is starting ..."); Serial.println("=============================="); Serial.println(delayTime); } /*======================================================================================================== * Loop *======================================================================================================== */ void loop() { Serial.println("Measuring ..."); powerSwitch(2); //Turn DHT-22 ON measureDHTTemHum(AveragingNumberDHT22); powerSwitch(1); //LED Blink delay(200); powerSwitch(3); //Turn DS18B20s ON measureDS18B20Tem(AveragingNumberDS18B20); powerSwitch(1); //LED Blink delay(200); Serial.println("DHT-22 Measurement Results: "); Serial.println("Temperature: " + dht22Temperature + " °C"); Serial.println("Relative Humidity: " + dht22Humidity + " %"); Serial.println("DS18B20 Measurement Results: "); Serial.println("Sensor 1 (Bottom) Measurement: " + bottomTemperature + " °C"); Serial.println("Sensor 2 (Surface) Measurement: " + surfaceTemperature + " °C"); Serial.println("========================================"); } /*======================================================================================================== * Sensor and Shift Register Functions *======================================================================================================== */ //DS18B20 Measurement function void measureDS18B20Tem (const uint8_t AveragingNumber){ DS18B20.begin(); bottomTem = 0; surfaceTem = 0; bottomTemperature = ""; surfaceTemperature = ""; for(byte i = 0; i < AveragingNumber; i++) { DS18B20.requestTemperatures(); delay(delayTime); bottomTem += DS18B20.getTempC(bottomSensorAddress); surfaceTem += DS18B20.getTempC(surfaceSensorAddress); } bottomTem /= AveragingNumber; surfaceTem /= AveragingNumber; if(bottomTem<10) bottomTemperature = "0"; bottomTemperature += bottomTem; if(surfaceTem<10) surfaceTemperature = "0"; surfaceTemperature += surfaceTem; } //DHT22 Measurement function void measureDHTTemHum (const uint8_t AveragingNumber) { dht.begin(); //activate Sensor delay(1000); //to prevent unstable status (see Datasheet) dht22AirTemSum = 0; dht22RelHumSum = 0; dht22Temperature = ""; dht22Humidity = ""; for (byte i = 0; i < AveragingNumber; i++) //take n measurements for both { do { //execute this at least once dht22AirTem = dht.readTemperature(); //measure Temperature dht22RelHum = dht.readHumidity(); //measure Humidity if (!isnan(dht22AirTem) && !isnan(dht22RelHum)) //if results are valid, add them to the sum { dht22AirTemSum += dht22AirTem; dht22RelHumSum += dht22RelHum; } else delay(2000); //if measurement is invalid, it has to be repeated. Sensor must not heat up } while (isnan(dht22AirTem) || isnan(dht22RelHum)); //if results were invalid, repeat the loop if (i < (AveragingNumber - 1)) //after the last measurement, there is no delay needed anymore delay(2000); } dht22AirTem = dht22AirTemSum / AveragingNumber; //averaging the measurements to get more accurate results if(dht22AirTem<10) //if the Temperature is below 10°C, add a 0 to the front of the String dht22Temperature = '0'; dht22Temperature = dht22Temperature + dht22AirTem; dht22RelHum = dht22RelHumSum / AveragingNumber; if(dht22RelHum<10) dht22Humidity = '0'; dht22Humidity = dht22Humidity + dht22RelHum; } //Shift Register Power Switch void powerSwitch(byte pin){ digitalWrite(LATCHPIN, LOW); shiftOut(DATAPIN, CLOCKPIN, MSBFIRST, sensor_numbers[pin]); digitalWrite(LATCHPIN, HIGH); }